Google Calendar API batch insert events with Apps Script

蹲街弑〆低调 提交于 2021-02-05 07:52:05

问题


Using Google Apps Script's UrlFetchApp, how can I use the Google Calendar v3 API to insert events in batches?

Google lists this example batch request, but I don't understand how exactly to convert it.

POST /batch/farm/v1 HTTP/1.1
Authorization: Bearer your_auth_token
Host: www.googleapis.com
Content-Type: multipart/mixed; boundary=batch_foobarbaz
Content-Length: total_content_length

--batch_foobarbaz
Content-Type: application/http
Content-ID: <item1:12930812@barnyard.example.com>

GET /farm/v1/animals/pony

--batch_foobarbaz
Content-Type: application/http
Content-ID: <item2:12930812@barnyard.example.com>

PUT /farm/v1/animals/sheep
Content-Type: application/json
Content-Length: part_content_length
If-Match: "etag/sheep"

{
  "animalName": "sheep",
  "animalAge": "5"
  "peltColor": "green",
}

--batch_foobarbaz
Content-Type: application/http
Content-ID: <item3:12930812@barnyard.example.com>

GET /farm/v1/animals
If-None-Match: "etag/animals"

--batch_foobarbaz--

回答1:


I believe your goal as follows.

  • You want to insert the several events to Google Calendar using the batch request with Google Apps Script.

In this case, in the current stage, it is required to create the request body as shown in your question. The sample request body can be seen at the official document. This has already been mentioned in your question.

When the request body is created and request it, the script becomes as follows.

Sample script:

Before you use this script, please enable Calendar API at Advanced Google services.

function myFunction() {
  const calendarId = "###";  // Please set the calendar ID.
  
  // Please set the object for inserting the calendar events. Each request body can be seen at https://developers.google.com/calendar/v3/reference/events/insert
  const events = [
    {start: {date: "2021-01-21"}, end: {date: "2021-01-21"},summary: "sample event 1"},
    {start: {date: "2021-01-22"}, end: {date: "2021-01-22"},summary: "sample event 2"}
  ];

  const boundary = "xxxxxxxxxx";
  const payload = events.reduce((s, e, i, a) => s += `Content-Type: application/http\r\n` +
    `Content-ID: ${i}\r\n\r\n` +
    `POST https://www.googleapis.com/calendar/v3/calendars/${calendarId}/events\r\n` +
    `Content-Type: application/json; charset=utf-8\r\n\r\n` +
    `${JSON.stringify(e)}\r\n` +
    `--${boundary + (i == a.length - 1 ? "--" : "")}\r\n`
  , `--${boundary}\r\n`);
  const params = {
    method: "post",
    contentType: "multipart/mixed; boundary=" + boundary,
    payload: payload,
    headers: {Authorization: `Bearer ${ScriptApp.getOAuthToken()}`},
    muteHttpExceptions: true,
  };
  const res = UrlFetchApp.fetch("https://www.googleapis.com/batch/calendar/v3", params);
  console.log(res)

  // CalendarApp.getCalendarById();  // This is used for automatically detecting the scope of https://www.googleapis.com/auth/calendar
}
  • In this sample script, the sample request body is used at events. So please modify it for your actual situation.

Note:

  • In the current stage, the batch request can run the 100 requests by one API call. So when you requests more than 100 using above script, please modify it. Please be careful this.

  • In this case, I thought that when the request body is created, it might be a bit complicate. So I created this as a Google Apps Script library. When this library is used, above script becomes as follows. In this library, even when the number of requests is more than 100, this can be run by processing in the internal library.

      const calendarId = "###";  // Please set the calendar ID.
      const requests = {
        batchPath: "batch/calendar/v3",
        requests: [
          {
            method: "POST",
            endpoint: `https://www.googleapis.com/calendar/v3/calendars/${calendarId}/events`,
            requestBody: {start: {date: "2021-01-21"}, end: {date: "2021-01-21"},summary: "sample event 1"},
          },
          {
            method: "POST",
            endpoint: `https://www.googleapis.com/calendar/v3/calendars/${calendarId}/events`,
            requestBody: {start: {date: "2021-01-22"}, end: {date: "2021-01-22"},summary: "sample event 2"},
          }
        ]
      };
      const res = BatchRequest.EDo(requests);
      console.log(res);
    

References:

  • Sending Batch Requests
  • Events: insert
  • BatchRequest
    • This is a library for running Batch Requests using Google Apps Script (GAS).


来源:https://stackoverflow.com/questions/65802629/google-calendar-api-batch-insert-events-with-apps-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!