-
Notifications
You must be signed in to change notification settings - Fork 3
/
salesforce_user_agent_flow.py
73 lines (59 loc) · 1.91 KB
/
salesforce_user_agent_flow.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# _author_ = "Jean-Claude Tissier"
# _github_ = "https://github.com/jctissier/Salesforce-Oauth2-REST-Metadata-API-Python-Examples"
import requests
class SalesforceOAuth2(object):
"""
Salesforce User-Agent Oauth Authentication Flow
"""
_authorize_url = '/services/oauth2/authorize'
def __init__(self, client_id, redirect_uri, sandbox):
"""
Create SalesforceOauth2 object
:param client_id: Connected App's Consumer Key
:param redirect_uri: Callback URL once logged in
:param sandbox: Boolean flag to determine authentication site
"""
if sandbox:
self.auth_site = 'https://test.salesforce.com'
else:
self.auth_site = 'https://login.salesforce.com'
self.client_id = client_id
self.redirect_uri = redirect_uri
def get_access_token(self):
"""
Sets the body of the POST request
:return: POST response
"""
body = {
'response_type': 'token',
'client_id': self.client_id,
'redirect_uri': self.redirect_uri
}
response = self._request_token(body)
return response
def _request_token(self, data):
"""
Sends a POST request to Salesforce to authenticate credentials
:param data: body of the POST request
:return: POST response
"""
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
response = requests.post(
"{site}{token}".format(
site=self.auth_site,
token=self._authorize_url
),
data=data,
headers=headers
)
return response
"""
Testing data
oauth = SalesforceOAuth2(
client_id='your_client_id',
redirect_uri='https://www.enter-url-here.com/',
sandbox=True
)
sf_authentication = oauth.get_access_token()
print(sf_authentication.text)
"""