google-api-nodejs-client
google-api-nodejs-client copied to clipboard
Calendar: Invalid Value When Setting accessRole in requestBody of calendar.calendarList.insert
I have a service account with domain-wide delegation enabled for Google Calendar. I'm trying to add a calendar to a user's calendar list within the domain while specifying a specific role for access.
Issue
When I include the accessRole property in the requestBody of the calendar.calendarList.Insert method, I receive an "Invalid Value" response error. Without specifying accessRole, the calendar gets added to the user's list with the default role of "reader."
In this api reference of nodejs client library(), accessRole property is mentioned in requestBody. Moreover, intelligence in vscode also provides an option for accessRole in the requestBody of insert method.
Environment details
- OS: Ubuntu 22.04
- Node.js version: v18.14.0
- npm version: 9.3.1
googleapisversion: 118.0.0
Steps to reproduce
import { google } from "googleapis";
import { JWT } from "google-auth-library";
const privateKey = process.env.GOOGLE_PRIVATE_KEY;
const googleClientEmail = process.env.GOOGLE_CLIENT_EMAIL;
const googlePrivateKey = privateKey.replace(/\\n/g, "\n");
const calendar = google.calendar("v3");
async function authorizeClient(email, serviceAccountEmail, privateKey, scopes) {
const jwtClient = new JWT(serviceAccountEmail, undefined, privateKey, scopes, email);
try {
await jwtClient.authorize();
return jwtClient;
} catch (error) {
throw new Error(`Failed to authorize client: ${error.message}`);
}
}
async function shareCalendar(calendarId, user) {
try {
const userJwtClient = await authorizeClient(user.email, googleClientEmail, googlePrivateKey, [
"https://www.googleapis.com/auth/calendar",
]);
const resp = await calendar.calendarList.insert({
auth: userJwtClient,
requestBody: {
id: calendarId,
accessRole: user.role,
},
});
console.log(resp.data);
} catch (error) {
console.log(error);
}
}
shareCalendar("[email protected]", {
email: "[email protected]",
role: "owner",
});
expected Response calendar should be added to calendar list of "dev test01" user.
Actual Response
response: {
config: {
url: 'https://www.googleapis.com/calendar/v3/users/me/calendarList',
method: 'POST',
params: {},
validateStatus: [Function (anonymous)],
retry: true,
body: '{"id":"[email protected]","accessRole":"owner"}',
responseType: 'json', },
data: { error: [Object] },
status: 400,
statusText: 'Bad Request',
request: {
responseURL: 'https://www.googleapis.com/calendar/v3/users/me/calendarList'
}
},
code: 400,
errors: [ { domain: 'global', reason: 'invalid', message: 'Invalid Value' } ]
}