Skip to content
Advertisement

can’t fetch msgraph data using vanilla js

I am trying to pull data from the endpoint https://graph.microsoft.com/v1.0/me/. I have done this using python but I can’t seem to fetch data from Microsoft Graph using vanilla js.

When I attempt to perform a fetch request. I get a 200 response but nothing is inside the response object.

Here is the fetch code:

fetch("https://graph.microsoft.com/v1.0/me/", {
  method: "GET",
  "headers": {
    "authorization": "Bearer ENTERTOKENHERE"}
}).then(data =>{console.log(data)});

I get a response of:

Response {type: 'cors', url: 'https://graph.microsoft.com/v1.0/me/', redirected: false, status: 200, ok: true, …}

but I am expecting more of a response like the one I get from the https://developer.microsoft.com/en-us/graph/graph-explorer website like this:

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users/$entity",
    "businessPhones": [],
    "displayName": "Edd Bighead",
    "givenName": "Edd",
    "jobTitle": null,
    "mail": "Edd@conglomo.onmicrosoft.com",
    "mobilePhone": null,
    "officeLocation": null,
    "preferredLanguage": "en-US",
    "surname": "Bighead",
    "userPrincipalName": "Edd@conglomo.com",
    "id": "2fa321d9-bda3-41c1-8be8-5d4049ed8765"
}

Is there anything I am missing to get the data from msgraph using vanilla js only?

Advertisement

Answer

I figured it out – I needed to jsonize the data then print that data. Can’t believe I missed that. lol

fetch("https://graph.microsoft.com/v1.0/me/", {
  method: "GET",
  "headers": {
    "authorization": "Bearer ENTERTOKENHERE"}
}).then(response => response.json())
.then(data => {console.log(data)})
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement