Last active
August 22, 2018 01:45
-
-
Save scorfuz/7d2d53a5f22bc3218bf3070dea683da4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from rauth import OAuth2Service | |
| import json | |
| import sys | |
| SLUG = 'diane' | |
| USER_NAME = 'Tim' | |
| CLIENT_ID = 'ID' | |
| CLIENT_SECRET = 'SECRET' | |
| REDIRECT_URI = 'http://callbackurlsuccess.ca' | |
| access_token_url = 'http://' + SLUG + '.nationbuilder.com/oauth/token' | |
| authorize_url = SLUG + '.nationbuilder.com/oauth/authorize' | |
| nb_service = OAuth2Service( | |
| client_id=CLIENT_ID, | |
| client_secret=CLIENT_SECRET, | |
| name=USER_NAME, | |
| authorize_url=authorize_url, | |
| access_token_url=access_token_url, | |
| base_url=SLUG + '.nationbuilder.com') | |
| token = nb_service.get_access_token(decoder=json.loads, | |
| data={ | |
| 'code': 'foo', | |
| 'redirect_uri': REDIRECT_URI, | |
| 'grant_type': 'authorization_code' | |
| }) | |
| session = nb_service.get_session(token) | |
| people_api_url = 'http://' + SLUG + '.nationbuilder.com/api/v1/people/' | |
| # CREATE | |
| create = { | |
| 'person': { | |
| 'last_name': 'Zappa', | |
| 'first_name': 'Frank', | |
| 'full_name': 'Frank Vincent Zappa', | |
| 'email': 'frank@zappa.ca', | |
| 'sex': 'M', | |
| 'primary_address': { | |
| 'address1': '11272 Magnolia Blvd', | |
| 'city': 'Toronto', | |
| 'state': 'ON', | |
| 'country_code': 'CA' | |
| } | |
| } | |
| } | |
| response = session.post(people_api_url, | |
| params={'format': 'json', 'person': create}, | |
| headers={'content-type': 'application/json'}) | |
| if response.status_code != 204: | |
| sys.exit('could not create') | |
| # UPDATE | |
| nation_builder_id = response['person']['id'] | |
| update = { | |
| 'person': { | |
| 'mobile': '999-999-9999' | |
| } | |
| } | |
| response = session.put(people_api_url + nation_builder_id, | |
| params={'format': 'json', 'person': update}, | |
| headers={'content-type': 'application/json'}) | |
| if response.status_code != 200: | |
| sys.exit('could not update') | |
| # DELETE | |
| response = session.delete(people_api_url + nation_builder_id, | |
| params={'format': 'json'}, | |
| headers={'content-type': 'application/json'}) | |
| if response.status_code != 200: | |
| print('could not delete') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment