googleapis / googleapis/google-api-nodejs-client

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

Open
#3,339 0 comments 0 reactions 0 assignees View on GitHub
api: calendar priority: p2 type: bug
Dominant language
TypeScript
Stars
12.2k
Forks
2k
Avg merge
1d 9h
Merged PRs (30d)
24

Description

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](https://googleapis.dev/nodejs/googleapis/latest/calendar/classes/Resource$Calendarlist.html#insert)(), `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
- `googleapis` version: 118.0.0

#### Steps to reproduce
```javascript
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' } ]
}

```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.