【问题标题】:Spotify Client Credentials Flow using Python使用 Python 的 Spotify 客户端凭证流
【发布时间】:2022-11-09 00:48:37
【问题描述】:

我正在尝试使用 Spotify 的 Client Credentials Flow 和 Python 获取令牌,但是我只得到以下信息:

{“错误”:“无效客户端”,“错误描述”:“无效客户端”}

我正在关注本指南 - https://developer.spotify.com/documentation/general/guides/authorization/client-credentials/

使用此示例脚本作为起点 - https://www.w3schools.com/python/showpython.asp?filename=demo_requests_post_headers

这是我的代码(我更改了包含客户端 ID 和客户端密钥的 Base 64 编码字符串):

import requests

url = 'https://accounts.spotify.com/api/token'
myobj = {'grant_type': 'client_credentials'}

#use the 'headers' parameter to set the HTTP headers:
x = requests.post(url, data = myobj, headers = {"Authorization": "Basic Base64EncodedStringHere==","Content-Type": "application/x-www-form-urlencoded"})

print(x.text)

如果我将最后一行更改为 print(x),我将得到:<Response [400]>

显然我做错了什么,但我不知道是什么?

【问题讨论】:

    标签: python spotify


    【解决方案1】:

    我在学习教程时遇到了同样的问题,但我设法在 Spotify 社区上找到了解决方案。

    导入请求

    导入base64

    client_id = "你的客户ID在这里" client_secret = "你的客户端密码"

    编码 = base64.b64encode((client_id + ":" + client_secret).encode("ascii")).decode("ascii")

    标题 = { “内容类型”:“应用程序/x-www-form-urlencoded”, “授权”:“基本”+编码 }

    有效载荷 = { “grant_type”:“client_credentials” }

    response = requests.post("https://accounts.spotify.com/api/token", data=payload, headers=headers) 打印(响应。文本)

    【讨论】:

      猜你喜欢
      • 2014-11-12
      • 2021-01-10
      • 1970-01-01
      • 2018-12-14
      • 2023-02-19
      • 1970-01-01
      • 1970-01-01
      • 2014-11-20
      • 2021-10-19
      相关资源
      最近更新 更多