What Do I Do With A Spotify Api Authentication Redirect Uri In Django?
Solution 1:
To implement this I ended up abandoning the spotipy module all together and just using javascript. I see your using the user authorization api flow which is fine because your server can send your secret key securely. When moving this to the frontend of your application you can't do this and instead must use the Implicit Grant flow:
#In your main page's <script>:
var loginSpotify = function(){
varSPOTIPY_CLIENT_ID = "Your Client ID Here"varSPOTIPY_REDIRECT_URI = "Your Django Callback View Here (www.example.com/callback/)"var spotifyScope = "playlist-read-private"var spotifyAuthEndpoint = "https://accounts.spotify.com/authorize?"+"client_id="+SPOTIPY_CLIENT_ID+"&redirect_uri="+SPOTIPY_REDIRECT_URI+"&scope="+spotifyScope+"&response_type=token&state=123";
window.open(spotifyAuthEndpoint,'callBackWindow','height=500,width=400');
//This event listener will trigger once your callback page adds the token to localStoragewindow.addEventListener("storage",function(event){
if (event.key == "accessToken"){
var spAccessToken = event.newValue;
//do things with spotify API using your access token here!!
}
});
}
Calling the above method will open a new popup window that will take the user through spotify's login. Once authorized, the window will redirect to the Django view you specify. Have that view load a callback page whose only purpose will be to collect the access token from its URI:
#in your views.py:defcallback(request):
return render(request, 'YourApp/spotifyLoginFinish.html',{})
When that page loads, in the popup window, it's URI will contain the grant you seek, ex: http://www.example.com/callback/#access_token=BQDoPzyrkeWu7xJegj3v1JDgQXWzxQk2lgXrQYonXkXIrhmjeJVS38PFthMtffwlkeWJlwejkwewlHaIaOmth_2UJ2xJrz2x-Voq0k0T0T4SuCUdIGY3w3cj5CpULkFla9zwKKjvdauM2KoUIQa1vULz-w8Da83x1&token_type=Bearer&expires_in=3600&state=123
Idk if youre using jquery or not but theres a bunch of ways to call a JS function on page load. So now all you need to do is parse the URI once the callback page loads and you can get your access token. Then you're done with this popup callback window, so you can pass the access token back to your main page by adding it to localStorage. Remember how on your main page we created an event listener to watch the localStorage so you'll know when its there. Here's an example of the code you'd want on your callback page (it uses some jquery but there are regular JS ways of doing stuff on page load):
#In spotifyLoginFinish.html's <script>:
$('document').ready(function(){
//i leave 'parseHash()' as an exercise for the reader, all it does is take the uri string(see above ex of what this looks like) and get the access_token from itvar spotifyAccessToken = parseHash(String(window.location.hash));
//you must clear localStorage for main page listener to trigger on all(including duplicate) entrieslocalStorage.clear();
localStorage.setItem('accessToken', spotifyAccessToken);
window.close();
});
Now your popup has closed itself and youre back in your main window, in the localStorage event listener function from the first piece of code in this post. It'll get the token from localStorage and youre all set! Hope this helps, please comment if it doesn't work.
Post a Comment for "What Do I Do With A Spotify Api Authentication Redirect Uri In Django?"