Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix user having to log in after token refresh #19

Merged
merged 2 commits into from
Aug 17, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions spotipy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import print_function

import sys
import base64
import requests
import json
Expand Down
16 changes: 12 additions & 4 deletions spotipy/oauth2.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def get_cached_token(self):
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
Expand Down Expand Up @@ -91,8 +91,7 @@ def get_access_token(self, code):
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

Expand All @@ -114,9 +113,18 @@ def refresh_access_token(self, refresh_token):
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