This repository is quickstart on integrating AD OAuth v2 authentication flow, utilising REST interface, to access information stored in the Microsoft Graph. This sample uses the App-only auth flow where there is no need for a user to be involved in the flow. This is great for APIs, background workers, etc.
This sample uses v2, but if you're not sure whether to use v1 or v2, see this article here.
- Node.js is required to run the app and to install dependencies.
- Office 365 Developer Account
-
Follow the first step listed here on Register your app to register your app.
-
Configure permissions as per the screenshot below. Read more here for background on permissions:
-
Add Web platform under Platforms on the app registration page, as per the screenshot below. Don't forget to save your changes at the end of the page.
-
Get administrator consent - inorder to access information without a user being involved, you need to get permissions from the admin user. Make the below REST GET request to get admin access on a dev/test O365 tenant. Alternatively you can replace the values and paste the below URL into a browser address bar too.
// Line breaks are for legibility only.
// "state" is just a added variable for continunity in case you have multiple requests orginiating from the same app.
// "redirect_uri" can be anything you like as long as it matches the value from the previous step.
GET https://login.microsoftonline.com/{TENANT_ID}/adminconsent
?client_id={get Application Id from App Registration page from Step #1}
&state=12345
&redirect_uri=https://google.com
Where to find your {TENANT_ID}?
You can find your Tenant ID by navigating to to the Azure Portal here.
Here is an example of the GET request respresenting the values you will have to fill in based on the code snippet above:
https://login.microsoftonline.com/c8f54d43-a05e-48f4-afeb-0028c5c865bb/adminconsent?client_id=6731de76-14a6-49ae-97bc-6eba6914391e&state=12345&redirect_uri=http://google.com
- Rename
.env.template
file to.env
and enter your authentication values- Replace ENTER_YOUR_APPLICATION_ID, ENTER_APPLICATION_PASSWORD and TENANT_ID with you values.
- Run
npm i
to install all dependencies frompackage.json
- Run
npm start
to run the app - Browse to
http://localhost:8080/ping
or use Postman to test the URL. You should get a response back with the bodyHealthy Ping!
. - Browse to
http://localhost:8080/auth
and you should get a JSON response back with an array of users includingid
anddisplayName
.