spotipy-dev / spotipy

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

HTTPError: 400 Client Error: Bad Request for url: https://accounts.spotify.com/api/token #723

Closed Eyfelix closed 3 years ago

Eyfelix commented 3 years ago

Hello, I created a serveur, put my python script on it, I can run it using Putty but when I enter the url where I was redirect to I got this error message :

Traceback (most recent call last):
  File "/home/felixdbtz/.local/lib/python3.8/site-packages/spotipy/oauth2.py", line 559, in get_access_token
    response.raise_for_status()
  File "/home/felixdbtz/.local/lib/python3.8/site-packages/requests/models.py", line 953, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://accounts.spotify.com/api/token

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "www/Get url/getUrl.py", line 16, in <module>
    historique=sp.current_user_recently_played(limit=50,after=None,before=None) #!THIS LINE BUG
  File "/home/felixdbtz/.local/lib/python3.8/site-packages/spotipy/client.py", line 1444, in current_user_recently_played
    return self._get(
  File "/home/felixdbtz/.local/lib/python3.8/site-packages/spotipy/client.py", line 297, in _get
    return self._internal_call("GET", url, payload, kwargs)
  File "/home/felixdbtz/.local/lib/python3.8/site-packages/spotipy/client.py", line 221, in _internal_call
    headers = self._auth_headers()
  File "/home/felixdbtz/.local/lib/python3.8/site-packages/spotipy/client.py", line 212, in _auth_headers
    token = self.auth_manager.get_access_token(as_dict=False)
  File "/home/felixdbtz/.local/lib/python3.8/site-packages/spotipy/oauth2.py", line 565, in get_access_token
    self._handle_oauth_error(http_error)
  File "/home/felixdbtz/.local/lib/python3.8/site-packages/spotipy/oauth2.py", line 146, in _handle_oauth_error
    raise SpotifyOauthError(
spotipy.oauth2.SpotifyOauthError: error: invalid_request, error_description: code must be supplied

I cant find anything about a similar error on Spotipy and on my computer the code work fine.

my code :

import spotipy
from spotipy.oauth2 import SpotifyOAuth
import os
from  pprint import pprint

##define os variables
os.environ["SPOTIPY_CLIENT_ID"]="client id"
os.environ["SPOTIPY_CLIENT_SECRET"]="client secret"
os.environ["SPOTIPY_REDIRECT_URI"]="localhost:8080"

scope="user-read-recently-played"

sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope,open_browser=False))

historique=sp.current_user_recently_played(limit=50,after=None,before=None) #!THIS LINE BUG
pprint(historique)

Thanks for your help.

stephanebruckert commented 3 years ago

What does the URL you are redirected to look like? (please replace or fake anything that looks like a "code")

Eyfelix commented 3 years ago

the URL is :
https://accounts.spotify.com/authorize?client_id=e9be6a4390624312b517226cf90d790d&response_type=code&redirect_uri=http%3A%2F%2Ffelixdbtz.alwaysdata.net%2F&scope=user-read-recently-played

And I dont understand he problem with my code, it's just a simplified version of my real code.

Peter-Schorn commented 3 years ago

You don't directly paste this URL back into the program. (What would be the point of that?) You open this URL in the browser, log in to your Spotify account, and click agree. Then paste the URL that you were redirected to back into the program. The URL that you are redirected to should begin with your redirect URI.

Eyfelix commented 3 years ago

Thanks so much it worked.