How to access a shared outlook calendar in personal accounts

devarshi sanghani 0 Reputation points
2024-07-03T13:53:17.47+00:00

I want to access the personal calendar by a person a@domain.com that is shared with me in my personal outlook calendar. I followed the code for making a graph client and accessing a shared a shared calendar that is given here. https://video2.skills-academy.com/en-us/graph/outlook-get-shared-events-calendars
and here is the relevent code for my function calls:
I know there should be a part for refreshing access tokens, it is there, and works works fine, i have just omitted it.
Error : Error: GraphError: The requested user 'abhishek@gmail.com' is invalid. at new GraphError
statusCode: 404, code: 'ErrorInvalidUser',

const { Client } = require("@microsoft/microsoft-graph-client");
require("isomorphic-fetch");
const connect = require('../configs/db-config')
const BotToken = require("../models/BotToken");
const httpStatus = require("http-status");
const msg = require("../constants/messages");
const axios = require("axios");


async function getInterviewerCalendarDetails(options) {
    console.log("authEmail", options.coordinatorEmail);
    console.log("targetEmail", options.interviewerEmail);
    await connect();
    const authEmail = options.coordinatorEmail;

    const botToken = await BotToken.findOne({
        email: authEmail,
    });

    if (!botToken) {
        console.log("bot token issue")
        throw {
            statusCode: httpStatus.UNAUTHORIZED,
            message: msg.USER_NOT_AUTHENTICATED,
        };
    }
    
    const microsoftToken = botToken.tokens.find(token => token.serviceName === "Microsoft");
    if (!microsoftToken) {
        console.log("microsoft token issue")
        throw {
            statusCode: httpStatus.UNAUTHORIZED,
            message: msg.USER_NOT_AUTHENTICATED,
        };
    }

    let accessToken = microsoftToken.accessToken;

    try {
        const client = Client.init({
            authProvider: (done) => {
                done(null, accessToken);
            },
        });
      
        // Make an API request to list the calendar events
        const response = await client
            .api(`/users/${options.interviewerEmail}/calendar/events`)
               
            .get();

        const events = response.value;
        console.log(events)
        return events;
    } catch (error) {
        console.log("error issue")
        console.error('Error:', error);
        throw {
            statusCode: httpStatus.UNAUTHORIZED,
            message: msg.USER_NOT_AUTHENTICATED,
        };
    }
}

const options = {
    coordinatorEmail: "devarshi@gmail.com",
    interviewerEmail: "abhishek@gmail.com",
};

getInterviewerCalendarDetails(options)
    .catch(error => {
        console.error('Error fetching calendar details:', error);
    });

module.exports = getInterviewerCalendarDetails;
Outlook
Outlook
A family of Microsoft email and calendar products.
3,403 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,392 questions
Microsoft Teams Development
Microsoft Teams Development
Microsoft Teams: A Microsoft customizable chat-based workspace.Development: The process of researching, productizing, and refining new or existing technologies.
3,053 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yakun Huang-MSFT 2,335 Reputation points Microsoft Vendor
    2024-07-04T05:41:38.1566667+00:00

    Hi @devarshi sanghani

    When accessing someone else's mailbox, you need to provide a userId or userPrincipalName rather than the address of the individual mailbox。

    Screenshot 2024-07-04 133955

    GET https://graph.microsoft.com/v1.0/users/{userId | userPrincipalName}/calendar/events
    

    Hope this helps.

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