【问题标题】:requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://accounts.spotify.com/api/tokenrequests.exceptions.HTTPError:400 客户端错误:对 url 的错误请求:https://accounts.spotify.com/api/token
【发布时间】:2019-05-15 05:34:16
【问题描述】:

我是 Python 3 和 Flask 的新手,我目前正在构建一个简单的 Web 应用程序来使用来自 Spotify API 的数据。

OAuth 2.0 身份验证流程几乎可以正常工作,因为我可以调用https://accounts.spotify.com/authorize

在对 API 的 POST 请求期间,我收到 HTTP 400:

authentication_token = request.args['code']
code_payload = {
    "grant_type": "authorization_code",
    "code": str(authentication_token),
    "redirect_uri": REDIRECT_URI
}
encoded_oauth2_tokens = base64.b64encode('{}:{}'.format(CLIENT_ID, CLIENT_SECRET).encode())
headers = {"Authorization": "Basic {}".format(encoded_oauth2_tokens)}
post_request = requests.post(SPOTIFY_TOKEN_URL, data=code_payload, headers=headers)
post_request.raise_for_status()

编辑:响应正文:

"GET / HTTP/1.1" 302 - b'{"error":"invalid_client"}'

不确定请求中有什么问题。

【问题讨论】:

  • 响应的正文是什么?
  • @Kaszaq 使用 print(str(post_request.content), file=sys.stderr) 打印响应正文。我没有得到太多。

标签: python python-3.x rest oauth-2.0 spotify


【解决方案1】:

我能够通过 question 的提示纠正问题。

向标头添加encoded_oauth2_tokens.decode() 是向Spotify API 发出有效请求的缺失部分。代码如下:

encoded_oauth2_tokens = base64.b64encode('{}:{}'.format(CLIENT_ID, CLIENT_SECRET).encode())
headers = {"Authorization": "Basic {}".format(encoded_oauth2_tokens.decode())}

【讨论】:

    猜你喜欢
    • 2019-11-10
    • 1970-01-01
    • 2020-05-20
    • 2020-08-25
    • 2015-08-17
    • 1970-01-01
    • 1970-01-01
    • 2020-09-30
    • 2012-07-04
    相关资源
    最近更新 更多