【问题标题】:Spotipy: How to pass the auth token to the clientSpotipy:如何将身份验证令牌传递给客户端
【发布时间】:2019-06-16 04:54:21
【问题描述】:

我正在尝试使用 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()

发送网址给用户。 在用户说她/他已经给予许可后: 验证码从网络服务器获取并用于生成令牌。

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

看起来我没有正确地将令牌传递给 spotipy 客户端。例如self.sp = spotipy.Spotify(auth="random_bullshit") 给了我同样的例外。我也尝试像auth=self.auth_token['access_token'] 这样传递令牌,结果相同。文档没有说明 auth 参数应该是什么,我也不太了解source code。但我想说这表明auth=self.auth_token['access_token'] 是正确的做法。

谢谢!

【问题讨论】:

    标签: python python-3.x oauth spotify spotipy


    【解决方案1】:

    正如我在上次编辑中已经建议的那样,auth=self.auth_token['access_token'] 是正确的,我只是在其中打错了字。无论如何,由于文档没有太多关于 auth 参数的说明,这可能会对某些人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-13
      • 2011-08-16
      • 1970-01-01
      • 2021-05-18
      • 2012-09-22
      • 1970-01-01
      • 2015-02-24
      • 1970-01-01
      相关资源
      最近更新 更多