From a6f096e3dc191c730b9d9e8898bf56ef91840fe2 Mon Sep 17 00:00:00 2001 From: Matthew Duck Date: Wed, 30 Jul 2014 17:36:21 +0100 Subject: [PATCH 1/2] Add missing sys import --- spotipy/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/spotipy/__init__.py b/spotipy/__init__.py index c7e884d..6ee4921 100644 --- a/spotipy/__init__.py +++ b/spotipy/__init__.py @@ -2,6 +2,7 @@ from __future__ import print_function +import sys import base64 import requests import json From 72d13dd0a7b056b502fc20bf7f4cde4a8a762926 Mon Sep 17 00:00:00 2001 From: Matthew Duck Date: Mon, 4 Aug 2014 15:42:15 +0100 Subject: [PATCH 2/2] Fix user having to login after first token refresh Fix issue where the get_cached_token method returns None instead of the cached token object, meaning that users have to login again when the initial token expires. The problem has two aspects: - The first time the user calls get_cached_token after token expiration, the token is refreshed but the "new_token_info" object is not returned. - The second time the user calls get_cached_token after token expiration, the scope check fails, because the refresh_access_token method didn't include the "scope" key when it cached the new token_info results. --- spotipy/oauth2.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/spotipy/oauth2.py b/spotipy/oauth2.py index 3c01feb..97475df 100644 --- a/spotipy/oauth2.py +++ b/spotipy/oauth2.py @@ -39,7 +39,7 @@ class SpotifyOAuth(object): return None if self.is_token_expired(token_info): - new_token_info = self.refresh_access_token(token_info['refresh_token']) + token_info = self.refresh_access_token(token_info['refresh_token']) except IOError: pass @@ -91,8 +91,7 @@ class SpotifyOAuth(object): if response.status_code is not 200: raise SpotifyOauthError(response.reason) token_info = response.json() - token_info['expires_at'] = int(time.time()) + token_info['expires_in'] - token_info['scope'] = self.scope + token_info = self._add_custom_values_to_token_info(token_info) self.save_token_info(token_info) return token_info @@ -114,9 +113,18 @@ class SpotifyOAuth(object): if response.status_code is not 200: raise SpotifyOauthError(response.reason) token_info = response.json() - token_info['expires_at'] = int(time.time()) + token_info['expires_in'] + token_info = self._add_custom_values_to_token_info(token_info) if not 'refresh_token' in token_info: token_info['refresh_token'] = refresh_token self.save_token_info(token_info) return token_info + def _add_custom_values_to_token_info(self, token_info): + ''' + Store some values that aren't directly provided by a Web API + response. + ''' + token_info['expires_at'] = int(time.time()) + token_info['expires_in'] + token_info['scope'] = self.scope + return token_info +