HTTP endpoint for less-awkward server-side token management
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@ -4,3 +4,4 @@ public_html
|
||||
/.sass-cache/
|
||||
*.orig
|
||||
Thumbs.db
|
||||
*.pyc
|
||||
|
||||
@ -4,7 +4,8 @@ import logging, os, json
|
||||
import tornado.web
|
||||
import tornado.websocket
|
||||
from mopidy import config, ext
|
||||
from frontend import IrisFrontend, RequestHandler
|
||||
from frontend import IrisFrontend
|
||||
from http import RequestHandler
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
__version__ = '2.10.1'
|
||||
@ -49,10 +50,9 @@ def factory(config, core):
|
||||
(r"/images/(.*)", tornado.web.StaticFileHandler, {
|
||||
"path": config['local-images']['image_dir']
|
||||
}),
|
||||
(r'/http', RequestHandler, {
|
||||
(r'/http/([^/]*)', RequestHandler, {
|
||||
'core': core,
|
||||
'config': config,
|
||||
'version': __version__
|
||||
'config': config
|
||||
}),
|
||||
(r'/(.*)', tornado.web.StaticFileHandler, {
|
||||
"path": path,
|
||||
|
||||
Binary file not shown.
@ -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 )
|
||||
|
||||
Binary file not shown.
53
mopidy_iris/http.py
Executable file
53
mopidy_iris/http.py
Executable file
@ -0,0 +1,53 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import logging, json, urllib, urllib2
|
||||
import tornado.web
|
||||
from spotipy import Spotify
|
||||
|
||||
# import logger
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class RequestHandler(tornado.web.RequestHandler):
|
||||
|
||||
def set_default_headers(self):
|
||||
self.set_header("Access-Control-Allow-Origin", "*")
|
||||
|
||||
def initialize(self, core, config):
|
||||
self.core = core
|
||||
self.config = config
|
||||
|
||||
def get(self, slug=None):
|
||||
|
||||
if( slug == 'refresh_spotify_token' ):
|
||||
self.write( self.refresh_spotify_token() )
|
||||
return
|
||||
|
||||
else:
|
||||
self.write('Invalid request')
|
||||
return
|
||||
|
||||
|
||||
##
|
||||
# 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 = json.loads(response)
|
||||
return response
|
||||
except urllib2.HTTPError as e:
|
||||
return e
|
||||
|
||||
@ -1068,7 +1068,7 @@
|
||||
|
||||
$.ajax({
|
||||
method: 'GET',
|
||||
url: '//' + getState().mopidy.host + ':' + getState().mopidy.port + '/iris/http?action=refresh_spotify_token',
|
||||
url: '//' + getState().mopidy.host + ':' + getState().mopidy.port + '/iris/http/refresh_spotify_token',
|
||||
dataType: "json",
|
||||
timeout: 10000
|
||||
}).then(function (response) {
|
||||
@ -7629,7 +7629,7 @@
|
||||
/* 66 */
|
||||
/***/ function(module, exports, __webpack_require__) {
|
||||
|
||||
var __WEBPACK_AMD_DEFINE_RESULT__;var require;/* WEBPACK VAR INJECTION */(function(process) {/** @license MIT License (c) copyright 2010-2014 original author or authors */
|
||||
var require;var __WEBPACK_AMD_DEFINE_RESULT__;/* WEBPACK VAR INJECTION */(function(process) {/** @license MIT License (c) copyright 2010-2014 original author or authors */
|
||||
/** @author Brian Cavalier */
|
||||
/** @author John Hann */
|
||||
|
||||
|
||||
File diff suppressed because one or more lines are too long
@ -130,7 +130,7 @@ function refreshToken( dispatch, getState ){
|
||||
|
||||
$.ajax({
|
||||
method: 'GET',
|
||||
url: '//'+getState().mopidy.host+':'+getState().mopidy.port+'/iris/http?action=refresh_spotify_token',
|
||||
url: '//'+getState().mopidy.host+':'+getState().mopidy.port+'/iris/http/refresh_spotify_token',
|
||||
dataType: "json",
|
||||
timeout: 10000
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user