Search code examples
node.jsoauth-2.0microsoft-graph-apimicrosoft-teamsmicrosoft-graph-teams

Graph api Request token without user


I'm trying to use the graph API to eventually automatically create a Microsoft Teams meeting where you can join with the URL. But before I can do this I need to get a bearer token. Because I want to create a meeting without the use of a user I need to use this documentation: https://learn.microsoft.com/en-us/graph/auth-v2-service. I've followed this documentation and first tried something in postman but that failed so I tried to do it in node.js. This my code now:

const fetch = require('node-fetch');
fetch("https://login.microsoftonline.com/f8cdef31-a31e-4b4a-93e4-5f571e91255a/oauth2/v2.0/token HTTP/1.1", {
    method: "POST",
    body: JSON.stringify({"client_id":"CLIENT_ID",
    "scope":"https%3A%2F%2Fgraph.microsoft.com%2F.default",
"client_secret":"CLIENT_SECRET",
"grant_type":"client_credentials"
})}).then((reposone)=>{
    console.log(reposone)
}
);

I'm not sure what would be confidential, so I just changed the client_id and client_secret to words. I got the client_id from Azure at the overview tab in the register application next to the "Application (client) id". And the client_secret is from the certificate & secrets tab where I generate a secret. If I execute this code with the values inserted I get a 404 response, so I don't see what I'm exactly doing wrong. I'm guessing my URL is wrong but I have no idea what else it could be. I got the tenant id in the url from the overview tab next to "Directory (tenant) ID".


Solution

  • It turns out there are a lot of things I did wrong. First the URL is "https://login.microsoftonline.com/f8cdef31-a31e-4b4a-93e4-5f571e91255a/oauth2/v2.0/token" and not with the HTTP/1.1 added. Secondly, my active directory configuration was wrong. I needed to recreate it so that I was an admin. Lastly, I needed to configure my application to receive other accounts than just Microsoft account or else I could not give application permissions for my API.