如何使用客户端ID和密码访问spotify的web api?

问题描述 投票:0回答:1

我正在尝试编写一个脚本,在python中的spotify帐户上创建一个播放列表,从头开始,不使用像spotipy这样的模块。

我的问题是如何使用请求模块使用我的客户端ID和客户端密钥进行身份验证,或使用这些凭据获取访问令牌?

python authorization spotify
1个回答
0
投票

由于引用了here,您必须将Bearer令牌提供给Authorization标头,并使用请求通过声明“headers”可选来完成:

r = requests.post(url="https://api.spotify.com/v1/users/{your-user}/playlists", 
                  headers={"Authorization": <token>, ...})

有关如何获得用户的持票人令牌的详细信息,请访问here


0
投票

尝试此完整的客户端凭据授权流程。

第一步 - 使用您的凭据获取授权令牌:

CLIENT_ID = " < your client id here... > "
CLIENT_SECRET = " < your client secret here... > "

grant_type = 'client_credentials'
body_params = {'grant_type' : grant_type}

url='https://accounts.spotify.com/api/token'
response = requests.post(url, data=body_params, auth = (CLIENT_ID, CLIENT_SECRET)) 

token_raw = json.loads(response.text)
token = token_raw["access_token"]

第二步 - 向任何播放列表端点发出请求。确保为<spotify_user>设置有效值。

headers = {"Authorization": "Bearer {}".format(token)}
r = requests.get(url="https://api.spotify.com/v1/users/<spotify_user>/playlists", headers=headers)
print(r.text)
© www.soinside.com 2019 - 2024. All rights reserved.