简体   繁体   中英

Connecting to the spotify API with spotipy

so I'm curious if anyone has ever done this before and can give me a code snippet. Connecting to this is so troublesome.

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)

I then run my code as followed

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:

I go to the url and its an error page, I have no idea how to get past this. Ussually when connecting to API in the past you just need your name, pw and the keys. Can someone give me some tips to just connect to the api. I know what to do after that.

An old question, but just to say that the copy/paste of the URL is working for me in Spotify 2.4.4.

From the spotipy code, you can tell that it simply parses the url and extracts the code from it. (See here: https://github.com/plamere/spotipy/blob/4c2c1d763a3653aa225c4af848409ec31286a6bf/spotipy/oauth2.py#L182 )

You need to have all of the parameters bellow set on your script (or in your class):

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 is crucial here. if http://localhost:8000 is not set, or with a single '/' misplaced, it will generate a connection error.

then pass them:

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

when run, and if all your credentials above are correctly set, python spotifybot.py will open a new tab on your browser.

go to this browser tab, copy the url address from it and past it inside terminal.

Enter the URL you were redirected to:

this should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM