Spotipy 尝试使用 oauth 进行验证

Spotipy trying to verify with oauth

我一直在尝试用spotipy验证制作播放列表等

import spotipy
import spotipy.util as util

class buildPlayList():
    def __init__(self,name):
        scope = 'playlist-modify-public'
        self.username=name
        token = util.prompt_for_user_token(self.username, scope,client_id='', client_secret='',redirect_uri='http://example.com/callback/')
        self.sp = spotipy.Spotify(auth=token)
        print self.sp.current_user()

    def make_and_build(self):
        pass
x=buildPlayList("myname")

我运行它然后我得到这个:

 User authentication requires interaction with your
 web browser. Once you enter your credentials and
 give authorization, you will be redirected to
 a url.  Paste that url you were directed to to
 complete the authorization.

我转到 URL,这是一个错误,我 post 它回到命令行,但它出错了。我不知道为什么这个库会这样做,所有其他库通常会做用户名、密码、客户端 ID 和客户端密码。出于某种原因,这会让你多走一步。一些帮助将不胜感激。我正在使用所有正确的信息。我不确定这个重定向 url 是什么东西,也许这是导致问题的原因?

按照此 link 获取您的客户端 ID、密码和回调 uri。然后确保在 prompt_for_user_token 调用

中正确设置这些参数

This one makes you go an extra step for some reason

额外步骤的原因是您使用 url 获得的响应包括代码和状态变量,这些变量又用于获取访问令牌。然后你最终将 Spotify Web API 与这些标记一起使用。如果你查看 official authorization guide

,你会更好地理解这个故事