I'd like to use this library to interact with the graph API for my AD - https://github.com/microsoftgraph/microsoft-graph-docs/blob/master/concepts/nodejs.md
However, all of the existing javascript libraries I've found to return access tokens expect a return URL to be passed in, as well as some other web-specific stuff, leading me to believe this is some kind of requirement on Microsoft's end.
Is there any good way to authenticate/receive an access token while running a backend node script (nothing web related) so that I can begin to make calls against the Microsoft Graph API? Thanks in advance for the advice.
I had somewhat of an issue for using the url string for the const endpoint
Instead, I passed tenant in this way instead from Microsoft graph api docs:
Reference from docs -> Request an authorization code
To run a back-end non-user-authenticated daemon connected to the Graph API, you want to use the app-only authentication flow. Here's a quick summary of the official steps:
yourtenant.onmicrosoft.com
name, and copy this value down.Azure Active Directory
blade'sApp Registrations
section, not directly within the tenant properties. Copy theApplication ID
; we'll need it later.Save
, and then also hit theGrant Permissions
button.login.microsoftonline.com
domain to obtain an access token.Here's a link to Microsofts Node.js example, and here's a link to the direct documentation on the HTTP call to make to retrieve an access token. And here's a super stripped-down example that will output the retrieved access token. Replace the
[Tenant]
,[ApplicationID]
, and[Key]
values:Once we have the access_token, we can call out to the Graph API. Assuming the apps permissions were configured correctly and applied from step #4, we can start making Graph API requests:
BU0's answer didn't work correctly for me because Microsoft changed their way of using the graph API so I wasn't able to get all the data I needed. Here's how I did it using BU0 answer and this tutorial: