这就是我使用 requests_oauthlib import OAuth2Session 获取令牌以从 Salesforce 应用程序获取访问令牌的方式
auth_service.py
`from requests_oauthlib import OAuth2Session`
def token_saver(self, token=None):
print("token to be saved", token)
def get_oauth2_requests(self, auth):
print("OAuth2", auth)
client_id = auth['oauth2']['client_id']
client_secret = auth['oauth2']['client_secret']
refresh_url = auth['oauth2']['refresh_url']
token_client = auth['oauth2']['token']
extra = {
'client_id': client_id,
'client_secret': client_secret,
}
token = {
'access_token': 'eswfld123kjhn1v5423',
'refresh_token': token_client,
'token_type': 'Bearer',
'expires_in': '-30'
}
client = OAuth2Session(client_id, token=token, auto_refresh_url=refresh_url,
auto_refresh_kwargs=extra, token_updater=self.token_saver)
return client
当它返回 oauth 2 客户端会话时。我们正在向 Salesforce 应用程序发出请求
触发器.py
if auth:
dataParams["auth"] = auth_object
print("Body", body)
print(method)
if body and method != "GET":
dataParams["data"] = json.dumps(body)
if content_type == "application/x-www-form-urlencoded":
print('Sending application x-www-form-urlencoded')
dataParams["data"] = body
dataParams["headers"] = rest_auth.get_headers(auth, content_type)
print("dataParams", dataParams)
print("URL", url, "\nMethod", method, "\nDataparams", dataParams)
response = client.request(method, url, **dataParams)
return response.text
它适用于一些 oauth 2 应用程序,如 google、asana、Microsoft,但是当它涉及到 salesforce 时,它会给出如下错误
Traceback (most recent call last):
File "F:\git-repos\unified\venv-unified\lib\site-packages\requests_oauthlib\oauth2_session.py", line 477, in request
url, headers, data = self._client.add_token(
File "F:\git-repos\unified\venv-unified\lib\site-packages\oauthlib\oauth2\rfc6749\clients\base.py", line 198, in add_token
raise TokenExpiredError()
oauthlib.oauth2.rfc6749.errors.TokenExpiredError: (token_expired)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "F:\git-repos\unified\venv-unified\lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "F:\git-repos\unified\venv-unified\lib\site-packages\flask\app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "F:\git-repos\unified\unified/modules\main\portal.py", line 184, in portal
response['result'] = rest(
File "F:\git-repos\unified\unified/modules\main\trigger.py", line 318, in rest
response = client.request(method, url, **dataParams)
File "F:\git-repos\unified\venv-unified\lib\site-packages\requests_oauthlib\oauth2_session.py", line 504, in request
url, headers, data = self._client.add_token(
File "F:\git-repos\unified\venv-unified\lib\site-packages\oauthlib\oauth2\rfc6749\clients\base.py", line 198, in add_token
raise TokenExpiredError()
oauthlib.oauth2.rfc6749.errors.TokenExpiredError: (token_expired)
我们从 token_saver() 方法获取令牌,如下所示
token to be saved {'access_token': '00D2v000002J66X!AQwAQFxIR.KkLSA74yI53A._b1Rp.uc1UXvBmpWP5erfHwULPj0yDLln0XBTXtfOcKCbPHlxGYTJWAqjMYwHH.JHKpIpgFqf', 'signature': 'O6MoT0xehqoZTvMi0wdwrD5fU+ITAXWixIf0ptt+oew=', 'scope': ['visualforce', 'custom_permissions', 'openid', 'refresh_token', 'wave_api', 'web', 'chatter_api', 'id', 'api', 'eclair_api', 'pardot_api', 'full'], 'instance_url': 'https://ap15.salesforce.com', 'id': 'https://login.salesforce.com/id/00D2v000002J66XEAS/0052v00000f0e07AAA', 'token_type': 'Bearer', 'issued_at': '1616182472230', 'refresh_token': '5Aep861dlMxAL.LhVRuNsLfAdWv0DjJVGBbZuLG8FqcTPokBCeMug96ABAgy7rQ.hAht.x5aTXCAzy5sMTnnLQd'}
我无法从 requests_oauthlib 找出天气问题或请求任何帮助,不胜感激