刷新和访问令牌时 Spotify "Unexpected status: 400" - python
Spotify "Unexpected status: 400" when refreshing and accessing token - python
当尝试使用 python 3 授权 spotify 时,我得到一个 "server_error" 和描述 "Unexpected status: 400".
我使用了正确的授权码,Spotify 文档 (https://developer.spotify.com/web-api/authorization-guide/) 指示我使用带有这些参数的 post 命令。
我在这方面很菜鸟,我不知道自己做错了什么。
代码如下:
import requests
params = {'grant_type': 'authorization_code', 'code': authcode, 'redirect_uri': 'https://example.com/callback','client_id':'example', 'client_secret':'example'}
req=requests.post('https://accounts.spotify.com/api/token', params=params)
print(req.content)
根据 spotify 自己的指南(参见步骤 #4):
https://developer.spotify.com/web-api/authorization-guide/
请求新令牌的授权信息必须通过 "Authorization" 变量进入 header:
Authorization: Required. Base 64 encoded string that contains the
client ID and client secret key. The field must have the format:
Authorization: Basic base64 encoded client_id:client_secret
您在请求 body 本身中拥有它。
所以你应该这样做:
import requests
import base64
authcode = 'valid_authcode_from_prev_authorization_step'
params = {'grant_type': 'authorization_code', 'code': authcode, 'redirect_uri': 'https://example.com/callback'}
client_id = 'example_id'
client_secret = 'example_secret'
b64_val = base64.b64encode("%s:%s" % (client_id, client_secret))
req = requests.post(
'https://accounts.spotify.com/api/token', params=params,
headers={'Authorization': b64_val})
但是,要使其正常工作,您需要一个有效的授权代码,您只能通过让用户在令牌获取步骤之前完成授权步骤来获得该代码。
此代码会发送到您在应用程序设置中注册的回叫,如果您设置了假回叫,则此代码将不起作用(即:http://example.com/callback ).
当尝试使用 python 3 授权 spotify 时,我得到一个 "server_error" 和描述 "Unexpected status: 400".
我使用了正确的授权码,Spotify 文档 (https://developer.spotify.com/web-api/authorization-guide/) 指示我使用带有这些参数的 post 命令。
我在这方面很菜鸟,我不知道自己做错了什么。
代码如下:
import requests
params = {'grant_type': 'authorization_code', 'code': authcode, 'redirect_uri': 'https://example.com/callback','client_id':'example', 'client_secret':'example'}
req=requests.post('https://accounts.spotify.com/api/token', params=params)
print(req.content)
根据 spotify 自己的指南(参见步骤 #4):
https://developer.spotify.com/web-api/authorization-guide/
请求新令牌的授权信息必须通过 "Authorization" 变量进入 header:
Authorization: Required. Base 64 encoded string that contains the client ID and client secret key. The field must have the format: Authorization: Basic base64 encoded client_id:client_secret
您在请求 body 本身中拥有它。
所以你应该这样做:
import requests
import base64
authcode = 'valid_authcode_from_prev_authorization_step'
params = {'grant_type': 'authorization_code', 'code': authcode, 'redirect_uri': 'https://example.com/callback'}
client_id = 'example_id'
client_secret = 'example_secret'
b64_val = base64.b64encode("%s:%s" % (client_id, client_secret))
req = requests.post(
'https://accounts.spotify.com/api/token', params=params,
headers={'Authorization': b64_val})
但是,要使其正常工作,您需要一个有效的授权代码,您只能通过让用户在令牌获取步骤之前完成授权步骤来获得该代码。
此代码会发送到您在应用程序设置中注册的回叫,如果您设置了假回叫,则此代码将不起作用(即:http://example.com/callback ).