Python Spotify API #2 - Setting Up The Endpoints

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
everyone welcome back to a new episode so today i'm following up on a video i did about using the spotify api because it's a highly requested topic that i complete this tutorial but highly requested i mean two people have asked me to do it sorry i didn't complete this earlier but you know i've been so busy just my calendar is completely packed you know i've been hanging out with so many friends i have so many friends i've been hanging out with friends and smile friends anyway let's get into it i did i recorded this whole thing and then lost the footage so i'm starting from scratch but no worries uh shouldn't take too long this time let's get into it so make yourself an app.py i want to show you guys this cool file i made um the setup.py i'm not gonna show you guys how to do it here but it basically lets you list out all your requires then you call this function and then you can do something really cool which is okay so i found the script here setup.py now if i call python3 setup py i can call install and what that'll do is install all of these dependencies most of these we're not going to need for this video but you know can't hurt to just do it anyway all right now let's get to the spotify nope spotify example all right cool so to run flask apps you just type flask run of course that's not going to run right now it's not going to work but let's get into it so what are we going to need first for our flask application probably flask right so let's go from flask import flask we're going to need this request thing this is how we're gonna you know get data from a request we're going to need url4 this just is a clean way to get urls session and then finally redirect if you watch the first video you'll know all about redirects we're also while we're here just going to import this thing called spotify so i think i have the dock up here spotify is this application that basically wraps the spotify api um in a nice little module and we're just gonna be using it because it's it's really easy to use so let's get into it so i always do this it's from this import uh spotify oauth and that's going to be the oauth we use all right so uh to get a flask application up let's get it up and working let's see app equals flask and then the application name that's just a static variable uh you don't have to set it it comes it's just given so that's going to actually create the spot this flask application so a few things that we want to set here we're going to set the secret key this is just going to be a random string and we this is basically going to be a random string that's going to be used to encrypt or sorry to sign the session cookie so it just make it something random i don't know i'll probably end up changing it you can generate it on the fly is probably the best idea you shouldn't check that into git but whatever doesn't matter for this for our purposes we don't really need to know how it works so then we've got this uh another thing we want to do is set this session cookie name um just to whatever you want to call it let's call it jason's cookie so um that's this is all going to be kind of using this session object we're going to talk about session a little bit more if you're not familiar with what a session is it's not that big of a deal but it's really not that complicated it's just it's just where we store data about a user's session so if they're on one page if they're on their home page and then they click like uh i don't know let's say you're on facebook facebook session you're on your home page and then you click your profile page you shouldn't have to log in again should be in the same session so that's what that is all right um another thing we need to do is set up a route i like to call these endpoints you'll often hear them referred to as endpoints in in industry we're going to set up a bunch of end points so the first one is just the index in that index what we do here is we just return html so it's pretty simple i can say jason's home page let's set up a second route to say um what do we want to call this one we'll call it uh get tracks we're going to have a get tracks endpoint anyway so these names don't have to match by the way which is kind of strange like this doesn't have to match that so i mean if you're feeling like you want to mix things up return uh some drake songs or something i'm gonna keep my musical preferences uh uh mystery for now but you'll find them out in a second when we get this working and so then like i said flask run should run the application and then you see we go to this url you can also go to localhost instead of 127.0.0.1 uh localhost is shorthand for that and there you go jason's homepage now if we throw in another thing what did we call it get tracks there you go some drake songs or something cool and you can see all that being logged here all right we're on our way um what what other things will we need well we're gonna need uh let's just get make this the login function so instead of having to like click a login button what's gonna happen is you're gonna go to the index page and it's gonna automatic automatically log you into spotify we're doing it because um i i'm not making like this really interactive site i'm just trying to get data from spotify uh what else will we need we've got get tracks we need this um authorize or sorry redirect url we talked about let's just call it redirect to be really explicit about it and this is going to this is going to be where spotify sends us back now if you go to that first video you'll make sure you understand that um redirect all right so here we've got the basics set up now what i want to do is stop pause for a second head over to the spotify developers page so spotify developer all right we'll go here um go to dashboard log in it's funny they just used oauth to log me in which is kind of funny but anyway let's continue so uh go ahead create another app call it uh test call my test two because the first time i i shot this was test all right cool look how easy that was it's already done grab that client id write it down just so you remember it show the secret i'm gonna change this so that you can't use mine but for now here's what it is now it should be mentioned you should never check this into github also don't check that into github because if if i steal yours if i steal these for your application then i can just pretend to be your app uh and that's no good it's like posting your password on your facebook you wouldn't do it uh good so now i had this like clip of code that i showed you guys last time and i'm going to pull it over in the last video i showed you this clip of code where we make this spotify oauth object and that's where i'm going to start plugging in these values and this i had i'd called it the read the auth the authorized url before now i'm going to call it the redirect url which is it's fine don't worry about it this is just the url we're telling them to come back to to send the user back to they're going to send the user here and this url4 this is just a really cool way of not having to hard code the path so i could put here localhost uh you know 5000 slash redirect and it would work for now but when i deploy this to production you know this is going to be now www.jasonsite.com oh jason jasonsite.com right and if i had hard-coded local host in here it wouldn't work and instead of having to keep track of all of this stuff you can just use this url for function and it'll go ahead and automatically generate that url for us so that just makes things a little bit easier again scope this is what we want we want to read the library really nothing groundbreaking so far one thing i i also need to mention i'm sorry to hold up again but this spotify oauth object don't try and save it in a variable up here just to be you know to not have to recreate objects all the time because you're trying to to be performant every time you use this object use a new one and it's bit me before trying to reuse it to be performant and then you end up getting in these weird states and nothing works so don't make my mistakes all right and let's just get into it like so the the login what we need to do is create one of these objects uh olaf equals create spotify oauth it's going to give us a spotify oauth object and then we do this url i believe it's the auth url we'll call it and take this function dot get uh authorize url and it's this simple we literally just redirect the user to that url pretty pretty simple right like there's nothing really to it and what will happen is um that they'll send the user back here because that's what we told them to but there is one very important thing spotify wants to make sure that they're sending the user back to the correct url uh and that there's no foul play going on so what they do here is they have this redirect uris in the in in your project you know you go i'm in the way uh in the project you go to edit settings and you've got this redirect uris look white listed addresses to redirect to so if i tried this application right now it wouldn't work the reason it wouldn't work is because i don't have localhost in here so i have to go http slash i actually write localhost it's kind of strange but they're just going to do like a string check i guess uh and then we want to call it redirect there you go now just for safe measure i like to do that also because they are considered different and then you know 127.00 this is just being extra precautious but you don't need to do all of this okay so um now now that that's white listed now that we have this all ready uh let's print yeah print print that we got to the redirect page is fine and let's give this application a run and see what happens okay oh got a server error what's the error yes yeah because i don't have to put the slash here because i'm using this url4 function redirect takes one argument redirect takes no arguments one given i don't know that to be true oh okay okay so yeah here's why i didn't call it redirect in the first place it's because it's getting confused um you know what i'm just gonna call this redirect page just to just that we don't have to go and change those white listed things again i don't want to have to do all that that's too much like work yep and i did mean you to redirect page here there we go so we've got this page up spotify do you give all this access yadda yadda we're going to click agree and then we get sent to the redirect page there you go so now you are halfway through interacting with the spotify api what's going to happen in the next step is sorry what's going to happen in the next step is um in this request basically we're going to do this uh in this request there is going to be some information there is going to be a um an access authorization or access token authorization token authorization code and i think they just refer to it as code what we're going to do is we're going to swap we're going to talk directly to spotify and swap that authorization code for an access token this access token is going to be used to get information but they're also going to give us something i didn't really talk about last video a refresh token this refresh token is basically you just check hey is the access token expired and if it is we go ahead and use the refresh token to get ourselves a new access token it's that simple there's really not that much to it and we're gonna go ahead and tackle that in the next video so thank you guys for watching this one i will see you in the next one
Info
Channel: Jason Goodison
Views: 23,137
Rating: undefined out of 5
Keywords: python flask oauth, oauth flask, spotify api python, python spotify, python spotify api tutorial, oauth, flask, flask oauth, how does oauth work, python, oauth2 explained, oauth 2.0 tutorial, oauth explained, python oauth2, python spotify api, python programming, intern, internship, internship projects, redirect url, oauth redirect url, oauth redirect url example, oauth redirect uri
Id: XZA_s-vfGKQ
Channel Id: undefined
Length: 16min 16sec (976 seconds)
Published: Thu Nov 12 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.