Azure Functions and Microsoft Graph
Good afternoon, so. I, did, Matthew Henderson I'm a program, manager on the Azure functions, and azure app service team focusing. On identity and integration stories and today. We're going to talk about using Azure functions and the Microsoft graph together quick show of hands how many folks are familiar with Azure functions, or service development all, right about half how. Many folks are familiar with the Microsoft graph. Okay. Also about half was that the same half, Wolfie. Will figure it out, so I'll kind of go quickly through some of the review, content, but, that's. Gonna be the first part of it we'll just kind of walk through what's actually in the graph why would I want to use it how, do I even get started and then how do we get start with Azure functions what is the benefit of this serverless thing that everybody keeps talking about and then, we'll put them together and we'll figure out how to build a very very. Reliable. Scalable application. That's working against our very, important data that's. All sound good, cool. So. The. Microsoft graph heard, quite a bit about it already, it built so far the. Idea is that this is where all of your data from Microsoft applications, lives this is where everything in office lists this is where all the things that you're doing with your enterprise. Mobility suite, all. Of these things feed, into the graph and provide, you with a rich set of data and insights. On top of that data, there's. A whole set of different api's all contained, within this one endpoint and that's, really powerful right because your one-stop shop for everything Microsoft, they. Are constantly, updating, all of the different api's that are available in, fact there were just a bunch of new user, management, API so you can do user onboarding, in, a ad so, those are all new so, the graph is a really really powerful tool because you know again if you're gonna be doing anything where you're providing, a business application or something like that go. To where the data is and. The. Important thing is, that we're gonna actually start you start building an application that works with that data and that's something that we can deliver as you, know a third, party software offering, at, something we could do for our own internal companies it's something we could do just for our own data but. The. Graph, provides the actual entry point to it all and then, you get all the nice benefits that come with it great, all of the security, management, that we put in that Azure Active Directory all the insights all of the you know actual. Webhook responsiveness. That comes with the, graph is yours for the taking, so. Promise. To be quick so we're gonna keep going. Adric. Functions we keep hearing this service term and it's a weird term because it turns out in Azure, we. Don't just buy an empty warehouse and you, know say that's our data center no, servers right we, actually.
The Point is that we abstract away the servers, from it for you so you're not worrying about managing, them scaling, them keeping them up to date and things like that the. Platform just takes care of all of it and with. That you, get a lot of freedom to really focus on your code, it's. Also event-driven, and what that tends to mean is that you're, not actually because you're not managing a server if you're. Not getting any events, we. Don't spend anything up for you you don't pay anything and we. Only charge you for the amount of time your code spins running this is incredibly powerful I've. Seen folks. Post actually it was a SharePoint developer posted, over a hundred fold cost reduction, by moving his infrastructure, over to service that's. Powerful. When. We talk about azor functions specifically, we're. Talking about taking a set of events right weird, reading event-driven that can be anything right, HTTP, something, in Azure like uploading something to a blob store a timer or an, event from the Microsoft graph. Then. We're gonna run a piece of code and that code could be in a number of languages dotnet. Obviously, very popular here this week but, also no js' we have Java support pythons on the way plenty. Of others and then, you, know you don't just react to data you're also able to influence, other parts of your system you're able to go right out to different data stores. For. Something in the cosmos TV, send. A notification through, notification, hubs those, types of things so. Again, we're, gonna have this, these events they're, coming in as certain, amount of data that's bound we, have the code that actually executes and we have bindings, to other data sources. Y'all. Want to see it in action. So. Let's, very quickly. Create. Our first function and reload the portal because I left, it hanging oh. No. Oh no. Network, issues. This. Was not planned let's. Jump over to demo Wi-Fi. Yeah. I've got an adapter sorry. Folks. Hey, that looks promising okay let's try again. Maybe. So, we're gonna start in Portal Eliezer calm. As. We do with many things in Azure. And. We're gonna hope it loads logging. In that's good sign. Excuse. Me while I vamp. Not, sure what's going on. Do, it one more try on another tab. So, what. I was gonna be showing it was first creating, an initial function we're, gonna actually set it up so that we're gonna use a blob, store so we're going to react to a. Image. Being uploaded to a blob and that's gonna set some context for how we work with bindings, and how you actually get started creating functions so, I'll. Navigate into the function that I've already created so. We've. Have a function app that's just a collection of functions that share some common code or config and then. While. We wait for that to spin we'll, go ahead and hit new and we have a whole set of templates for all sorts of different data sources and inner interactions. And functions I'm gonna use blob trigger, and I'm gonna use C sharp because that's what I'm most comfortable with it, asks for some parameters, right where, do you want this to actually be I'm gonna copy this over so, we can actually upload our image in a minute and, you notice the name is actually being treated as a parameter sorry, let me make sure folks on the back and see that real quick so. The name is actually being treated as a parameter, so that we can actually pull that data into our function and.
Then We, just have a connection string which functions, has one by default so we're good to go go, ahead and create that real quick and then, all. Of a sudden now we have a service function so it's gonna have a piece of C sharp that's the code and then the configuration, for those events so. The. Portal is being really slow apologies, there but. You. Know this is just how we go. Ahead and start in the portal you do not have to use the portal to work with functions that just happens to be what we're doing with today but, you can work in Visual Studio vs, code whatever your favorite editor of choice is so. Here's, my function it's. Pretty straightforward all, we're gonna do is take. A look at a blob. And we're actually breeding that in as a stream we'll talk a bit more about that later and, then we're getting that name parameter, that we talked about and all we're gonna do is we're gonna log it we're gonna log the name of the function and how I'm, sorry the name of the blob and how, big it is so. What's, under the covers happening is we have, this. Configuration and this configuration is actually, represented, by a. JSON. File you. Don't need to worry about that though we have a nice little editor here to make things simple and it's all the same parameters we saw when we created so, I now have a function. I'm gonna go ahead and pull up the log so that we can see when it actually runs and then. I'm gonna use the azure storage Explorer if you, working with Azure storage at all I'm, gonna go and refresh this just because the network if, you're working with Azure storage at all you, need to check out the azure storage Explorer it makes things so much better so, we go ahead here, and we're gonna go and see our blob. Containers, and you're actually gonna see some stuff that's actually been created already. But. I'm gonna go ahead and create a new container we're gonna call it samples work items because that's what our function expects. We, ran wait for that and then, I have all, my favorite serverless product, logos and we're. Gonna upload one of those and we should see some logs show up. Now. One, of the things I want you to notice is in our, function we didn't have to write any code to work with storage, that's, the power of bindings, and natural functions, so.
The Idea is a, we see that we are an we see that we have, the. Name. Of the function is functions underscore color PNG, and then we have the size, of it there in the logs so. I. Just. Got that data as a, stream, right. But, I don't have to treat it just as a stream right if. I'm using a strong type language like C sharp we actually have the ability to bring it in as any data form and that's gonna be important when we start talking about the graph so. Now. You just saved that real quick and upload. Another one and it works right I switched to a byte array that. Happens to have the same size. Property I go, ahead and upload another one function. Doesn't care didn't, have to change any configure anything like that right, so we have a lot of flexibility, here and we're, not having to write storage code or learn how that SDK, works it's important. So. We. Learned about bindings, web. Settings switching machines. So. Now, let's. Talk about Microsoft, graph bindings, so. These, are currently in preview and the, idea is that we want to actually make it so that you can do all the same types of things replace blob with onedrive right, so. There's, a whole set of web hooks which provide triggers for your functions and then we also have bindings, to let you just pull in data or write, data back out and you, know it's a whole host of api's we, don't cover everything and that's important to note if you, need to work with api's that don't have explicit bindings, and as your functions we have a token binding and that's what we'll start with. And we'll go ahead and start, building you know we can start working with ApS but we can extract away all the authentication, trouble, right you, don't have to worry about all that auth code to start working with the Microsoft graph so. I, promise. The slides would be quick, let's. Keep going so. We'll jump back over to our function. And. We're go ahead and create a new one using the Microsoft graph. So. We actually have a whole section of templates for this if. I go into the scenario tab up here and choose, ms, graph we. Actually have a collection that around web books we'll save that for later but, then we also have this one that's using, the profile photo API now. We don't actually have a mining for the profile photo but this template will show you kind of how things get working now. Normally, when you should first open this you, would get a prompt that says you need to install an extension I'm skipping, that just to save time because we only have a few precious minutes together but, you'll also notice I'm now getting a warning and what does that warning say it, says hey you, haven't configured everything with authentication, how. Somebody had actually talked to the Microsoft, graph right we need an ad or MSA identity, so. Communally. There's a button here that just says configure ad now so I'm gonna click it. We. Wait briefly. We're. Talking to ad behind the scenes making sure everything happens and tada all of a sudden might have is good to go so. We're, not ready to start actually working with graph data I'll, go ahead and create this function and. We'll wait again for the portal to kind of get itself together but the, idea here is that I'm, actually just gonna be getting you'll see a graph token, passed in as a parameter I'm, just gonna attach that to an HTTP request that I make to a graph endpoint and the nice. Thing there is again. I'm, not doing any auth code myself I'm just getting the token attaching, it to a request so this lets me talk to any API, in the Microsoft graph regardless, of whether or not the binding has an explicit it.
Has An explicit binding, for it. I'm. Sorry about the delay in the portal here today folks but, let's, just jump around a little bit and see if we can that there it is so, you. Can see that I'm actually getting an HTTP request message, and returning a response right, so I'm actually just treating this as an API and that's important right because a lot of the applications that we want to be building with the Microsoft graph are things that we want people to be able to interact with and work with their data right so, I need to be able to have the sort of request response mode where somebody's interacting, with it but. Then all, we do is we, spin up an HTTP client we. Add, the authorization, header just, to include that token and then, we call this in point so. Let's. Take a look again at how that configuration, looks so the template helped us skip some of the steps but. We. Have by, default the, HTTP, requests, in response and then. We also have this graph token binding. Right. So. We'll. Focus on that so. Again we see all the aad config that we needed before and. We have a choice of identity, so. Right, now I can see that I'm, using user from HTTP, request again we want something interactive I want to work with the person who's communicating, with my API I could also choose to have my application have, its own identity that's. The client credentials flow the user from ID and user from token are more advanced scenarios we're, not time to go into those but for the most part you don't need them and. Then we give it the name that shows up in our function as and we. Tell it the resource we want and in this case Microsoft, graph. Ok, so, let's. Go back and run, this function but. Wait you say, he. Hasn't logged in you're. Exactly correct we. Need to actually do that and so we're actually going to work with edge just, to get all of our. Our. Endpoints. Handled and we'll keep the ad session live so, what I'm going to do is I'm going to call this in point I'm, gonna call this. Slash dot off slash login slash AED endpoint that comes on your function at when you configure a ad, syndication. With it you, also see a query string parameter prompt equals consent we're, gonna be using that a lot because we're gonna be changing permissions, and every, time you change permissions on your user you have to get them to consent to the new Scopes, that they're authorizing, right. So, when, I go and actually, login. So, I not seen Irma password because I've already am already in the session but the, application is now saying hey are you okay with us signing and reading your profile and of course I say yes. So. Then. We're logged in there's, plenty of stuffing but this is I actually did a full session, yesterday that talks about sort of how these authentication, pieces, work but. Then we'll go ahead and actually, grab. The end point so, I need. To copy the URL of this specific function and again, this function all it's doing is grabbing my profile photo from a ad whoops. Skill, that tab. Grabbing. It from a ad and then. Returning. It to us so with any luck what we should see oh hey. We get to debug live. So. We, got it HTTP 500, let's see why that might have been so. First thing we want to do always is check our logs and it. Looks like. Aha.
Data. Like. That so. We can see it's all the same data that we had we can see that that excel input demo was in progress we don't have time to change it but if, you do make those changes there's a little bit of a propagation delay but you can still see, those changes coming in live what. You really want though is to be able to react to things and that's where web hooks come in so, we'll do that one really quickly, and. Apologies I know we're at time so we're slightly. Over so, in this case I've, got a function that is taking in a message it's actually using the message type from the Microsoft graph SDK, fantastic. So now I can do things like check the subject make sure that we're only gonna react things that come in with this subject and from me because hey demos, and. Then I'm just gonna log the body of the message right, so the, trick is that I, actually need, a web. Hook creator. Right because in the graph I don't, have web hooks natively happening, right the graph doesn't know that it needs to let my that function, respond. To me getting an email so, we're gonna go and do is create an MS graph web. Hook creator, there's also refreshing. And deleting which are very important, for real applications, right. A subscription. From, the graph webhooks will. Only last for, three days and, so you want to be able to refresh that, one. Of the things that we'll see sorry. The portal just kind of hiccup there we'll. Go and do the web Book Creator and what. You'll see is I need now to be able to read user mail so in order to subscribe. To a web book you have to have read permissions on the entity you're reacting to we'll, go in and add those now. I'm. Gonna hit create. Okay. And so, all this is going to be doing is registering. A new subscription and then it knows to call back into functions and then the function that we already saw will, be ready to handle it. And. We'll see that email come through faster than the sport of loads. Hopefully. Okay. At. Least I can grab the URL. Now. We did just change permissions so one more time, we're. Gonna go ahead and do that. Login consent we should see read your mail come up. Yep. Read user mail, accept. So. We'll be logged in and we, see this function all it's doing is just going. In calling into the graph and creating, a new registration, that's. That's, what this, binding. Does we'll. Jump very quickly to our handlers so we can see the logs. And. We've. Successfully signed in we're gonna go ahead and create the subscription, by, running that function oh. Well. Since. I am over time I might not be able to troubleshoot this but so quickly my. Guess is that, we. Actually were a little bit faster than aad which. Can't happen. So. But. No it failed because. Client. State coroner. Yeah, okay well apologies, there because, we're running low, on time if. Folks would like to just follow me back to the azure functions, booth I'm glad to provide that dimmer there I'll be there for the rest of the day as well so you just come say hey you owe me a demo I'll be glad to give it to you very. Quickly. We'll. Just wrap up what we covered so. There's. A lot of power in the Microsoft graph there's a lot of power and Azure functions if, you haven't been using Azure today we, actually have, a free. No credit card required trial great way to get started and. Then these bindings let you focus on your app logic right we didn't have to write a bunch of code to go read, things from onedrive we'd have to learn a 1 Drive SDK or anything like that it just worked and, you, know we use the graph Explorer in combination, of that token API and we can cover anything that's in a Microsoft graph so, thank you very much there's plenty of great other sessions please evaluate and. Thanks. Very much appreciate sorry.