Using the Microsoft Graph API in Python | Authentication

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back to another python tutorial all right so today we are kicking off a new series where we are going to start working with the microsoft graph api so for those of you who are not aware the microsoft graph api is provided by you guessed it microsoft and it's an api that the way i like to think about it is it's kind of like this one size fits all or this kind of one-stop uh place where you can grab a bunch of different information about your office 365 account and being able to leverage it in other projects or applications so the idea behind it is you would go to the graph api if you want to get user information if you wanted to get file information in some cases you want to get email information or even teams call so microsoft teams call information and being able to go to this one place to grab all of that kind of office 365 if you want to put it information from a single location so it's very advantageous additionally we can use the graph api in a limited fashion to interact with office applications like excel so while you wouldn't necessarily work specifically with the excel application you could work with an excel workbook for example and you could grab range values or you could grab a table object for example the same goes for other applications like outlook and microsoft onenote you could use the graph api to grab that information so what we're going to do in the first video is we're going to get an application set up on microsoft azure and we're going to make sure that this application can access the microsoft graph api and then once we've done that we are going to show you how to clone the microsoft graph api client library i built so that way you can start working with the graph api now i will warn you that the library is still in development so i haven't nearly hit all the endpoints but there are a few different services you can kind of start using in a limited fashion so things like drive services the notebook services and the next one i think i'm working on is i believe mail if i remember correctly i think it was insert the mail one next or file items so that's what the plan is for this particular video is get the app set up make sure you can clone the repo and then from there we're gonna just get you a token and then save that token so with that being said let's get started now i am going to just take you really briefly to the microsoft graph homepage so if you go to developer.microsoft.com forward slash graph this will take you to the main page where you can learn more about the microsoft graph project or not project product and additionally they do offer things like getting started guides documentation and then the api reference if you want to see specific endpoints also some resources like blogs training sample sdks and all sorts of different stuff additionally they have a tool for those of you who want to kind of just start using it right away to see kind of what it looks like and what are the things you can do with it they have the graph explorer so if you click this little link here you're going to be redirected to this little page and then this is a way where you could interact with the graph api to see just some common examples that you could do so you can see here there's uh endpoints that are related to excel groups microsoft teams microsoft to do notifications onedrive onenote outlook calendar people planner sharepoint sites sharepoint lists search lots of different things that you can start doing with it so for example if i wanted to grab my my root drive and then the children that belong to that drive i could just do this url and then you know lo and behold you get a list of information so lots of good information here and also it kind of helps if you need to just kind of start exploring a little bit and figuring out what endpoints you might need to hit in order to get the information you need this would be a good place to start additionally they do provide some quick start guides that fit different languages i will warn you that these are not necessarily like actual packages like you can install these are more like projects where you could get up and started with a particular language and they walk you through some of the common stuff in order to get ready with that and then additionally they have some tutorials on the documentation page as well now i also have provided my own documentation on github it's more for setting up the application because that's really where most of the time is going to be spent what i would say is getting ready so if you go to my github a read 1192 and then you go to my repositories tab because this one's not pinned on the front you're going to see one for the microsoft graph python clients and if you go into the samples folder you'll see setting up an app.md this is a readme file and this will walk you through the steps in order to uh get ready so feel free to use that as well so in order to set up an application like i said before we're gonna have to go on microsoft azure so if you go to your azure portal so there's different ways you can do it you can just go to azure portal like this and if you go to microsoft azure portal you can go to portal if you're not signed in you would have to sign in obviously and then once you're here we're going to go to your active directory and then once we're at that active directory we're gonna set up a new uh application why is it not loading god forbid it's like sitting and sitting and sitting and sitting what's going on why is it taking so long to load well while that's loading i don't know why it's not loading it could be that the portal is down which would be very upsetting if it was when i'm trying to record a video additionally if you do want to explore my library a little bit i do have the actual underlying code as well i think it's going perfect okay we got somewhere that's the that's the fun part okay so once you're on your dashboard page or your homepage wherever you end up being you want to go over here to the left side pane and you'll notice one called azure active directory and then from here you would want to go down to app registrations and then you're going to want to do new registration now once you do new registration you do need to give your application a name so say for example microsoft graph api tests can be whatever you want but naturally you try to give your application a name that's meaningful and then from here there's a couple things that you need to consider so when you register an application you want to be able to decide who can access this application so by default it says accounts in this organizational directory only in other words it's saying basically the azure directory itself so that's the only one who can access it or that's the only accounts you could access so you could access accounts that are associated with this directory this does not represent a personal account i'm going to say that again this does not represent a personal account additionally you can do any organizational directory so if you have multiple azure directories so i have a personal one and then i have a work one if i wanted to be able to access multiple directories then i would do this one right here the second option additionally if you wanted to be able to access multiple directories and personal microsoft accounts then you would want to do this one we are going to be doing personal microsoft accounts because i want to be able to get information related to my personal account so my personal onedrive not my work onedrive my personal onedrive now this will do it for both so i'm okay with doing this one if you want it to be a little bit more strict you could just say i want to do only personal microsoft accounts so that's an option as well additionally you need to specify a redirect uri you don't technically have to add this but you should so this will help when it comes to the login process so in this situation i just need to provide a url and in this situation i don't have to do anything fancy i can just do localhost and then i can give it a path basically so i'll just call it graph login that works for me i'm going to copy this really quickly and then when we get to the point where we need to clone our repository i will show you where we're going to be putting it but i'm just going to put it there for right now i'm going to go back here and then you would just do register and it does take a second or two who knows it might take a little bit longer given how slow it's been but it looks like it went pretty quickly so once you're here there's a couple other pieces of information that you need the first thing is your client id so this little guy right here so we'll copy that as well again i'm going to put it in my little folder so that way it's there and then additionally we need to get a client secret so this is not on this page you actually have to navigate a little bit so on this little pane right here you'll notice certificates and secrets so you're just going to click that and then from here you're going to do new client secret okay and then you have an option when do you want it to expire one year two year or never i'll just but never to be simple and then from here i need to add a client secret so this is just a description uh my client secret for my graph api project again nothing super crazy but just a little bit and then it's going to create a secret please make sure to copy it from this page if you leave this page you can't get it again it's gone you have to create a new client secret so copy it once you copy it i'm just gonna again put it in my little file right here and that is pretty much it simple i know but it's not too bad and then i think with this one we can see well i guess it doesn't technically go away right away but basically if you were to close out the portal and then try to go back into it you wouldn't be able to see it and they put that on the documentation as well but you can see right here it's still matching the same so just copy it in other words just be safe and copy it even if it might stick there for like two seconds okay and then once you do that that's it for right now next we're gonna go into the microsoft graph python library and we're gonna clone it so i'm gonna copy this really quickly so we'll go over to our main one and then i'll do code and then right here uh you want to do https they have a new cli option as well but we want this one i'm gonna go back into my visual studio code i'm gonna do a new window really quickly just so you guys can see this process okay i got my source control pane i'm gonna go there then go to clone repository control v to paste it in here and then press enter okay anytime you clone it you need to specify a location i want on my desktop just to be simple and then you can say open this repository and basically it's going to look exactly the same as like we said before so um the the main thing that you want to do is when you're in this particular location i did provide some sample files for you so you would just want to fill out this information in the write config video dot py file like i did in the previous window so you'd want to just fill it out like you see it here obviously this information would be different it would match with what's on your app and then from here you can just do run python file in terminal and you can see right here it does create a new uh file for you i'm actually gonna make it where it overwrites that other one for me okay perfect i'm gonna delete that other one i don't need it and that's pretty much it for the the config file so now from this point if you wanted to actually install the library so if you wanted to actually install it because it's not technically hosted on pi pi yet still in development if you want to do a local install it would simply be pip install hyphen e period so for your root directory and then you do enter now this should go pretty quick for me because i already have it at least all the other stuff oh we get to do another update soon fun it's only two requirements right now so there is a request library and then there is the microsoft authentication library this is actually really helpful when it comes to getting an access token in a refresh token so this is leveraged in the main client file you'll see me use it multiple times when it comes to getting tokens and all sorts of fun stuff like that so i think it's some of these you'll see we use the actual thing so grab refresh token and stuff like that and then from here just to kind of validate and go through this process of actually using certain services i'm going to leverage this one right here and i'm just going to change a couple things so i'm actually going to do a couple things that we need here i'll create a new file and i'll say test client dot py um i'll import pretty prints i will import config parser actually i'll import the actual object so from config parser import config parser and then actually from here too i'll do the same thing so from pretty print import pretty print just like that and then obviously we need our our microsoft graph client as well so we say from our microsoft.clients import the graph microsoft graph client so that's what that one would do and just to kind of make things go a little bit quicker i just want to grab some of this and then we'll walk through what's kind of a going on here already disregard this for a second so all this is going to do is it's going to initialize your config parser so that's going to basically help read your file now right now it's leading into my old one i don't want it to do that i want it to lead into this one that is on my samples folder so i'm going to go to samples and then configs s at the end and then i want to make sure i'm still reading that config.ini file keep in mind that i organized it into sections so there's a graph api section and there's simply free values i read each of those values and i store them in a variable and then from here i simply initiate a new instance of my microsoft graph client there's a couple arguments i need to pass through so i'm gonna first change this really quickly before i forget uh you need to pass through your client id you need to pass through your client secret your redirect uri and then your scopes again i'll talk about that in a second and then your credentials so the credentials is simply a file path to a json file where you can store things like your refresh token and your access token the reason we're doing this is so that way if you close down your script and then say you walk away from your computer for two days and then you come back and you want to reuse your code again you don't have to go through the full authentication process again because by that point your access token will have expired your access token expires i think in like one hour and so when it expires you're gonna have to get a new one using your refresh token so if that refresh token is not provided then i can't get you a new access token and then i'm gonna have to ask you to go through that full authentication process again however if you provide this what it will do is it saves that access token and your refresh token in a location ideally you want to put it so much secure and then what it will do is every time you initialize the script it will take that refresh token and as your access token expires it will go and get you a new one behind the scenes so that's nice you don't have to worry about it then and then you can just use your script every so often uh don't quote me i'm not sure exactly how long the refresh token expires but i ran it this morning and it's been a few days since i've used the graph api so it seems like it lasts a pretty decent long time okay and then something that is important when it comes to microsoft graph is you do have to specify your scopes and basically the scope it just defines what access you have to what services right so for example i have files well i might want to read write to all those files so i might want to be able to read those files and write those files so you need to specify your scopes depending on what you're trying to do now you might be wondering well what scopes do i need when well i'm going to kind of throw it back to you and i'm saying well what service do you want to use if you go up to me and say hey alex i want to use files great let me give you some options and you tell me what works best for you so i think if i remember correctly if you go down to the different services down here so like say i go to overview for like calendars and it's slow today let me also go here too because there's another place you can get it as well which is if you go to api permissions on your azure portal you can also see certain ones that are delegated at this point so for example if i want to see the different permissions i could go add permission and then i can go to microsoft graph and i can look at application permissions and you can see all the permissions that you can offer so for example let's say i want to look at mail you can do mail read mail read basic uh mail read basic all and they give you a nice little description about what those what that access entails and whether it requires admin consent so if i want to be able to read your emails i have to have admin consent at least from the application perspective so this kind of gets a little bit complicated it's really kind of centered around how you want this application in your situation because you're going to be interacting with the graph api directly and you're going to be able to click a button that says i give you permission you don't technically have to do it from right here so um i think it gave up here find meeting times here we go so i know they listed it somewhere i can't remember exactly i'll have to just put a link because i'm not going to spend too much time on it but there's different places you can find what scopes you need when i think they even tell you on the graph explorer so here is where you can see your permissions as well so you can modify these permissions you can do all sorts of different things so this is another place as well where if you need to see that information you need to see what permissions you need when this would also be a really good resource to kind of do that so if you if you use it they will prompt you and say hey do you want you know to be able to do this so don't feel like there's not a lot of resources around this topic there really is so what we're going to do next is now that we've kind of written everything out we're going to log in first so i'm going to run this and then we're going to show you what the output looks like oh my goodness so just like the td ameritrade api library um you will need to go to a new url and you can see right here and so first they're telling you hey this is an unverified app which is a good thing it's basically saying i don't know who it is but they're asking for these permissions so it's something good to be aware of and it's just telling you the permissions that you want to consent to and i'm saying okay i'm fine with this you can read all my information take all my data right and then it's going to go to it you're going to want to copy this little guy up here this is your redirect url so this contains the code that i need in order to go and get your information and then once you've done that you'll notice over here a new file was created and this contains all your information so there you go oh my god so much different stuff your access token your refresh token id token so lots of different information so now that we've done that let's kind of just take it one little tiny step further and let's see um let's get my root drive and we'll just again show you kind of what that looks like so the nice thing i try to do with the graph client is if you want to go to a particular service as i call it you would just specify that service so here i'm saying okay i want to get the drive services i'm probably not going to make methods i might make them properties to kind of represent like an object per per se so this might change a little bit but i'll probably have updated it by the time i record the second video so all this is going to do is it's going to go to my drive services and then it's going to get my root drive so basically my personal one drive so if i run this i should see a bunch of crazy information you're like oh my goodness look at all this stuff so it's basically just my drive nothing super fancy but enough to uh make it interesting right so you can see there's a tag created by oh my god create a time folder account so i have 36 folders so on and so on if i wanted to i could even navigate to it so lots of good stuff so lots of good information and then you could technically use this information so they have things like a drive id which you could then go and get drive items like actual files and so on and so on so with that being said though that does conclude our first video of getting you up and running with the microsoft graph api so if you do have any questions about uh just getting it set up if you're confused about permissions or anything like that you know feel free to put those down below as well and i'll do my best to hopefully guide you through it and then next video we're going to go through i think probably the drive services first we'll go in detail looking at the different endpoints and then probably after that we will go to users and then onenote so we'll start with an application looking at onenote so again any final questions put them down below otherwise we will see you in the next video
Info
Channel: Sigma Coding
Views: 36,178
Rating: undefined out of 5
Keywords: Microsoft, Graph API, API, Python, Microsoft Graph API, How to use the Microsfot Graph API in Python?, Access Token, Refresh Token, Authentication, OAuth, Microsoft Azure, How to authenticate an Application with the Graph API?
Id: bBslvqBCjAA
Channel Id: undefined
Length: 24min 43sec (1483 seconds)
Published: Sun Oct 25 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.