Skip to content

Instantly share code, notes, and snippets.

@jmertic
Created June 23, 2013 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jmertic/5844722 to your computer and use it in GitHub Desktop.
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.
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()
@faulkner
Copy link

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().

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