spotipy-dev / spotipy

A light weight Python library for the Spotify Web API
http://spotipy.readthedocs.org
MIT License
5.01k stars 957 forks source link

400 - Bad Request on oauth2.get_access_token #494

Open CAAAR12 opened 4 years ago

CAAAR12 commented 4 years ago

Hello, I have the latest release (2.12.0). I was running the example to list tracks and artist name just to pass client authentication variables (ID, secret) and redirect_uri as strings directly from the code instead of my environment variables as a quick test. However when I run, I get the traceback shown at the bottom. I can sign into Spotify and get redirected to the uri normally (authentication successful), but I can't seem to get the post successful for the access token from the function get_access_token.

Any ideas what I'm doing wrong or if it's a bug? Help would be appreciated! Thank you for providing this!!!!

(I used some print lines to verify some of the payload inputs, which is why it point to line 445. On the stock, it should show line 436.)

Traceback (most recent call last):
  File "c:/Users/crisr/OneDrive/Documents/Python Codes/Spotify Player/Spotify_Test.py", line 17, in <module>
    token = util.prompt_for_user_token(username, scope, client_id,client_secret,redirect_uri)
  File "C:\Users\crisr\AppData\Local\Programs\Python\Python38-32\lib\site-packages\spotipy\util.py", line 92, in prompt_for_user_token   
    token = sp_oauth.get_access_token(code, as_dict=False)
  File "C:\Users\crisr\AppData\Local\Programs\Python\Python38-32\lib\site-packages\spotipy\oauth2.py", line 445, in get_access_token     
    raise SpotifyOauthError(response.reason)
spotipy.oauth2.SpotifyOauthError: Bad Request
PS C:\Users\crisr\OneDrive\Documents\Python Codes\Spotify Player> 

My Code:

import sys
import spotipy
import spotipy.util as util

scope = 'user-library-read'

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

username = 'xxxxxx'
client_id = 'xxxxxxxxxxx' #Vinyl Player 2
client_secret = 'xxxxxxxxxxxx'
redirect_uri = 'http://localhost:8001'
token = util.prompt_for_user_token(username, scope, client_id,client_secret,redirect_uri)

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)
stephanebruckert commented 4 years ago

Hey @CAAAR12,

This works for me but I'm not using Windows. It looks like a bunch of issues are being discovered on Windows only recently. Any chance you can help find which version of spotipy was fine for your problem?

Please follow this https://github.com/plamere/spotipy/issues/493#issuecomment-631103985, looking forward to your help on this

CAAAR12 commented 4 years ago

Hey @CAAAR12, This works for me but I'm not using Windows. It looks like a bunch of issues are being discovered on Windows only recently. Any chance you can help find which version of spotipy was fine for your problem? Please follow this #493 (comment), looking forward to your help on this

Thanks @stephanebruckert for the quick response. I just downgraded to 2.11.2 per your recommendation (I tried 2.11.0 first but it defaulted install 2.11.2) and its working great now. As for the solution to why , I haven't seen the changes from 2.11 to 2.12 yet. I'm curious to know the solution. Many thanks!!!

stephanebruckert commented 4 years ago

This is very strange, I see nothing from the changes from 2.11.2 to 2.12.0 that could explain that problem. Can you try 2.12.0 again?

CAAAR12 commented 4 years ago

Interesting.... uninstalled 2.11.2 and installed 2.12.0 and seems to work again still... Last night I uninstalled and reinstalled 2.12.0 multiple times and it was failing every time :/

Maybe it has to do with some sort of cache being saved in 2.11.2? Unless uninstalling the package removes the caches associated as well. I'm not proficient enough sorry.

lcouto commented 4 years ago

Hi I'm running into a similar issue but when trying to get use an auth code to get a token. Please see code below spotipy version = 2.11.1

client_id = 'REDACTED'
client_secret = 'REDACTED'
redirect_uri = 'http://127.0.0.1:9091'
scope = 'user-read-recently-played'
username='REDACTED'
code = 'REDACTED'
sp_oauth = oauth2.SpotifyOAuth(client_id,client_secret,redirect_uri,scope,username)
print(sp_oauth.get_access_token(code, as_dict=False, check_cache=False))

returns a

/usr/local/lib/python3.7/site-packages/spotipy/oauth2.py in get_access_token(self, code, as_dict, check_cache)
    423         )
    424         if response.status_code != 200:
--> 425             raise SpotifyOauthError(response.reason)
    426         token_info = response.json()
    427         token_info = self._add_custom_values_to_token_info(token_info)

SpotifyOauthError: Bad Request

any help is greatly appreciated.