简体   繁体   中英

Bad Request at Authorization Code Flow Spotify

I am trying to built a script that creates a playlist on a user's spotify profile. To learn spotipy I decided to try the examples they have on the documentation page.

The code I run is:

import sys
import spotipy
import spotipy.util as util

token = util.prompt_for_user_token('idxxx',
                           'user-library-read',
                           client_id='axxx',
                           client_secret='Bxxx',
                           redirect_uri='http://localhost')

scope = 'user-library-read'

if len(sys.argv) > 1:
    username = sys.argv[1]
else:
    print("Usage: %s username" % (sys.argv[0],))
    sys.exit()

token = util.prompt_for_user_token(username, scope)

if token:
    sp = spotipy.Spotify(auth=token)
    results = sp.current_user_saved_tracks()
    for item in results['items']:
        track = item['track']
        print(track['name'] + ' - ' + track['artists'][0]['name'])
else:
    print("Can't get token for", username)

The problem occurs when i run the code. I get redirected on my redirect uri and after i paste it back on the terminal i get this:

Traceback (most recent call last):
  File "spot01.py", line 9, in <module>
    redirect_uri='http://localhost')
  File "/home/user/.local/lib/python3.6/site-packages/spotipy/util.py", line 92, in prompt_for_user_token
    token = sp_oauth.get_access_token(code, as_dict=False)
  File "/home/user/.local/lib/python3.6/site-packages/spotipy/oauth2.py", line 434, in get_access_token
    raise SpotifyOauthError(response.reason)
spotipy.oauth2.SpotifyOauthError: Bad Request

I tried to access the oauth2.py from the File Manager and the terminal but it says that this repository does not exist. Also i tried to install spotipy through the github page they have where the neccessary files exist but still nothing.

Any ideas? Thanks a lot.

I solved the problem by downloading the required files from here https://github.com/plamere/spotipy/tree/master/spotipy

Then I changed some things inside of each.py file and i run every code I wanted to inside there. It must be another more fancy solution but this one worked for me.

Oh before running any command I typed this set of commands on the terminal:

$ bash
$ export SPOTIPY_CLIENT_ID='xxx'
$ export SPOTIPY_CLIENT_SECRET='xxx'
$ export SPOTIPY_REDIRECT_URI='http://localhost/'

where xxx you put your credentials and at the refirect uri your localhost or your github profile link.

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