Spotipy 当前用户不工作 AttributeError:“str”对象没有属性“get_access_token”

Posted

技术标签:

【中文标题】Spotipy 当前用户不工作 AttributeError:“str”对象没有属性“get_access_token”【英文标题】:Spotipy Current user not working AttributeError: 'str' object has no attribute 'get_access_token' 【发布时间】:2022-01-14 09:23:36 【问题描述】:

我正在尝试使用 python 为 spotify 创建一个音乐机器人,但没有任何效果。这是我的代码:

    scope = 'user-read-private'
myClientId = "[ur id]"
mySecret = "[ur secret]"
myRedirect="http://google.de/"
myUsername="[ur name]"

token = spotipy.prompt_for_user_token(myUsername,scope, myClientId, mySecret, myRedirect)

sp = spotipy.Spotify(auth_manager=token)
sp.current_user()

这是控制台中的完整错误:

  File "C:\Users\a\Downloads\spotify2.py", line 18, in <module>
    sp.current_user()
  File "C:\Users\a\AppData\Local\Programs\Python\Python39\lib\site-packages\spotipy\client.py", line 1173, in current_user
    return self.me()
  File "C:\Users\a\AppData\Local\Programs\Python\Python39\lib\site-packages\spotipy\client.py", line 1167, in me
    return self._get("me/")
  File "C:\Users\a\AppData\Local\Programs\Python\Python39\lib\site-packages\spotipy\client.py", line 297, in _get
    return self._internal_call("GET", url, payload, kwargs)
  File "C:\Users\a\AppData\Local\Programs\Python\Python39\lib\site-packages\spotipy\client.py", line 221, in _internal_call
    headers = self._auth_headers()
  File "C:\Users\a\AppData\Local\Programs\Python\Python39\lib\site-packages\spotipy\client.py", line 212, in _auth_headers
    token = self.auth_manager.get_access_token(as_dict=False)
AttributeError: 'str' object has no attribute 'get_access_token'

【问题讨论】:

【参考方案1】:

根据documentation,要使用令牌初始化 Spotipy 对象,您需要这样初始化它:

sp = spotipy.Spotify(auth=token)

sp = spotipy.Spotify(token)

而不是将其作为auth_manager 参数传递。

【讨论】:

以上是关于Spotipy 当前用户不工作 AttributeError:“str”对象没有属性“get_access_token”的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Spotipy 获取 Spotify 的曲目时长

使用 Spotipy 获取当前正在播放的歌曲会出现错误:“'Spotify' 对象没有属性 'currently_playing'”

Spotipy Oauth2 流存储令牌

spotipy auth 工作流在浏览器中导致 error=state_mismatch

尝试在 spotipy 中获取访问令牌时授权码无效

获得使用 Python3 的 spotipy 修改 Spotify 播放列表的授权