spotipy-dev / spotipy

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

Error Only valid bearer authentication supported, reason: None?? #687

Open phantom2152 opened 3 years ago

phantom2152 commented 3 years ago

Hey I was getting info about a single track while I got this error

my code is

from spotipy import oauth2,spotify
re=oauth2.SpotifyClientCredentials(client_id=id,client_secret=sec).get_access_token()
spo = Spotify(re)
url = spo.track("https://open.spotify.com/track/3fHNjvF6AU1LXVez0wnFvw")
print(url)

And I am getting following error

HTTP Error for GET to https://api.spotify.com/v1/tracks/3fHNjvF6AU1LXVez0wnFvw returned 400 due to Only valid bearer authentication supported
Traceback (most recent call last):
  File "/home/anishgowda/.local/lib/python3.8/site-packages/spotipy/client.py", line 245, in _internal_call
    response.raise_for_status()
  File "/home/anishgowda/.local/lib/python3.8/site-packages/requests/models.py", line 943, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://api.spotify.com/v1/tracks/3fHNjvF6AU1LXVez0wnFvw

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/anishgowda/.local/lib/python3.8/site-packages/spotipy/client.py", line 339, in track
    return self._get("tracks/" + trid, market=market)
  File "/home/anishgowda/.local/lib/python3.8/site-packages/spotipy/client.py", line 291, in _get
    return self._internal_call("GET", url, payload, kwargs)
  File "/home/anishgowda/.local/lib/python3.8/site-packages/spotipy/client.py", line 261, in _internal_call
    raise SpotifyException(
spotipy.exceptions.SpotifyException: http status: 400, code:-1 - https://api.spotify.com/v1/tracks/3fHNjvF6AU1LXVez0wnFvw:
 Only valid bearer authentication supported, reason: None
Peter-Schorn commented 3 years ago

Read the documentation: https://spotipy.readthedocs.io/en/2.18.0/#client-credentials-flow

spyvanilla commented 2 years ago

The .get_access_token() method returns a JSON object, to get the acess_token inside the object, you have to do this:

spo = Spotify(auth=re['access_token'])
Peter-Schorn commented 2 years ago
spo = Spotify(auth=re['access_token'])

While this works, it is not recommended because the access token will not be automatically refreshed when it expires after an hour. As shown in the README, this is how you should initialize Spotify:

sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id="YOUR_APP_CLIENT_ID",
                                               client_secret="YOUR_APP_CLIENT_SECRET",
                                               redirect_uri="YOUR_APP_REDIRECT_URI",
                                               scope="user-library-read"))
stephanebruckert commented 2 years ago

Elements of answer provided here https://github.com/plamere/spotipy/issues/828#issuecomment-1156641256