Ronald Currier
Ronald Currier

Reputation: 585

Refreshing Spotify access_token

I'm trying to use the refresh_token to (obviously) refresh the Spotify access_token I have. The documentation gives this as an example curl statement:

curl -H "Authorization: Basic ZjM4Zj...Y0MzE=" -d grant_type=refresh_token -d refresh_token=NgAagA...NUm_SHo https://accounts.spotify.com/api/token

and here is my Python implemtation:

payload = { 'grant_type': 'refresh_token', 'refresh_token': <REFRESH_TOKEN> } r = requests.post('https://accounts.spotify.com/api/token', data=payload, headers={'Authorization': 'Basic <CLIENT_SECRET>'})

In both cases I get back {"error":"invalid_client"}

I've tried passing my client_id and the client_secret in the post data but I always get back invalid_client. Someone here on SO said I need to pass in code, but code is a single use, very short lived object that has already been consumed/expired.

Any thoughts to what I'm doing wrong?

Upvotes: 1

Views: 1473

Answers (2)

Haris Majeed
Haris Majeed

Reputation: 1

spot = SpotifyOAuth(<CLIENT_ID>, <CLIENT_SECRET>, <REDIRECT_URL>)
access_token = spot.refresh_access_token(<SPOTIFY_REFRESH>)
print(access_token)

This worked for me

https://spotipy.readthedocs.io/en/2.13.0/#spotipy.oauth2.SpotifyOAuth

Upvotes: 0

jooon
jooon

Reputation: 2031

The only problem I can see is that you write:

headers={'Authorization': 'Basic <CLIENT_SECRET>'}

when it, using your notation, should be:

headers={'Authorization': 'Basic ' + base64.b64encode('<CLIENT_ID>:<CLIENT_SECRET>')}

However, that could be simplifed further since you are using requests. As described here: http://docs.python-requests.org/en/latest/user/authentication/ Just remove headers and replace it with auth instead:

auth=('<CLIENT_ID>', '<CLIENT_SECRET>')

However, I guess that this isn't what is actually wrong in your code either, since you somehow managed to get a refresh token which requires that you authorized successfully once before.

Upvotes: 1

Related Questions