如何使用python获取spotify Oauth令牌?

ej83mcc0  于 2022-10-22  发布在  Python
关注(0)|答案(1)|浏览(208)

如何使用Python Requests库创建和发送授权请求以从Spotify获取访问令牌?
spotify网站上的示例仅在js中
https://developer.spotify.com/documentation/general/guides/authorization/client-credentials/

9gm1akwq

9gm1akwq1#

client_idclient_secret值更改为您的登录数据,这样就可以了。

import base64
import requests

client_id = 'CLIENT_ID'
client_secret = 'CLIENT_SECRET'
base64_bytes = base64.b64encode(f"{client_id} + ':' + {client_secret}".encode("ascii"))
base64_string = base64_bytes.decode("ascii")

authOptions = {
  'url': 'https://accounts.spotify.com/api/token',
  'headers': {
    'Authorization': 'Basic ' + base64_string,
    'form': {
        'grant_type': 'client_credentials'},
    'json': True}
}

r = requests.post(url = authOptions['url'], data = authOptions['headers'])
assert r.status_code == 200, f"Something went wrong. Status code: {r.status_code}"
token = r.access_token

相关问题