Skip to content

Instantly share code, notes, and snippets.

@kmclaugh
Created January 27, 2019 17:56
Show Gist options
  • Save kmclaugh/2292158c57da0638337d186961f9d9ea to your computer and use it in GitHub Desktop.
Save kmclaugh/2292158c57da0638337d186961f9d9ea to your computer and use it in GitHub Desktop.
Use nationbuilder API and rauth to create, update, and delete a person
from rauth import OAuth2Service
nation_slug = "aura"
REDIRECT_URI = "aura_redirect"
## Authenticate using OAuth
access_token_url = "http://" + nation_slug + ".nationbuilder.com/oauth/token"
authorize_url = nation_slug + ".nationbuilder.com/oauth/authorize"
service = OAuth2Service(
client_id = "aaaaaaaa",
client_secret = "bbbbbbb",
name = "any name",
authorize_url = authorize_url,
access_token_url = access_token_url,
base_url = nation_slug + ".nationbuilder.com")
token = service.get_access_token(decoder=json.loads, data = {"code": code,
"redirect_uri": REDIRECT_URI, "grant_type": "authorization_code"})
session = service.get_session(token)
def endpoint(endpoint):
"""
Helper for endpoints
"""
return 'https://' + nation_slug + ".nationbuilder.com/api/v1/" + endpoint
## Create the person
person = {'person':{
'first_name': 'Kevin',
'last_name': 'McLaughlin',
'email': '[email protected]'
}}
response = session.post(endpoint("people"),
params={'format': 'json'},
json=person)
assert 201 == response.status_code, "Could not create person!"
user_id = repr(response.json().get('person').get('id'))
## Update the person by adding an address
address = {"person", {"home_address": {
"address1": "123 Nationbuilder Rd",
"address2": null,
"address3": null,
"city": "Austin",
"state": "Texas",
"country_code": "US",
"zip": "78705",
}}}
response = session.put(endpoint('people/'+user_id), params={'format': 'json'}, json=address)
assert 200 == response.status_code, "Could not update person!"
## Delete the person
#remove person
response = session.delete(endpoint('people/'+user_id), params=params)
assert 204 == response.status_code, "Could not remove person!"
@kmclaugh
Copy link
Author

Note: not sure this works because nationbuilder doesn't let you test drive their api until you become an approved developer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment