Connecting to Microsoft Graph API with Python msal library.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to my latest video on how to authenticate with Microsoft graph API and python in this video I'll be diving into the world of Microsoft's graph API and showing you how to access its resources and perform various operations the Microsoft graph API is a unified API endpoint for accessing data across Microsoft 365 services including SharePoint OneDrive and more with the help of python I'll show you how to obtain the necessary authentication token to use it to interact with the graph API in a secure and efficient manner so sit back relax and get ready to learn how to do that so let me start with describing the goal we want to achieve so I have my demo account of Microsoft for the development purposes and it has some data in it so we have a couple of users automatically created these are some some dummy data these are fake people so they are not really actual users and not not real identities so our goal is going to use graph API to actually get those names and uh first of all let me create a python project so to do that I'm going to create Neutron virtual environment and I'm going to activate it so let me open up visual studio code and I have a couple of libraries I like to use for my development so the linter which in my case I use flake 8 black for and I sorts for automatic formatting of my code I hover the two libraries or two packages we are going to need to use graph API to connect with graph API is the m cell which stands for Microsoft authentication library and we are going to use requests to basically make some basic HTTP rest calls to the graph API so let me install those let me install those requirements and we should be good to go now let me create a graph demo file if we are gonna put our code into so that we can actually start development so I think we are ready and yeah so let's import them so what we are going to do what we are what we need basically to authenticate this couple of things so client ID client Secret and tenant ID so let's go to our Azure portal and let's figure out what we need so we need an application or we need the user some sort of a user and uh in Microsoft overall this this user this technical user is called app registration so we need to basically have an application this application will then have permissions and it will be able to authenticate and authorize to make API calls so I have created the application and this application is sometimes called a client and it has ID a uuid for it so we need to we need that and we need that in our we will need that for our code and we need as well the 10th ID in secret that's another 10th ID is sometimes called directory ID so let me copy this one and then the client secret kind secret is basically like a password for our application and we can create a secret for our application here by default there is no secret so let me create this Secret let me copy it we have this the secret in the value and we should be good to go I'm going to remove this data so I'm not really worried about showing you that however of course your secret should be kept somewhere not in the code but somewhere somewhere else like in Key Vault or environment variables of your of your system that you have um but for the purpose of this video I'm gonna just just leave it in the code itself so we have our application we have the password for our application or the secret however the application requires specific permissions so in the API permissions we need a couple of things so to get the users from the graph API we will need a couple of permissions so so make sure that you select the application permissions so I've clicked on ADD permissions and then application permissions and we're gonna go for users and I'm just gonna go user read all and we might need as well directory to access Direct Microsoft Azure access directory so I'm going to go and read off now you will notice that there is it is not granted for Microsoft here so let me Grant those permissions that Will consent and once we should see now green check marks so it means that we are good to go and these permissions are granted now for for our application um actually from him so let's import that's your client application so we want to create something that is called a confidential client application This Confidential client application requires a couple of input parameters so and client ID it requires client credentials credential and it's going to be our client Secret and requires as well Authority Authority is basically like a string that we need to provide so we need to tell Microsoft how what we want to authenticate with and this m cell Authority is basically a string that ends with r1080 so I have actually and prepare this slink so that's the link itself so login.microsoftonline.com 1080 and now we will be able to get our application [Music] so to obtain the token we need to ask our application to for the token itself so let's go for it and some application acquire why it doesn't um choir okay sure why it's enough helping me out with with some smart okay what do we need to provide here to acquire the token is some something that's called scope you don't have scope defined yet um let me Define it so scope basically is a list of star that we want to get access to and in our case it's going to be a graph Microsoft graph default scope so we're going to use the default permissions of our client of our application require took it silent so I'll try to obtain the token from the cache however if it will not if it will fail we have to use a different let's say method so I'm going to check if not and salt the result is equal to Amazon at acquire you can for clients this one doesn't require the account so if you will failure here we'll we're going to do it here don't ask me why it couldn't be done in one go um this is what it is so now let's check if we actually have our access token for Access token is presently the result otherwise we want to raise an exception this token account okay cool um let's actually try to see if we are forget this access token I'm gonna run this code and we probably should get it already okay so you can see that I was able to obtain the access token so because it's very good to go now one so you have the extra stock and we can use it in the header to actually authenticate with our with the graph API um so let me import requests and now we're going to do response requests get and go around so you need a URL and graph API has its own URL so we need to know it and we can of course get it from the documentation that's the URL I want to get the users in order to authenticate with the graph API we need to provide a header with a better than the token others headers let me actually create some headers oops some headers our headers should be authorization actually now and then the content type application Json so add those two prepared so just not make any typo and now if you provide those headers you should be able to get the users and see if we'll get the users let me pretty print that Prince Jason dump s response four so that should give us a little bit more prettier output of of what we want to get so let's see if we are gonna be successful to obtain our list of users yay so you can see that we were actually successful and we were able to get our our users so you can see that there are some some users here uh let's say Adele V we should probably see the Adobe in our our users so let's see yeah until V you can see that this is the person and go to the details of the person should get the [Music] membership and all the other stuff for that person so um and that's it I think for the video so in that we were able to I was able to show you how to investigate with Microsoft graph API with python I hope you found it informative and helpful in case of any questions or or comments leave me a leave a comment and don't forget to like And subscribe to stay up to date with other videos I will be making and thank you for watching and I'll see you next time
Info
Channel: Python Bites
Views: 11,396
Rating: undefined out of 5
Keywords: python, msal, microsoft graph, graph api, sharepoint api, azure, aad
Id: wAIZn6RDSJg
Channel Id: undefined
Length: 13min 48sec (828 seconds)
Published: Sat Feb 04 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.