【问题标题】:How to request an access token with OAuth2.0?如何使用 OAuth2.0 请求访问令牌?
【发布时间】: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


    【解决方案1】:

    两件事:

    1. 错误 (invalid_client) 听起来像是服务无法识别 client_id。我建议尝试 curl(请参阅他们列出的示例)以确保该服务对您的凭据提供良好的响应,然后再尝试使其在您的脚本中工作。

    2. 没有看到变量“tokens”,我无法判断 JSON 转换是否成功。我会检查令牌的类型以查看 JSON 是否已成功转换以及该密钥是否存在。

    【讨论】:

      猜你喜欢
      • 2020-11-17
      • 2013-06-24
      • 2013-04-30
      • 2019-08-17
      • 2019-11-27
      • 2019-02-19
      • 2020-12-12
      • 2012-03-23
      • 1970-01-01
      相关资源
      最近更新 更多