0

I have a Node.js back which currently is running on Firebase in the form of cloud functions. Except for that, I also have an Azure Active Directory with some users that I have invited

enter image description here

So, I want to be able to access them from the Node.js get a list of their emails and names. From what I understood, I can achieve that by making a reference to Microsoft's Graph API and more specifically to their Users API. As every request to Azure AD needs to be OAuth2 authenticated, I was wondering what is the best way of achieving that in my situation. What client flow do I need to implement? I am currently focused on the one which is based on client credentials.

Thanks in advance and whatever general suggestion are more than welcome!

user2128702
  • 2,059
  • 2
  • 29
  • 74
  • Have you tried using Microsoft Graph API + authorization code flow? I think this is more suitable for you. – Carl Zhao Aug 20 '20 at 08:22

1 Answers1

0

This issue gets Access token and calls Microsoft Graph API using node.js.

The user API of Azure Active Directory Graph API is no longer updating. This MS graph API is newer.

Get access token using client credentials flow:

const request = require("request");

const endpoint = "https://login.microsoftonline.com/[Tenant]/oauth2/v2.0/token";
const requestParams = {
    grant_type: "client_credentials",
    client_id: "[ApplicationID]",
    client_secret: "[Key]",
    scope: "https://graph.microsoft.com/.default"
};

request.post({ url:endpoint, form: requestParams }, function (err, response, body) {
    if (err) {
        console.log("error");
    }
    else {
        console.log("Body=" + body);
        let parsedBody = JSON.parse(body);         
        if (parsedBody.error_description) {
            console.log("Error=" + parsedBody.error_description);
        }
        else {
            console.log("Access Token=" + parsedBody.access_token);
        }
    }
});

Call MS Graph API:

function testListGroupGraphAPI(accessToken) {
    request.get({
        url:"https://graph.microsoft.com/v1.0/users",
        headers: {
          "Authorization": "Bearer " + accessToken
        }
    }, function(err, response, body) {
        console.log(body);
    });
}
unknown
  • 6,778
  • 1
  • 5
  • 14