Spotipy: How to pass the auth token to the client(Spotipy:如何将身份验证令牌传递给客户端)
本文介绍了Spotipy:如何将身份验证令牌传递给客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用spotipy python库构建一个应用程序来访问Spotify API。
我的OAuth代码如下所示,除了使用正确的auth参数初始化客户端之外,它似乎可以正常工作。
self.sp_auth=spotipy.oauth2.SpotifyOAuth(secrets.sp_auth_id,
secrets.sp_auth_pw, secrets.sp_callback_url,
scope="playlist-modify-public user-library-read", state=state)
...
url = self.sp_auth.get_authorize_url()
将url发送给用户。
在用户表示她/他已给予许可后:
从Web服务器获取授权码并用于生成令牌。
self.auth_token=self.sp_auth.get_access_token(self.auth_code)
self.auth_Token如下所示:
{'access_token' : 'BQD ... qE7K3PBZKB6iZFU3_4p',
'token_type' : 'Bearer',
'expires_in' : 3600,
'refresh_token' : 'AQCOS2Xo ... MK09ry7-a-fl61OwhuO1Q',
'scope' : 'playlist-modify-public user-library-read',
'expires_at' : 1548247835}
然后,我按如下方式初始化SPOTiPY客户端模块:
self.sp = spotipy.Spotify(auth=self.auth_token)
然后我尝试以下操作:
playlists = self.sp.current_user_playlists(limit=10)
这将引发此异常:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/spotipy/client.py", line 119, in _internal_call
r.raise_for_status()
File "/usr/local/lib/python3.6/dist-packages/requests/models.py", line 940, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://api.spotify.com/v1/me/playlists?limit=10&offset=0
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/telegram/ext/dispatcher.py", line 279, in process_update
handler.handle_update(update, self)
File "/usr/local/lib/python3.6/dist-packages/telegram/ext/callbackqueryhandler.py", line 143, in handle_update
return self.callback(dispatcher.bot, update, **optional_args)
File "spotify_playlist_bot_v2.py", line 140, in button_auth_done
User.data[user_id].msg_start(bot, update)
File "spotify_playlist_bot_v2.py", line 84, in msg_start
self.msg_choose_playlist()
File "spotify_playlist_bot_v2.py", line 90, in msg_choose_playlist
playlists = self.sp.current_user_playlists(limit=10)
File "/usr/local/lib/python3.6/dist-packages/spotipy/client.py", line 355, in current_user_playlists
return self._get("me/playlists", limit=limit, offset=offset)
File "/usr/local/lib/python3.6/dist-packages/spotipy/client.py", line 146, in _get
return self._internal_call('GET', url, payload, kwargs)
File "/usr/local/lib/python3.6/dist-packages/spotipy/client.py", line 124, in _internal_call
headers=r.headers)
spotipy.client.SpotifyException: http status: 400, code:-1 - https://api.spotify.com/v1/me/playlists?limit=10&offset=0:
Only valid bearer authentication supported
看起来我没有正确地将令牌传递给恶意客户端。例如,self.sp = spotipy.Spotify(auth="random_bullshit")给了我相同的例外。我还尝试像这样传递令牌auth=self.auth_token['access_token'],结果相同。文档并没有明确说明auth参数应该是什么,我也没有真正理解source code。但我要说的是,这表明auth=self.auth_token['access_token']是正确的。
谢谢!
推荐答案
正如我在上一次编辑中所建议的auth=self.auth_token['access_token']是正确的,我只是其中有一个打字错误。无论如何,由于文档中没有太多关于auth参数的内容,这可能会对某些人有所帮助。
这篇关于Spotipy:如何将身份验证令牌传递给客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
织梦狗教程
本文标题为:Spotipy:如何将身份验证令牌传递给客户端
基础教程推荐
猜你喜欢
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
