【发布时间】:2021-12-17 22:43:21
【问题描述】:
我仍在学习如何使用 REST API。现在我想为我的移动应用程序开发集成一个 FatSecret Rest API。我目前卡在第 2 步 here。
这是我从 HERE 引用的当前代码(在 CLIENT CREDENTIALS GRANT TYPE 部分下):
import requests, json
import urllib3
urllib3.disable_warnings()
token_url = "https://oauth.fatsecret.com/connect/token"
test_api_url = "https://platform.fatsecret.com/rest/server.api"
#client (application) credentials
client_id = "<CLIENT_ID>"
client_secret = "<CLIENT_SECRET>"
#step A, B - single call with client credentials as the basic auth header - will return access_token
data = {'grant_type': 'client_credentials',
'scope': 'basic'}
access_token_response = requests.post(token_url, data=data, verify=False, allow_redirects=False, auth=(client_id, client_secret))
print(access_token_response.headers)
print(access_token_response.text)
print(access_token_response.status_code)
tokens = json.loads(access_token_response.text)
print("access token: " + tokens['access_token'])
#step B - with the returned access_token we can make as many calls as we want
api_call_headers = {'Authorization': 'Bearer ' + tokens['access_token']}
api_call_response = requests.get(test_api_url, headers=api_call_headers, verify=False)
print(api_call_response.text)
但我收到了这些错误:
{'Date': 'Wed, 03 Nov 2021 14:13:17 GMT', 'Content-Type': 'application/json; charset=UTF-8', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'Set-Cookie': 'AWSALB=ulzLdAF+yzvlb1zyySg3WOSoBgvDZVL924yxgKUcgcXfDccOCZVe+toy32y9oHStYz3ljYQBgSuXCZvC5bKSALd5nRVnsmb/kOipLY7EcSG5qUjcDUkkfkRxbtjj; Expires=Wed, 10 Nov 2021 14:13:16 GMT; Path=/, AWSALBCORS=ulzLdAF+yzvlb1zyySg3WOSoBgvDZVL924yxgKUcgcXfDccOCZVe+toy32y9oHStYz3ljYQBgSuXCZvC5bKSALd5nRVnsmb/kOipLY7EcSG5qUjcDUkkfkRxbtjj; Expires=Wed, 10 Nov 2021 14:13:16 GMT; Path=/; SameSite=None; Secure', 'Cache-Control': 'no-store, no-cache, max-age=0', 'Pragma': 'no-cache', 'Server': 'Kestrel', 'X-Powered-By': 'ASP.NET'}
{"error":"invalid_client"}
400
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-24-9611470e9c26> in <module>()
24 tokens = json.loads(access_token_response.text)
25
---> 26 print("access token: " + tokens['access_token'])
27
28 #step B - with the returned access_token we can make as many calls as we want
KeyError: 'access_token'
我已经在这个平台上尝试过其他一些解决方案,但大多数都会返回这个错误,例如 {"error":"invalid_request"} 或 {"error":"invalid_client"}。
我不确定出了什么问题?是 OAuth2.0 吗?我应该使用其他oauth吗?有人可以帮我解决这个问题。提前谢谢你。
【问题讨论】:
标签: api oauth-2.0 python-requests