Miscrosoft graph api Error: Request failed with status code 401

Zen Huzaini 1 Reputation point
2021-03-16T09:18:06.87+00:00

Hi,

I am new to ms graph api. I am learning this API and I followed this tutorial https://learn.microsoft.com/en-us/azure/active-directory/develop/tutorial-v2-nodejs-console and it works fine for me when retrieving the user.

async function main() {
try {
// here we get an access token
const authResponse = await auth.getToken(auth.tokenRequest);
console.log("get auth reespones ", authResponse)

    const options = {  
        method: 'get',  
        headers: {  
            Authorization: `Bearer ${authResponse}`  
        }  
    };  
    // call the web API with the access token  
    const users = await fetch.callApi(auth.apiConfig.uri, options);  

    console.log("get users ", users)  
} catch (error) {  
    console.log("error here",error);  
}  

};

But I have a problem accessing the calendar API.
here is the new function I use to call the calendar

async function getcalendar() {
try {
// here we get an access token
const authResponse = await auth.getToken(auth.tokenRequest);

    const options = {  
        method: 'post',  
        headers: {  
            Authorization: `Bearer ${authResponse}`,  
            Prefer: `outlook.timezone="Pacific Standard Time"`  
        },  
        data:{          
            "Schedules": ["zen_huzaini@live.com"],  
            "StartTime": {  
                "dateTime": "2021-08-06T09:00:00",  
                "timeZone": "Pacific Standard Time"  
            },  
            "EndTime": {  
                "dateTime": "2021-08-06T18:00:00",  
                "timeZone": "Pacific Standard Time"  
            },  
            "availabilityViewInterval": "15"  
        }  
    }  
    // call the web API with the access token  
    const users = await fetch.callApi('https://graph.microsoft.com/v1.0/me/calendar/getschedule', options);  

    console.log("get users ", users)  
} catch (error) {  
    console.log("error is here ",error);  
}  

};

in my application i already set all the permissions
78195-appp.png

I have no idea why it keeps saying unauthorized.
78221-capture.png

Any help is appreciated.

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,584 questions
{count} votes