From bf65ae3f4d29cd3af348e6118976460a6f272458 Mon Sep 17 00:00:00 2001 From: Chris Date: Mon, 14 May 2018 10:29:15 -0400 Subject: [PATCH] Use different URI for user data The user_data page is now located at /user_data, instead of /callback as before. Closes #7. --- spotifyvis/urls.py | 2 +- spotifyvis/views.py | 30 ++++++++++++++++++++++++++---- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/spotifyvis/urls.py b/spotifyvis/urls.py index b9c0221..0d5e894 100644 --- a/spotifyvis/urls.py +++ b/spotifyvis/urls.py @@ -5,5 +5,5 @@ urlpatterns = [ path('', views.index, name='index'), path('login', views.login, name='login'), path('callback', views.callback, name='callback'), - + path('user_data', views.user_data, name='user_data'), ] \ No newline at end of file diff --git a/spotifyvis/views.py b/spotifyvis/views.py index 4070133..5a4ee80 100644 --- a/spotifyvis/views.py +++ b/spotifyvis/views.py @@ -5,7 +5,9 @@ import random import requests import os import urllib -import datetime +from datetime import datetime + +TIME_FORMAT = '%Y-%m-%d-%H-%M-%S' def generate_random_string(length): """Generates a random string of a certain length @@ -32,7 +34,7 @@ def token_expired(token_obtained_at, valid_for): token_obtained_at: datetime object representing the date and time when the token was obtained valid_for: the time duration for which the token is valid, in seconds """ - time_elapsed = (datetime.datetime.today() - token_obtained_at).seconds + time_elapsed = (datetime.today() - token_obtained_at).total_seconds() return time_elapsed >= valid_for @@ -78,13 +80,33 @@ def callback(request): response = requests.post('https://accounts.spotify.com/api/token', data = payload).json() # despite its name, datetime.today() returns a datetime object, not a date object # use datetime.strptime() to get a datetime object from a string - request.session['token_obtained_at'] = str(datetime.datetime.today()) + request.session['token_obtained_at'] = datetime.strftime(datetime.today(), TIME_FORMAT) request.session['access_token'] = response['access_token'] request.session['refresh_token'] = response['refresh_token'] request.session['valid_for'] = response['expires_in'] print(response) - auth_token_str = "Bearer " + response['access_token'] + return redirect('user_data') + + +def user_data(request): + + token_obtained_at = datetime.strptime(request.session['token_obtained_at'], TIME_FORMAT) + valid_for = int(request.session['valid_for']) + + if token_expired(token_obtained_at, valid_for): + req_body = { + 'grant_type': 'refresh_token', + 'refresh_token': request.session['refresh_token'], + 'client_id': os.environ['SPOTIFY_CLIENT_ID'], + 'client_secret': os.environ['SPOTIFY_CLIENT_SECRET'] + } + + refresh_token_response = requests.post('https://accounts.spotify.com/api/token', data = req_body).json() + request.session['access_token'] = refresh_token_response['access_token'] + request.session['valid_for'] = refresh_token_response['expires_in'] + + auth_token_str = "Bearer " + request.session['access_token'] headers = { 'Authorization': auth_token_str }