[GH-ISSUE #723] HTTPError: 400 Client Error: Bad Request for url: https://accounts.spotify.com/api/token #434

Closed
opened 2026-02-27 23:22:37 +03:00 by kerem · 4 comments
Owner

Originally created by @Eyfelix on GitHub (Aug 31, 2021).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/723

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.

Originally created by @Eyfelix on GitHub (Aug 31, 2021). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/723 <!--- Please make sure you've: - read the FAQ https://github.com/plamere/spotipy/blob/master/FAQ.md - read the documentation https://spotipy.readthedocs.io/en/latest/ - searched older issues If your question is about code, please share the code you are using ---> 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.
kerem 2026-02-27 23:22:37 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@stephanebruckert commented on GitHub (Aug 31, 2021):

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

<!-- gh-comment-id:909275344 --> @stephanebruckert commented on GitHub (Aug 31, 2021): What does the URL you are redirected to look like? (please replace or fake anything that looks like a "code")
Author
Owner

@Eyfelix commented on GitHub (Aug 31, 2021):

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.

<!-- gh-comment-id:909416860 --> @Eyfelix commented on GitHub (Aug 31, 2021): 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.
Author
Owner

@Peter-Schorn commented on GitHub (Aug 31, 2021):

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.

<!-- gh-comment-id:909459513 --> @Peter-Schorn commented on GitHub (Aug 31, 2021): 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.
Author
Owner

@Eyfelix commented on GitHub (Aug 31, 2021):

Thanks so much it worked.

<!-- gh-comment-id:909487395 --> @Eyfelix commented on GitHub (Aug 31, 2021): Thanks so much it worked.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotipy#434
No description provided.