Spotify在Python 3中刷新令牌3
我正在尝试获得刷新令牌,并在临时访问令牌到期时使用它来请求新的访问令牌。 Spotify的文档指出响应主体应包含以下JSON数据:
{
"access_token": "NgCXRK...MzYjw",
"token_type": "Bearer",
"scope": "user-read-private user-read-email",
"expires_in": 3600,
"refresh_token": "NgAagA...Um_SHo"
}
但是,在下面使用我的代码仅提供以下内容:
{
"access_token": "xxxx....",
"token_type": "Bearer",
"expires_in": 3600
}
我使用以下功能请求访问令牌:
def get_access_token(client_id, client_secret): # getting access token from spotify using client ID
# Encoding Client ID and Client Secret in Base64
token_url = "https://accounts.spotify.com/api/token"
client_creds = f"{client_id}:{client_secret}"
client_creds_b64 = base64.b64encode(client_creds.encode())
token_data = {
"grant_type": "client_credentials"
}
token_headers = {
'Authorization': f"Basic {client_creds_b64.decode()}"
}
r = requests.post(token_url, data=token_data, headers=token_headers)
valid_request = r.status_code in range(200, 299)
token_response_data = r.json()
# pretty print response body
print(json.dumps(token_response_data, indent=2)) # response 200 -> successful attempt
if valid_request:
access_token = token_response_data['access_token']
return access_token
我不确定为什么响应中缺少范围和刷新令牌身体。如何更改此功能以包含这些功能?
预先感谢您的时间!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更改令牌数据修复了以下操作:
您需要在Spotify上为Devs设置重定向URI,然后对其进行编码以将其用作参数。
要将“代码”参数用数据填充此URL,您将被重定向到重定向URI。
https://accounts.spotify.com/authorize?client_id= your客户端ID& wendesp_type = code& redirect_uri =您编码的redirect uri&scope = playlist-modify-publist-modify-public-public%20playlist-modify-privatifify-code
>>然后,代码将在搜索栏中复制/粘贴。它应该看起来像这样:
https:// redirecturi?code = xxx .....
以下是一个很好的视频指南:
https://www.youtube.com/watch?v=-FsFT6OwE1A&ab_channel=EuanMorgan
Altering the token data fixes this:
You need to set up a redirect uri on Spotify for devs, then encode it to use it as a parameter.
To get the 'code' parameter fill-out this URL with your data and you will be redirected to your redirect uri.
https://accounts.spotify.com/authorize?client_id=YOUR CLIENT ID&response_type=code&redirect_uri=YOUR ENCODED REDIRECT URI&scope=playlist-modify-public%20playlist-modify-private
The code will then be in the search bar to copy/paste. It should look something like this:
https://your redirectURI?code=xxx.....
Here is a good video guide for reference:
https://www.youtube.com/watch?v=-FsFT6OwE1A&ab_channel=EuanMorgan