googleapis / google-api-nodejs-client

Google's officially supported Node.js client library for accessing Google APIs. Support for authorization and authentication with OAuth 2.0, API Keys and JWT (Service Tokens) is included.
https://googleapis.dev/nodejs/googleapis/latest/
Apache License 2.0
11.38k stars 1.91k forks source link

Calendar: Invalid Value When Setting accessRole in requestBody of calendar.calendarList.insert #3339

Open kashif-ghafoor opened 1 year ago

kashif-ghafoor commented 1 year ago

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

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("c_dke9ue52o12rl6hjtdfjqnn044@group.calendar.google.com", {
  email: "dev.test01@company.com",
  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":"c_dke9ue52o12rl6hjtdfjqnn044@group.calendar.google.com","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' } ]
}