Created
November 17, 2011 22:29
-
-
Save metadaddy/1374762 to your computer and use it in GitHub Desktop.
Simple query against Force.com REST API in Python
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
import os | |
import urllib | |
import urllib2 | |
import json | |
import pprint | |
# Grab credentials from the environment | |
consumer_key = os.environ['CLIENT_ID'] | |
consumer_secret = os.environ['CLIENT_SECRET'] | |
username = os.environ['USERNAME'] | |
password = os.environ['PASSWORD'] | |
login_server = os.environ['LOGIN_SERVER'] | |
# Do OAuth username/password | |
token_url = login_server+'/services/oauth2/token' | |
params = urllib.urlencode({ | |
'grant_type': 'password', | |
'client_id': consumer_key, | |
'client_secret': consumer_secret, | |
'username': username, | |
'password': password | |
}) | |
data = urllib2.urlopen(token_url, params).read() | |
oauth = json.loads(data) | |
pprint.pprint(oauth) | |
# Now do a query | |
params = urllib.urlencode({ | |
'q': 'SELECT Name FROM Account' | |
}) | |
query_url = oauth['instance_url']+'/services/data/v23.0/query?%s' % params | |
headers = { | |
'Authorization': 'OAuth '+oauth['access_token'] | |
} | |
req = urllib2.Request(query_url, None, headers) | |
data = urllib2.urlopen(req).read() | |
result = json.loads(data) | |
pprint.pprint(result) |
if i may ask, what is the login server? Is the instance server? Im getting 401s all over the place...
login_server is either https://login.salesforce.com or https://test.salesforce.com depending on whether you are using a production or sandbox instance.
For those linked to this looking for the same but as a class, https://gist.github.com/JeremyDavis-/b4f4227f479054d03885
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this