-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsalesforce_python.py
More file actions
49 lines (42 loc) · 1.8 KB
/
Copy pathsalesforce_python.py
File metadata and controls
49 lines (42 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Import Packages
import requests
import json
params = {
"grant_type": "password",
"client_id": "######", # Consumer Key
"client_secret": "#####", # Consumer Secret
"username": "######", # The email you use to login
"password": "######" # Concat your password and your security token
}
r = requests.post("https://login.salesforce.com/services/oauth2/token", params=params)
print(r.content)
access_token = r.json().get("access_token")
instance_url = r.json().get("instance_url")
print("Access Token:", access_token)
print("Instance URL", instance_url)
def sf_api_call(action, parameters = {}, method = 'get', data = {}):
#Helper function to make calls to Salesforce REST API.
#Parameters: action (the URL), URL params, method (get, post or patch), data for POST/PATCH.
headers = {
'Content-type': 'application/json',
'Accept-Encoding': 'gzip',
'Authorization': 'Bearer %s' % access_token
}
if method == 'get':
r = requests.request(method, instance_url+action, headers=headers, params=parameters, timeout=30)
elif method in ['post', 'patch']:
r = requests.request(method, instance_url+action, headers=headers, json=data, params=parameters, timeout=10)
else:
# other methods not implemented in this example
raise ValueError('Method should be get or post or patch.')
print('Debug: API %s call: %s' % (method, r.url) )
if r.status_code < 300:
if method=='patch':
return None
else:
return r.json()
els
raise Exception('API error when calling %s : %s' % (r.url, r.content))
print(json.dumps(sf_api_call('/services/data/v39.0/query/', {
'q': 'SELECT Account.Name, Name, CloseDate from Opportunity where IsClosed = False order by CloseDate ASC LIMIT 10'
}), indent=2))