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://learn.microsoft.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;