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:
JavaScript
x
6
1
fetch("https://graph.microsoft.com/v1.0/me/", {
2
method: "GET",
3
"headers": {
4
"authorization": "Bearer ENTERTOKENHERE"}
5
}).then(data =>{console.log(data)});
6
I get a response of:
JavaScript
1
2
1
Response {type: 'cors', url: 'https://graph.microsoft.com/v1.0/me/', redirected: false, status: 200, ok: true, …}
2
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:
JavaScript
1
15
15
1
{
2
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users/$entity",
3
"businessPhones": [],
4
"displayName": "Edd Bighead",
5
"givenName": "Edd",
6
"jobTitle": null,
7
"mail": "Edd@conglomo.onmicrosoft.com",
8
"mobilePhone": null,
9
"officeLocation": null,
10
"preferredLanguage": "en-US",
11
"surname": "Bighead",
12
"userPrincipalName": "Edd@conglomo.com",
13
"id": "2fa321d9-bda3-41c1-8be8-5d4049ed8765"
14
}
15
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
JavaScript
1
7
1
fetch("https://graph.microsoft.com/v1.0/me/", {
2
method: "GET",
3
"headers": {
4
"authorization": "Bearer ENTERTOKENHERE"}
5
}).then(response => response.json())
6
.then(data => {console.log(data)})
7