HTTP endpoint for less-awkward server-side token management

This commit is contained in:
James Barnsley
2016-10-30 18:06:43 +13:00
parent 49b511b038
commit 474ebc6738
9 changed files with 63 additions and 58 deletions

View File

@ -213,53 +213,4 @@ class IrisFrontend(pykka.ThreadingActor, CoreListener):
##
def restart( self ):
os.execl(sys.executable, *([sys.executable]+sys.argv))
class RequestHandler(tornado.web.RequestHandler):
def set_default_headers(self):
self.set_header("Access-Control-Allow-Origin", "*")
def initialize(self, core, config, version):
self.core = core
self.config = config
self.version = version
##
# Get a new spotify authentication token
#
# Uses the Client Credentials Flow, so is invisible to the user. We need this token for
# any backend spotify requests (we don't tap in to Mopidy-Spotify, yet). Also used for
# passing token to frontend for javascript requests without use of the Authorization Code Flow.
##
def refresh_spotify_token( self ):
url = 'https://accounts.spotify.com/api/token'
authorization = 'YTg3ZmI0ZGJlZDMwNDc1YjhjZWMzODUyM2RmZjUzZTI6ZDdjODlkMDc1M2VmNDA2OGJiYTE2NzhjNmNmMjZlZDY='
headers = {'Authorization' : 'Basic ' + authorization}
data = {'grant_type': 'client_credentials'}
data_encoded = urllib.urlencode( data )
req = urllib2.Request(url, data_encoded, headers)
try:
response = urllib2.urlopen(req, timeout=30).read()
response_dict = json.loads(response)
self.spotify_token = response_dict
return response_dict
except urllib2.HTTPError as e:
return e
##
# Get a new spotify authentication token
#
# Uses the Client Credentials Flow, so is invisible to the user. We need this token for
# any backend spotify requests (we don't tap in to Mopidy-Spotify, yet). Also used for
# passing token to frontend for javascript requests without use of the Authorization Code Flow.
##
def get(self):
token = self.refresh_spotify_token()
self.write( token )