Skip to content

Instantly share code, notes, and snippets.

@inhumantsar
Created August 29, 2017 18:40
Show Gist options
  • Save inhumantsar/49512a6dd12b20f62136a321cb59eacf to your computer and use it in GitHub Desktop.
Save inhumantsar/49512a6dd12b20f62136a321cb59eacf to your computer and use it in GitHub Desktop.
nationbuilder python exercises
#!pip install rauth
from rauth import OAuth2Service
import json
# init work
NATION_SLUG = 'somenation'
OAUTH_ID = 'someid'
OAUTH_SECRET = 'somesecret'
REDIRECT_URI = 'http://somecallback.url'
access_token_url = "http://" + NATION_SLUG + ".nationbuilder.com/oauth/token"
authorize_url = NATION_SLUG + ".nationbuilder.com/oauth/authorize"
service = OAuth2Service(
client_id = OAUTH_ID,
client_secret = OAUTH_SECRET,
name = "any name",
authorize_url = authorize_url,
access_token_url = access_token_url,
base_url = NATION_SLUG + ".nationbuilder.com")
session = service.get_session(service.get_access_token(
decoder=json.loads,
data = {
"code": code,
"redirect_uri": REDIRECT_URI,
"grant_type": "authorization_code"
}))
# create a person
create_person = {
"person": {
"last_name": "Doe",
"first_name": "Jane",
"email": "[email protected]",
"sex": "F",
"signup_type": 0,
"employer": "Free Folk Inc",
"party": "C",
"registered_address": {
"state": "BC",
"country_code": "CA"
}
}
}
response = session.post(
"https://" + NATION_SLUG + ".nationbuilder.com/api/v1/people",
params={'format': 'json', 'person': create_person},
headers={'content-type': 'application/json'}
)
# update a person
update_person = {
"person": {
"employer": "Knee Benders Co-op"
}
}
response = session.post(
"https://" + NATION_SLUG + ".nationbuilder.com/api/v1/people/" + response['person']['id'],
params={'format': 'json', 'person': update_person},
headers={'content-type': 'application/json'}
)
# delete a person
response = session.delete(
"https://" + NATION_SLUG + ".nationbuilder.com/api/v1/people/" + response['person']['id'],
params={'format': 'json'},
headers={'content-type': 'application/json'}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment