All right. We are going to get started.
I am richard a principle engineer of microsoft.
We work with partners that are building commercial grade
Software that they are selling across a number of the technologies.
My area of focus is graph.
I work the iot solutions that connect to things like the graph.
So when we look at really the first thing to look at the
Challenges of working with the graph o on the iot device.
What is the problem that the devices have related to off?
Anyone have an idea? i have special swag for someone
That is willing to answer. What is your --
[Speaker off mic] >> i wouldn't say cpu.
It is amazing what the small devices from the cpu standpoint.
Do you have any idea? ui.
You win an actual speaker hood hoodie.
Here you go. So, yeah, i think that the big
Thing no browser or key board.
So the graph is secured by azure directory.
It is required for a delegated flow, what is what you are going
To on an iot device. It is required to have a browser
Of some sort. You can go with a client
Credential flow. You have to distribute that with
A secret. You don't want to distribute
That on a device, like a consumer type of thing, someone
Could get in and get the secret and perform operations.
You want to stick with a delegated type of flow, but
These devices don't have a browser. A lot of times they don't have a
Key board. You have left with two
Approaches. One is the companion app.
This is common. That could be a website that you
Go to. It could be a mobile device that you have.
You sign in there and that companion app manages the token cash.
Until time i'm calming into the graph i deal with getting the
Token and that companion device manages the process of getting
The token and storing it and refreshing it and sending it out
To an actual iot device that needs in a sinc process or pairing process.
You are seeing that commonly. The other popular approach is
You don't want to build a companion device it is a device
Code flow. You are going to typically
Output a random code on the screen, you are going to direct
The useer to go to a specific site and enter in the code.
After that, they are asked to sign in and consent to things
That you want to do with the api and then the device is
Essentially is waiting for a token to come back from that
Service. One of the great things is that
As your active directory provides a device code
Infrastructure, right now, it is only available in azure
Directory authentication libraries and a newer version
Call calling
Calling -- you are not familiar with the v 1 and v 2, it is not
Important right now. For the device code you can use
It for either. One thing to point out is that
The device code flow written today is not supporting the
Consumer. It is more of
Enterprise. It is not that you couldn't
Build a device code flow. You could. You would have to build it on
Your own. In this case, i am building it
From scratch in a few minutes and do it in five minutes.
It is one of the easy authentication flows.
So much so that people over using that more when they shouldn't.
Those are the two options. Companion app or device code
Flow. The example is youtube.
It is an example of a device code flow.
Go to this u rl and enter in this code and the process has
Them sign and pass the tokens behind the scenes.
Hopefully you have seen this out in the wild.
It is pretty popular. What i will show you when we get
Into the code is with directory, when we do the device code flow,
We give you a friendly message or raw code and you can treat it
Like you want. Going back one screen here,
There's devices here that have zero display, like camera, and
So, in some cases, you might have to get creative on how to
Do that. In alexia make speak.
Hey, here is the code or send them to some url that might
December play that. So with that, i'm going to jump
Over and we are going to do some coding from scratch.
So i will do this in node. Or actually, type script with node.
And i think here in just about five or eight minutes we'll on
Iot app written. The first thing that i have
Done, this is a bare bones project. All i have done ahead of time is
Pull in a few. I have 8 node and a few what we
Call graph sdks. I could just use the pure rest.
This is make it more strongly tight. So the first thing that i need
To do, i have already registered an application.
That is the app id. What i will do first of all is includes.
What i will do here, i have a quick snip it, standard.
Importing those libraries that i have already pulled in there mpn.
I am setting oun authentication context.
This is a snip it for this. I want a new authentication
Context and passing into this an authority.
Whether i am saying this is the authority for azure directory.
This is just saying do i want to validate the authority.
You can set that to true if you want. So if i wanted this to be single
Tenant for the organization, but
In this case, common makes it to any organization could use that application.
So now that i have my authentication context, it is a
Two-step process for the device code flow.
First acquire a user code. And in here, it is going to want
To resource. What am i trying to connect and
What secure resource, in this case, graph.
The next thing that it wants is my client id.
I have that at the top. I did a registration ahead of time.
There is nothing fancy about the registration. So i will go ahead and put that.
The next thing is the language. This is because it outputs a
Message, hey, go to this url. You will see this here.
But for our cases, the only language that i can speak is
English. Finally, there is a call back,
It returns an error, this is type script, or what we hope is
User info. I will say user info.
And this is going to be of type 8h dot user code info. Okay.
Let's just really quickly test this as we go along the way.
We'll do a console log of user
Info info.Message. Pay attention to the console
Here and hopefully we'll get something. Sign in, to sign in, go to a web
Browser, microsoft.Com. That is step one.
We have the code. Now listen to the user that is
Using the code to get a token. We are implementing that part of this.
In this case, we do the second part again with off context.
Acquire token with device code. And so let's see what this takes.
It is taking a resource.
It wants our client id. It wants our user code info.
And then finally, a call back here. So let's put our call back in.
In this case the call back has an error or in this case, it is
Going to be our token. So we'll
Do 8l.Token response. Okay.
Let's see -- actually we are skipping the process and go into
Using the graph client. I will go ahead and initialize a
Graph sdk client. Iot graph client.
You can see i am saying... And injected the token.
So in this case, we say token.Access token.
Api. In this case, maybe go out and
Get my profile information. And then, i will, that's a get.
Perform the verb that you want to do.
Do a get. And that returns and in this
Case error or we'll get actually a user back.
So in this case, it is graph g types
User. And let's do a console log and
See if we are successful of user. So i will put a break point here.
We'll run this one more time. We should have a completed
Really simple iot app. So there we go.
It is hard to see. I will zoom in.
There is the device code. I will copy that.
I will go to a browser. And go to, device log in.
Here is what is really type. When i type in the code, it
Knows what app that code was generated for.
You can see the device flow sample. It is telling me up front what
Application is trying to get information.
Now, i have consented the app once.
If i hadn't done that, hey, this iot device wants this
Information from you. So you are not going to see that here.
It is going to finish. So it is done.
And you can see we have hit the break point.
We have gotten to token back. If i look at the user, it has
All my information from the graph.
My job title, my phone number, all of that.
Pretty simple, right. We did that like in five minutes.
So as a quick follow up here, i
Wanted to provide a few resources, one is the exact
Sample that i built.
Pull down the whole thing. If you want more information
About the microsoft graph, we
Have lots of information. Experts out there. What i added just for fun, it is
A pet project and a lot of people at microsoft, the smart
Mirrors and walk up to the mirror and some sort of facial
Recognition to automatically sign you in.
We built one of those. It is open source.
If you want to play with a smart mirror. You can get one sided mirror
With a monitor. It is a cool example of not only
Leverage the things like the device code flow and the great
Cognitive services. With that, thanks for coming to
The theatre session. I will hang around if you have