question

ZenHuzaini-6550 avatar image
0 Votes"
ZenHuzaini-6550 asked FionaMatu-8876 edited

Miscrosoft graph api Error: Request failed with status code 401

Hi,

I am new to ms graph api. I am learning this API and I followed this tutorial https://docs.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-calendar
appp.png (43.8 KiB)
capture.png (23.4 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@ZenHuzaini-6550 You seem to be accessing the getSchedule API. Change the request to '/users/{id|userPrincipalName}/calendar/getSchedule' as in the getSchedule documentation then let me know how it goes


0 Votes 0 ·

0 Answers