PATCH calendar GAPI error: Request contains an invalid argument

PATCH calendar GAPI error: Request contains an invalid argument

我在使用带有日历 GAPI 的 PATCH 请求时遇到问题。当我向请求提供正文时,出现错误 400:请求包含无效参数。

 code: 400,
 errors: [
   {
     message: 'Request contains an invalid argument.',
     domain: 'global',
     reason: 'invalidArgument'
   }
 ]

如果我不向请求添加正文,请求有效,我收到状态 200,事件的数据如预期的那样未更改,所以我认为身份验证工作正常,问题仅与请求正文。

这是我的代码的简化版本。任何帮助将不胜感激! :)

 const updatedEvent = {
   summary: 'My new title',
 };
 const jsonBody = JSON.stringify(updatedEvent);

 // Set JWT and PATCH modification on user calendar
 const oauth2Client = new JWT({
   subject: SERVICE_ACCOUNT_EMAIL,
   keyFile: GOOGLE_APPLICATION_CREDENTIALS,
   scopes: ['https://www.googleapis.com/auth/calendar',
   'https://www.googleapis.com/auth/calendar.events']
 });
 const url = `https://www.googleapis.com/calendar/v3/calendars/${calendarID}/events/${calendarEventId}?alt=json`;
 await oauth2Client.request({
   url,
   method: 'PATCH',
   body: jsonBody,
 })
 .then((res) => {
   console.log(res);
 })
 .catch((error) => {
   console.log(error); 
   throw Error('GAPI PATCH request error')
 });

修复: 使用 npm googleapis 库的工作实施

import { google } from 'googleapis';
...
await google.calendar('v3').events.patch({
  auth: oauth2Client,
  calendarId: calendarId,
  eventId: calendarEventId,
  requestBody: {
    summary: 'New title',
  }
})

应该是resource而不是body

出于测试目的以获得正确的语法,您可以

  1. 使用 Try this API
  2. 测试请求
  3. 在展开并选择 JAVASCRIPT 时,按照 Try this API 的建议使用 gapi 客户端测试请求 ` 在你的情况下:
    return gapi.client.calendar.events.patch({
      "calendarId": "XXX",
      "eventId": "YYY",
      "alt": "json",
      "resource": {
        "summary": "My new title"
      }
    })

一旦你的语法正确,你就可以使用 URL.

将其传输到请求中