mirror of
https://github.com/c0de-archive/spotipy.git
synced 2025-01-07 23:22:49 +00:00
Fixed refresh token problem
This commit is contained in:
parent
1813b2502e
commit
9a3b5784b0
@ -1,9 +1,11 @@
|
|||||||
|
from __future__ import print_function
|
||||||
import base64
|
import base64
|
||||||
import urllib
|
import urllib
|
||||||
import requests
|
import requests
|
||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
|
import sys
|
||||||
|
|
||||||
class SpotifyOauthError(Exception):
|
class SpotifyOauthError(Exception):
|
||||||
pass
|
pass
|
||||||
@ -49,7 +51,7 @@ class SpotifyOAuth(object):
|
|||||||
def save_token_info(self, token_info):
|
def save_token_info(self, token_info):
|
||||||
if self.cache_path:
|
if self.cache_path:
|
||||||
f = open(self.cache_path, 'w')
|
f = open(self.cache_path, 'w')
|
||||||
print >>f, json.dumps(token_info)
|
f.write(json.dumps(token_info))
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
def is_token_expired(self, token_info):
|
def is_token_expired(self, token_info):
|
||||||
@ -111,10 +113,16 @@ class SpotifyOAuth(object):
|
|||||||
|
|
||||||
auth_header = base64.b64encode(self.client_id + ':' + self.client_secret)
|
auth_header = base64.b64encode(self.client_id + ':' + self.client_secret)
|
||||||
headers = {'Authorization': 'Basic %s' % auth_header}
|
headers = {'Authorization': 'Basic %s' % auth_header}
|
||||||
|
|
||||||
response = requests.post(self.OAUTH_TOKEN_URL, data=payload,
|
response = requests.post(self.OAUTH_TOKEN_URL, data=payload,
|
||||||
headers=headers, verify=True)
|
headers=headers)
|
||||||
if response.status_code is not 200:
|
if response.status_code != 200:
|
||||||
raise SpotifyOauthError(response.reason)
|
if False: # debugging code
|
||||||
|
print('headers', headers)
|
||||||
|
print('request', response.url)
|
||||||
|
self._warn("couldn't refresh token: code:%d reason:%s" \
|
||||||
|
% (response.status_code, response.reason))
|
||||||
|
return None
|
||||||
token_info = response.json()
|
token_info = response.json()
|
||||||
token_info = self._add_custom_values_to_token_info(token_info)
|
token_info = self._add_custom_values_to_token_info(token_info)
|
||||||
if not 'refresh_token' in token_info:
|
if not 'refresh_token' in token_info:
|
||||||
@ -131,3 +139,6 @@ class SpotifyOAuth(object):
|
|||||||
token_info['scope'] = self.scope
|
token_info['scope'] = self.scope
|
||||||
return token_info
|
return token_info
|
||||||
|
|
||||||
|
def _warn(self, msg):
|
||||||
|
print('warning:' + msg, file=sys.stderr)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user