使用spotipy连接到spotify API

问题描述 投票:1回答:2

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

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。我知道在那之后该怎么做。

python spotify
2个回答
0
投票

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

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


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将在您的浏览器上打开一个新选项卡。

转到此浏览器选项卡,从中复制URL地址并在终端内将其移过。

Enter the URL you were redirected to:

这应该工作。

© www.soinside.com 2019 - 2024. All rights reserved.