我正在尝试编写一个脚本,在python中的spotify帐户上创建一个播放列表,从头开始,不使用像spotipy这样的模块。
我的问题是如何使用请求模块使用我的客户端ID和客户端密钥进行身份验证,或使用这些凭据获取访问令牌?
尝试此完整的客户端凭据授权流程。
第一步 - 使用您的凭据获取授权令牌:
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)