【发布时间】:2021-10-22 17:04:17
【问题描述】:
我想创建一个应用程序,它将某些类型的歌曲添加到 Spotify 播放列表中。因此,使用 Spotipy(Python 库)来解决它可能是一个好主意。
要将歌曲添加到播放列表,我需要知道它的 URI。要知道它的 URI,我应该使用名为 search (https://spotipy.readthedocs.io/en/2.19.0/#spotipy.client.Spotify.search) 的方法。它需要一些输入,其中之一是 Authorization,它需要访问令牌(文档链接https://developer.spotify.com/documentation/web-api/reference/#category-search)。然后我使用 Spotify 的授权指南进行授权(AuthGuide https://developer.spotify.com/documentation/general/guides/authorization-guide/#authorization-code-flow 的链接),然后我堆叠了。
第一步很简单:
self.query = {
'client_id': client_id,
'response_type': 'code',
'redirect_uri': 'andriime.github.com/pc/'
}
self.info_code = requests.get(url=SPOTIFY_URL, params=self.query)
self.authorization_code = self.info_code.text
但是第二步真的很吓人。我在整个互联网上搜索它的解决方案,没有任何一个正常的指南如何通过它。但这是我发现的最好的:
Spotify API {'error': 'invalid_client'} Authorization Code Flow [400]
拜托,如果你知道如何处理它或者有一个答案,我错过了就写在这里!
编辑
如果我打印 self.info_code 响应代码是 200。client_id 和 client_secret 是环境变量,我在上面定义了它们(我没有发布此代码)。我的第 2 步代码如下所示:
self.authorization_code = self.info_code.text
code_payload = {
'grant_type': 'authorization_code',
'code': self.authorization_code,
'redirect_uri': redirect_uri,
}
auth_str = '{}:{}'.format(client_id, client_secret)
b64_auth_str = base64.urlsafe_b64encode(auth_str.encode()).decode()
print(b64_auth_str)
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic {}'.format(b64_auth_str)
}
post_request = requests.post(url=API_URL, data=code_payload, headers=headers)
print(post_request.text)
响应文本为 {"error":"invalid_grant","error_description":"Invalid authentication code"}。 StackOverflow 上有一个答案,但它对我不起作用(How to fix 'error: invalid_grant Invalid authorization code' when asking for reshresh_token from Spotify API?),因为我没有犯那个写在那里的错误。
在 Spotify 社区论坛上也有答案,但那里没有答案。另一个论坛有一个问题,但也没有答案
链接:
2.https://developer.salesforce.com/forums/?id=906F00000009B2AIAU)
因此,如果您知道它应该如何工作,请再次要求您回答。非常感谢!
【问题讨论】:
-
您好,我认为这个问题有点像stackoverflow.com/questions/54313030/… 的重复,请先查看那个问题,然后用您不理解的任何内容编辑您的问题。
-
@EricTaurone 感谢您的回复!我会尝试这种方法,但我需要一些时间才能了解如何正确使用它
-
@EricTaurone 感谢您的反馈,但这个答案并不是我所需要的。现在我设法得到了正确的答案,但谢谢你,那篇文章也对我有帮助)