[英]How to request an access token with OAuth2.0?
I'm still learning on how to use REST API.我仍在学习如何使用 REST API。 Right now I want to integrate a FatSecret Rest API for my mobile apps development.现在我想为我的移动应用程序开发集成一个 FatSecret Rest API。 I'm currently stuck at step 2 here .我目前停留在步骤2 在这里。
This is my current code which I referred from HERE (under CLIENT CREDENTIALS GRANT TYPE section):这是我从这里引用的当前代码(在 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)
But I got these errors:但我收到了这些错误:
{'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'
I've already tried some other solutions on this platform but most of them would return this error like {"error":"invalid_request"} or {"error":"invalid_client"}.我已经在这个平台上尝试过一些其他的解决方案,但大多数都会返回这个错误,比如 {"error":"invalid_request"} 或 {"error":"invalid_client"}。
I'm not sure what went wrong?我不确定出了什么问题? Is it the OAuth2.0?是 OAuth2.0 吗? Should I use other oauth?我应该使用其他 oauth 吗? Can someone kindly help me how to get through this.有人可以帮助我如何度过难关。 Thank you in advance.先感谢您。
Two things:两件事情:
The error (invalid_client) sounds like the client_id is not recognized by the service.错误 (invalid_client) 听起来像是服务无法识别 client_id。 I would suggest trying curl (see the example they listed) to make sure the service provides a good response to your credentials before trying to get it to work in your script.我建议尝试 curl (请参阅他们列出的示例)以确保该服务在尝试使其在您的脚本中工作之前对您的凭据提供良好的响应。
Without seeing the variable "tokens", I cannot tell if the JSON conversion was successful or not.没有看到变量“tokens”,我无法判断 JSON 转换是否成功。 I would check the type of tokens to see if the JSON was successfully converted and if that key exists.我会检查令牌的类型以查看 JSON 是否已成功转换以及该密钥是否存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.