Created
June 23, 2013 11:37
-
-
Save jmertic/5844722 to your computer and use it in GitHub Desktop.
Sample Python script using Requests for connecting to the new RESTful SugarCRM REST API in 6.7 and later.
This file contains 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
import json | |
import requests | |
url = "<<instanceurl>>/rest/v10/oauth2/token" | |
payload = {"grant_type":"password","username":"<<username>>","password":"<<password>>","client_id":"sugar"} | |
r = requests.post(url, data=json.dumps(payload)) | |
response = json.loads(r.text) | |
if response[u'error']: | |
print response[u'error_message'] | |
token = response[u'access_token'] | |
print 'Success! OAuth token is ' + token | |
url = "<<instanceurl>>/rest/v10/me" | |
headers = { "Content-Type" : "application/json", "OAuth-Token": token } | |
r = requests.get(url, headers=headers); | |
response = json.loads(r.text) | |
if response[u'error']: | |
print response[u'error_message'] | |
print r.json() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This will throw exceptions any time the API doesn't return an error (since 'error' won't exist).
Using r.json() seems a bit cleaner than json.loads().