From the course: Microsoft Graph for Developers

Write the daemon logic and query

From the course: Microsoft Graph for Developers

Write the daemon logic and query

- [Instructor] Now, I went ahead and added some using statements at the top. These are statements we will need because, you know, we want to talk adal, we need to create an authority for which we need system dot globalization. And it's probably going to be a multi threaded call, so, system dot threading, it'll be a issue because of system net http, and so and so forth. For system dot configuration to work, I need to add a reference to system dot configuration. If you're a seasoned dot developer, you're probably already very familiar with this 'cause I intend to use configuration manager. So, for that I needed that reference. Now, let's start writing some real code here. First, all those values that I specified in the dot config, let's reference them here. So those are right up here, and based on that, I also went ahead and created an authority. This is just like the code you've seen so far. So string dot format, invariant culture, add instance and tenant, consistent calculation of the two. Next, let's start filling out the logic and my static void main. So first I'll wait for the user to hit enter. And next, I need to authenticate and therefore get that auth resulting through which I can get the access token. Remember how we did it in the web app, there was a retry loop three times. Yes, that's exactly what I'm going to do here, as well. So I said auth context dot acquire token async for a given resource. That's the service resource you and I have for Microsoft graph. Different web api's will have different resource uri's, Pass in the client credential, there is no user identity here. So it's client id and app key. And then hopefully you get the auth result, but if you don't, we put some gating logic here just to make sure that the authentication did go through. And if your code has gotten this far, then at this point, you would have gotten an auth result. And through the auth result you can get an access token. So now I simply need to make https call, which I'm going to put in a separate method. And that method looks like this. So I create a new http client, I get the access token, then I make the call to slash users. Remember, this is something that we had granted access for my web application to be able to call. So there you go. If you're creating a new web application, you'll have to grant that access again. That's important. And again, if we do get a success code, we just say console dot write line, otherwise, we say access denied. So, let me go ahead and put a break point here so we can see what results come back, and I will also go ahead and put a break point here just to make sure that we do have an access token. And with that, let's go ahead and run this application. So it says press enter to start. Okay, I'm pressing enter now. And looks like we're able to authenticate and we have a nice looking access token. You can decrypt it. You'll find that it does not contain any user identity. This is just like the web app example I mentioned earlier. I'm going to continue and jump to the next break point. So, at this point we should have written out the results of the call. And as you can see that we were able to successfully call Microsoft graph through a demon. And this concludes the various scenarios you can use Microsoft graph in. At this point, you should feel confident of writing any kind of application targeting any kind of Microsoft graph api.

Contents