Geting 401 Unauthorised Access when accessing calendar using graph api

Shivam Sood 1 Reputation point
2022-12-29T06:24:29.11+00:00

/**
* Calls the endpoint with authorization bearer token.
* @Paramjeet Dahiya {string} endpoint
* @Paramjeet Dahiya {string} accessToken
*/
async function callApi(endpoint, accessToken) {

const options = {  
    headers: {  
        Authorization: `Bearer ${accessToken}`  
    }  
};  

console.log('request made to web API at: ' + new Date().toString());  

try {  
    const response = await axios.get(endpoint, options);  
    // console.log(`Response`,response.data);  
    const temp = await axios.get(`https://graph.microsoft.com/v1.0/users/${response.data.value[0]['id']}/events`, options);  
    console.log(`temp`,temp);  
    return response.data;  
} catch (error) {  
    console.log(error)  
    return error;  
}  

};

async function temp(){
let temp = await getToken(tokenRequest)
console.log(Temp -->,temp.accessToken);
// let endpoint = 'https://graph.microsoft.com/v1.0/users/20d65521-105b-4ba8-bc47-2429e89cc5b2/calendars'
// let endpoint = "https://graph.microsoft.com/v1.0/users('20d65521-105b-4ba8-bc47-2429e89cc5b2')/calendars"
let endpoint = 'https://graph.microsoft.com/v1.0/users'
console.log(await callApi(endpoint,temp.accessToken))
}

temp()

Using This Code I Am Able To Extract The data from the endpoint /users but when i am passing a user id to /calendar api it showing me 401 unauthorised access error and idea why this is happening . i have also given all the permission related to calendar & user in my application

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

1 answer

Sort by: Most helpful
  1. Gopinath Chennamadhavuni 2,431 Reputation points
    2022-12-29T12:01:09.513+00:00

    Hi @Shivam Sood ,

    Thank you for reaching out to us. As I see you are getting error 401 while performing GET /v1.0/users/{user-id}/calendars to Graph API.

    Error 401 is an Unauthorized error. Make sure that your application is presenting a valid access token to Microsoft Graph as part of the request. This error often means that the access token may be missing in the HTTP authenticate request header or that the token is invalid or has expired.

    We strongly recommend that you use the Microsoft Authentication Library (MSAL) for access token acquisition.

    Also, you can check your scopes by decoding access token in https://jwt.ms/, make sure you have Calendars.Read, Calendars.ReadWrite permission added. Please refer the link for more details.

    For application permission token, the permissions are in the “roles” claim:

    For delegated permission token, the permissions are in the “scp” claim.

    Hope this helps.

    If the answer is helpful, please click Accept Answer and kindly upvote. If you have any further questions about this answer, please click Comment.

    0 comments No comments