From 6d5add75b93e30df139ea78b87b8a3d28e3f3dd9 Mon Sep 17 00:00:00 2001 From: Casey Chu Date: Fri, 9 Oct 2020 18:39:30 -0700 Subject: [PATCH] Improve login flow --- spotify-backup.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/spotify-backup.py b/spotify-backup.py index 51bb081..627279c 100755 --- a/spotify-backup.py +++ b/spotify-backup.py @@ -54,12 +54,14 @@ class SpotifyAPI: # Pops open a browser window for a user to log in and authorize API access. @staticmethod def authorize(client_id, scope): - webbrowser.open('https://accounts.spotify.com/authorize?' + urllib.parse.urlencode({ + url = 'https://accounts.spotify.com/authorize?' + urllib.parse.urlencode({ 'response_type': 'token', 'client_id': client_id, 'scope': scope, 'redirect_uri': 'http://127.0.0.1:{}/redirect'.format(SpotifyAPI._SERVER_PORT) - })) + }) + log(f'Logging in (click to open if it doesn\'t open automatically): {url}') + webbrowser.open(url) # Start a simple, local HTTP server to listen for the authorization token... (i.e. a hack). server = SpotifyAPI._AuthorizationServer('127.0.0.1', SpotifyAPI._SERVER_PORT) @@ -97,7 +99,10 @@ class SpotifyAPI: self.send_header('Content-Type', 'text/html') self.end_headers() self.wfile.write(b'Thanks! You may now close this window.') - raise SpotifyAPI._Authorization(re.search('access_token=([^&]*)', self.path).group(1)) + + access_token = re.search('access_token=([^&]*)', self.path).group(1) + log('Received access token: {access_token}') + raise SpotifyAPI._Authorization(access_token) else: self.send_error(404)