使用spotipy连接到spotify API

时间:2015-10-09 03:09:03

标签: python spotify

所以我很好奇,如果有人曾经这样做过,并且可以给我一个代码片段。连接到这是非常麻烦的。

self.username=name
        token = spotipy.util.prompt_for_user_token(self.username,client_id=my_id, client_secret=my_secret)

        self.sp = spotipy.Spotify(auth=token)
然后我按照

运行我的代码
python spotifybot.py


            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.


Please navigate here: 


Enter the URL you were redirected to:

我转到网址及其错误页面,我不知道如何通过这个。在过去连接到API时,你只需要你的名字,密码和密钥。有人可以给我一些提示,只需连接到api。我知道在那之后该怎么做。

2 个答案:

答案 0 :(得分:0)

一个老问题,但只是说在Spotify 2.4.4中,URL的复制/粘贴对我有用。

从spotipy代码中,您可以告诉它只是解析url并从中提取代码。 (见这里:https://github.com/plamere/spotipy/blob/4c2c1d763a3653aa225c4af848409ec31286a6bf/spotipy/oauth2.py#L182

答案 1 :(得分:0)

您需要在脚本(或您的班级)中设置所有参数:

USERNAME = 'your_username' #your spotify username
CLIENT_ID = 'your_client_id'#set at your developer account
CLIENT_SECRET = 'your_client_secret' #set at your developer account
REDIRECT_URI = 'your_redirect_uri' #set at your developer account, usually "http://localhost:8000"
SCOPE = 'playlist-modify-public' # or else

PS。 REDIRECT_URI在这里至关重要。如果未设置http://localhost:8000或单个'/'放错位置,则会产生连接错误。

然后传递给他们:

token = util.prompt_for_user_token(username = USERNAME, 
                                   scope = SCOPE, 
                                   client_id = CLIENT_ID, 
                                   client_secret = CLIENT_SECRET, 
                                   redirect_uri = REDIRECT_URI)

if token:
   sp = spotipy.Spotify(auth=token)
   # dig your api
  

运行时,如果上面的所有凭据都已正确设置,python spotifybot.py将在您的浏览器上打开一个新标签。

     

转到此浏览器标签,从中复制网址并在终端内将其移过。

Enter the URL you were redirected to:

这应该有效。