mirror of
https://github.com/spotipy-dev/spotipy.git
synced 2026-06-19 09:13:53 +00:00
Temporarily revert #428
This commit is contained in:
parent
bc7d49ca50
commit
26db832e24
@ -6,10 +6,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
### Changed
|
|
||||||
|
|
||||||
- Auto-Refresh token in long-running apps when using Authorization Code Flow
|
|
||||||
|
|
||||||
## [2.7.1] - 2020-01-20
|
## [2.7.1] - 2020-01-20
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|||||||
@ -1,42 +0,0 @@
|
|||||||
# long running example (need to be authenticated via oauth)
|
|
||||||
|
|
||||||
import sys
|
|
||||||
import time
|
|
||||||
from datetime import datetime
|
|
||||||
|
|
||||||
import spotipy
|
|
||||||
import spotipy.util as util
|
|
||||||
|
|
||||||
if len(sys.argv) > 1:
|
|
||||||
username = sys.argv[1]
|
|
||||||
else:
|
|
||||||
print("Whoops, need your username!")
|
|
||||||
print("usage: python user_playlists.py [username]")
|
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
SCOPE = ','.join([
|
|
||||||
'user-modify-playback-state',
|
|
||||||
'user-read-playback-state',
|
|
||||||
'user-read-currently-playing'
|
|
||||||
])
|
|
||||||
|
|
||||||
oauth = util.prompt_for_user_token(
|
|
||||||
username,
|
|
||||||
redirect_uri="http://localhost/",
|
|
||||||
scope=SCOPE)
|
|
||||||
|
|
||||||
if oauth:
|
|
||||||
sp = spotipy.Spotify(auth=oauth)
|
|
||||||
# sp.trace = True
|
|
||||||
# sp.trace_out = True
|
|
||||||
start = datetime.now()
|
|
||||||
while True:
|
|
||||||
current_playback = sp.current_playback()
|
|
||||||
print(datetime.now() - start,
|
|
||||||
"is_playing?",
|
|
||||||
None if current_playback is None
|
|
||||||
else current_playback['is_playing'])
|
|
||||||
|
|
||||||
time.sleep(60)
|
|
||||||
else:
|
|
||||||
print("Can't get token for", username)
|
|
||||||
@ -63,7 +63,7 @@ class Spotify(object):
|
|||||||
"""
|
"""
|
||||||
Creates a Spotify API client.
|
Creates a Spotify API client.
|
||||||
|
|
||||||
:param auth: An authenticated OAuth2 Instance
|
:param auth: An authorization token (optional)
|
||||||
:param requests_session:
|
:param requests_session:
|
||||||
A Requests session object or a truthy value to create one.
|
A Requests session object or a truthy value to create one.
|
||||||
A falsy value disables sessions.
|
A falsy value disables sessions.
|
||||||
@ -94,9 +94,7 @@ class Spotify(object):
|
|||||||
|
|
||||||
def _auth_headers(self):
|
def _auth_headers(self):
|
||||||
if self._auth:
|
if self._auth:
|
||||||
token_info = self._auth.get_cached_token()
|
return {'Authorization': 'Bearer {0}'.format(self._auth)}
|
||||||
access_token = token_info['access_token']
|
|
||||||
return {'Authorization': 'Bearer {0}'.format(access_token)}
|
|
||||||
elif self.client_credentials_manager:
|
elif self.client_credentials_manager:
|
||||||
token = self.client_credentials_manager.get_access_token()
|
token = self.client_credentials_manager.get_access_token()
|
||||||
return {'Authorization': 'Bearer {0}'.format(token)}
|
return {'Authorization': 'Bearer {0}'.format(token)}
|
||||||
|
|||||||
@ -16,6 +16,7 @@ import sys
|
|||||||
import time
|
import time
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
# Workaround to support both python 2 & 3
|
# Workaround to support both python 2 & 3
|
||||||
import six
|
import six
|
||||||
import six.moves.urllib.parse as urllibparse
|
import six.moves.urllib.parse as urllibparse
|
||||||
@ -134,41 +135,32 @@ class SpotifyOAuth(object):
|
|||||||
self.cache_path = cache_path
|
self.cache_path = cache_path
|
||||||
self.scope = self._normalize_scope(scope)
|
self.scope = self._normalize_scope(scope)
|
||||||
self.proxies = proxies
|
self.proxies = proxies
|
||||||
self.token_info = None
|
|
||||||
|
|
||||||
def get_cached_token(self):
|
def get_cached_token(self):
|
||||||
''' Gets a cached auth token
|
''' Gets a cached auth token
|
||||||
'''
|
'''
|
||||||
if self.token_info is not None:
|
token_info = None
|
||||||
self._refresh_token_if_expired()
|
|
||||||
return self.token_info
|
|
||||||
|
|
||||||
if self.cache_path:
|
if self.cache_path:
|
||||||
try:
|
try:
|
||||||
f = open(self.cache_path)
|
f = open(self.cache_path)
|
||||||
token_info_string = f.read()
|
token_info_string = f.read()
|
||||||
f.close()
|
f.close()
|
||||||
self.token_info = json.loads(token_info_string)
|
token_info = json.loads(token_info_string)
|
||||||
|
|
||||||
# if scopes don't match, then bail
|
# if scopes don't match, then bail
|
||||||
if 'scope' not in self.token_info or not self._is_scope_subset(
|
if 'scope' not in token_info or not self._is_scope_subset(
|
||||||
self.scope, self.token_info['scope']):
|
self.scope, token_info['scope']):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
self._refresh_token_if_expired()
|
if self.is_token_expired(token_info):
|
||||||
|
token_info = self.refresh_access_token(
|
||||||
|
token_info['refresh_token'])
|
||||||
|
|
||||||
except IOError:
|
except IOError:
|
||||||
pass
|
pass
|
||||||
|
return token_info
|
||||||
return self.token_info
|
|
||||||
|
|
||||||
def _refresh_token_if_expired(self):
|
|
||||||
if self.is_token_expired(self.token_info):
|
|
||||||
self.token_info = self.refresh_access_token(
|
|
||||||
self.token_info['refresh_token'])
|
|
||||||
|
|
||||||
def _save_token_info(self, token_info):
|
def _save_token_info(self, token_info):
|
||||||
self.token_info = token_info
|
|
||||||
if self.cache_path:
|
if self.cache_path:
|
||||||
try:
|
try:
|
||||||
f = open(self.cache_path, 'w')
|
f = open(self.cache_path, 'w')
|
||||||
|
|||||||
@ -103,7 +103,9 @@ def prompt_for_user_token(username, scope=None, client_id=None,
|
|||||||
print()
|
print()
|
||||||
|
|
||||||
code = sp_oauth.parse_response_code(response)
|
code = sp_oauth.parse_response_code(response)
|
||||||
sp_oauth.get_access_token(code)
|
token_info = sp_oauth.get_access_token(code)
|
||||||
|
|
||||||
# Auth'ed API request
|
# Auth'ed API request
|
||||||
return sp_oauth
|
if token_info:
|
||||||
|
return token_info['access_token']
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user