Building .NET Core Apps with Microsoft Graph API: A Step-by-Step Guide Using dotnet SDK | AZ-204

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign smart coding in this video what I'm going to talk about is the Microsoft crap how do we access the apis the graph apis using the SDK come let's dive in all right first thing first so before we proceed basically you need visual studio for this demo I have Visual Studio 2022 and you can even use Visual Studio 2019 but the sample code that you're seeing on my screen which is indicator repository is built using the dotnet6 so if you don't have the dotmus X installed I will give you this link you can come over here you can install the dotnet SDK 4.net 6.0 once you install the studio Visual Studio you will be ready to download this code and open up this code so the sample code that you're seeing here has been built using a tutorial from the Microsoft I will give you the link for the tutorial if you want to do it by yourself feel free to do it but I'm gonna open up this project and I'll demonstrate what it is and how we have to use it I have opened up this project so in this project there are only few files okay let's quickly go through it okay okay so you have a app settings.json file so basically under the settings we have some client ID tenant ID and then the Scopes if you remember in the previous video every single API is tied up with a scope okay if you're talking about the profile then we'll start up with the user scope uses dot read or uses dot read write or use is not right so that's why right so in this demo based on Microsoft tutorial we are going to talk about the profile accessing the profile get the access token and then do some actions about the mailing stuff okay so that is what this project is all about that's why here the Scopes has only the uses of read mail.read and mail.censcope okay all right now uh let's take a look at the other files so we have something called graph Helper and then there are a couple of methods we have program dot CS where the main methods have been called okay and then there's something called settings file it's basically reading all the settings in the app settings files now before we dive into each of these right so let's run this and see how it works okay all right so let's let's run this dot net run so now what it is going to do is it is going to authenticate this app at the uh registration level which we will see shortly so basically let's follow on the screen section so it is asking us to go to this link so once I go to this link it is asking me to put the code let's copy this code export this all right once I put my code it is asking me to log in so I'm going to use my accessing login it's gonna send me some notification to my authenticator app and then let me authorize myself I put my request it's authenticated and now it's asking me that hey some app the application which is running from the public right it's not running on the Azure uh portal it is running from our local so basically it's saying the app that you're trying to unverified app this is trying to access your profile it's going to read your emails and email do you want to give the access to this and I say yes so when I say s it is all done now let's go to the screen see once that is done you can see couple of options coming up so if I click on one it's going to give me the access token you remember the previous video there was one section called the access token section so that is what so we will see we will take a look at the code what kind of code has been running to get this access token okay now let's take a look at the second one third and fourth okay so the second one is basically list the uh inbox okay so there is no items that's why it's not listing so let's me click on number three to send an email so it's saying this is my email ID and there was an email sent okay so if I log into this email ID I should be able to see some emails so you can see in my email whenever we sign in right through the app it's sending me an email that you know something is new sign-in has been detected that you have to review it on all those things all right okay now it's time to quickly dig into this code and take a look at this code so before we take a look at the code and replace some IDs okay there are one important thing that we have to do is the app registration so basically when you come to your portal I've logged in with my relearn smart coding at the gmail.com okay so once you come here uh there is something called app registration you can either type here and uh like app registration so for me it's already there because I've been using it so once you go there uh you know there was some registration done for this demo that's why we're able to see the demo coming up but let me show you how to do it click on new registration and provide a name so in this case graph graph demo I'll put a scrap demo and here what you have to do is because I'm using the personal uh Microsoft account and it's outside uh this it's a you have to choose the third option okay so accounts in any optimization directory and the personal Microsoft account okay so once you choose this that's it don't give anything redirect URI and I'll just leave it and do a registration once you register this app you need to go inside this app which we are already there and you can see the client ID this is the client ID okay so you need to copy this ID and if you come to this app settings you have to put your client ID here okay so once you put your client ID here that's it uh you don't need to do anything else and then there is something that you have to do here okay so now click on this authentication okay and then if you scroll down there's something called Hollow public line flows okay this is currently set to no you need to set your as and you will say reason because the application that we are running is a console application which is running from the public right public means it's not within the Azure Network it's outside the network so in order for us to access or create any application that can access this ad you have to make this uh as yes and your save so once you're done with that we should be okay with the authentication okay now I copied this application client ID and I'm going to go back to this one and go here and change this to the new one that we added okay so we're good so right now we have access to this user.read and mail dot read okay so let's go to this uh API permissions and then click on Adder permissions and here you can choose Microsoft graph and then application permission so here you can actually say users and you know users will come you see this under user you can actually provide the scope here so this is the scope that we were giving consent uh from the graph API if you remember okay so I'm gonna uh you know give read access I'm going to give all this basic read and write access add permission and then let's add few more things which is the mail so if I click here application and let's search with mail see we got the mail right so I can give all the mail access so that it can send me email so add permission and once you add these things here you see this it's just added here we haven't granted the permission so basically what you have to do is you have to click on Grant admin concern and say yes so it's giving you the access now everything is given all good so we can refresh and see now this is all set all what we have to do is we need to rerun the application okay now let's save this let's go back and let's do an exit let's run this so now when it runs if you remember it's gonna give me a different application ID right so I'm gonna copy this head back to browser and hit on the Z Model it is asking me to give the code I'm going to copy the code face structure next it is going to authenticate I am saying my credential send notification let's approve this quickly I'm approving it it's upload okay now you can see clearly we gave a different uh the access right so based on the axis now it is asking me can I run this I say yes all done now I can actually play with this access token I got I can send an email okay so all good right so this is how you do so now let's quickly go take a look at the code how things works if you go to this program dot uh CS the first one that executes is the initial initialize graph basically we are reading the settings and uh initializing it and then uh based on the option that you choose each method has been called okay all right so once the settings has been loaded once we initialize the graph you can see the first one is the create user and if I come here okay so we are calling a method called uh the caduce racing from the helper so let's go to this method and see what is there in this one okay so basically this helper method let's go to this Helper and get user async let's find it so this is the get user async method and you can clearly see basically what we are doing is we are returning the client I mean we are trying to access the client so let's see where is this client user client is coming okay so user client is coming from here so let's take a look at this user client okay so it's a static variable it's been initialized in the Constructor so what basically it is initializing is it's going and sending the device code credential and the scope that we have it here okay so based on the initialization that we have we can actually use that dot me dot me if you remember that is what the URL or in other words I can say you see this dot me so this is in turn calling this endpoint okay so every single action that we do here is in turn calling an end point so we are just using X SDK to call that and once you request that uh API it is going to fetch this information and this information is being sent and that is what we display in the console application similarly you can see there are many methods here okay all these methods are doing the similar one so so for example it is for the for the inbox axis we are just reading the mailbox folder messages and then we're sending the request eventually you know the request has been sent and it is just taking the top 55 one so all these things so long story short all what I'm saying is Microsoft graph is available through the apis which you can explore from the graph Explorer and if you want to use uh you know benefited stuff through the SDK you can also use the Microsoft SDK Microsoft graph SDK is available for Android Java typescript and so many languages including this C sharp so we are using dot at SDK to build this one so we are in turn calling all the graph API and we can nicely Even build a sample application or a useful application which will be used by your organization or for your own purpose okay so hope you understand and you enjoyed it thanks for watching if you have any questions let me know in the comment section and uh I'll be more than happy to help you thanks for watching if you like my video don't forget to subscribe my channel like it share it comment it and never forget to click on the Bell icon
Info
Channel: Learn Smart Coding
Views: 6,105
Rating: undefined out of 5
Keywords: Microsoft Graph, Graph Explorer, Microsoft 365, Microsoft Graph API, Enterprise Mobility + Security services, Microsoft 365 core services, Dynamics 365 Business Central services, Access Microsoft Graph data, dotnet core adk, How do I use Microsoft Graph API in .NET core?, How do I use Microsoft Graph API in .NET core using dotnet SDK
Id: VuHMc_h6nvo
Channel Id: undefined
Length: 12min 26sec (746 seconds)
Published: Tue Jan 03 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.