Errors On Second Spotify Refresh Token

9 hours ago 3
ARTICLE AD BOX

I'm running a simple personal test script to simply make some API requests to Spotify and I'm not understanding why I'm seeing errors.

My full code (a rough test script I have where I've hard coded the spotify redirect code and secrets to make track requests):

import requests, json, time from requests.auth import HTTPBasicAuth class TokenManager: current_refresh_token = "" def __init__(self): self.current_refresh_token = "" def get_first_spotify_token(self): headers = { "Content-Type": "application/x-www-form-urlencoded" } data = { "redirect_uri": "<callback>", "code": "<redirect-authorization-code>", "grant_type": "authorization_code" } token_request = requests.post( url="https://accounts.spotify.com/api/token", data=data, headers=headers, auth=HTTPBasicAuth("<client-id>", "<client-secret>") ) if (token_request.status_code == 200): token_request_json = json.loads(token_request.text) self.current_refresh_token = token_request_json["refresh_token"] print(token_request_json) return token_request_json else: print(token_request.status_code) print(token_request.text) def get_refresh_token(self): headers = { "Content-Type": "application/x-www-form-urlencoded" } data = { "client_id": "<client-id>", "client_secret": "<client-secret>", "refresh_token": self.current_refresh_token, "grant_type": "refresh_token" } token_request = requests.post( url="https://accounts.spotify.com/api/token", data=data, headers=headers ) if (token_request.status_code == 200): token_request_json = json.loads(token_request.text) print(token_request_json) self.current_refresh_token = token_request_json["access_token"] return token_request_json else: print(token_request.status_code) print(token_request.text) def get_tracks(): token_manager = TokenManager() token = token_manager.get_first_spotify_token() limit = 50 offset = 0 next = "" total = 1000000 # Super large int while (offset <= total) or next is not None: params = { "limit": limit, "offset": offset } headers = { "Authorization": "%s %s" % (token["token_type"], token["access_token"]) } tracks_request = requests.get( url="https://api.spotify.com/v1/me/tracks", headers=headers, params=params ) tracks_json_response = json.loads(tracks_request.text) offset += limit total = tracks_json_response["total"] next = tracks_json_response["next"] token = token_manager.get_refresh_token() get_tracks()

And this is the full output (the response from the first spotify token, then the refresh token request that works, and then the 400 and invalid response from the second refresh token request):

{'access_token': '<access-token>', 'token_type': 'Bearer', 'expires_in': 3600, 'refresh_token': '<refresh-token>', 'scope': '<scope>'} {'access_token': '<access-token>', 'token_type': 'Bearer', 'expires_in': 3600, 'scope': '<scope>'} 400 {"error":"invalid_grant","error_description":"Invalid refresh token"} Traceback (most recent call last): File "spotify_test.py", line 98, in <module> get_tracks() File "spotify_test.py", line 81, in get_tracks "Authorization": "%s %s" % (token["token_type"], token["access_token"]) TypeError: 'NoneType' object is not subscriptable

My question here is if where am I incorrectly implementing the refresh token flow incorrectly here?
Am I sending the refresh token request incorrectly? I presume I'd get a refresh token from my first refresh token request but I don't see one being returned (so that's why I used the access token).

My presumption would be following the Spotify documentation for refresh tokens that I would keep getting a refresh token for each refresh token request but I'm not seeing it so not sure how I should be packaging a refresh token.

Read Entire Article