Home > Software design >  Missing End Time - Google Calendar API call to insert event
Missing End Time - Google Calendar API call to insert event

Time:12-10

Please help with why the code below returns an error stating the end date time is missing when trying to insert an event on behalf of another user in our domain (using the delegation)...

I can GET data from their calendars, just not POST the event.

I've tried it with and without the calendarId & resource in the request and with and without the timezones in the start/end objects. Everything else I've searched draws a blank.

function testRequest(){

  var service = getDWDS('Calendar: ', 'https://www.googleapis.com/auth/calendar', '[email protected]');

  var requestBody = 
    {
      calendarId: "[email protected]",
      resource: {
        start: {
          dateTime: '2022-12-09T09:00:00Z',
        },
        end: {
          dateTime: '2022-12-09T11:30:00Z',
        },

        summary: "Test Request",
        conferenceData: {
          createRequest: {
            requestId: 'random'
          }
        },
      }
    };
    requestBody.headers            = {'Authorization': 'Bearer '   service.getAccessToken()};
    requestBody.contentType        = "application/json";
    requestBody.method             = "POST";
    requestBody.muteHttpExceptions = false;  

  var url = 'https://www.googleapis.com/calendar/v3/calendars/primary/events?conferenceDataVersion=1&sendUpdates=none';

  Logger.log(requestBody)
  Logger.log(JSON.parse(UrlFetchApp.fetch(url, requestBody)))

  return JSON.parse(UrlFetchApp.fetch(url, requestBody));
}


const OAUTH2_SERVICE_ACCOUNT_PRIVATE_KEY = "KEY GOES HERE"
const OAUTH2_SERVICE_ACCOUNT_CLIENT_EMAIL = 'SERVICE ACCOUNT EMAIL HERE';

function getDWDS(serviceName, scope, email) {

  // Logger.log('starting getDomainWideDelegationService for email: '   email);

  return OAuth2.createService(serviceName   email)
    // Set the endpoint URL.
    .setTokenUrl('https://accounts.google.com/o/oauth2/token')

    // Set the private key and issuer.
    .setPrivateKey(OAUTH2_SERVICE_ACCOUNT_PRIVATE_KEY)
    .setIssuer(OAUTH2_SERVICE_ACCOUNT_CLIENT_EMAIL)

    // Set the name of the user to impersonate. This will only work for
    // Google Apps for Work/EDU accounts whose admin has setup domain-wide
    // delegation:
    // https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority
    .setSubject(email)

    // Set the property store where authorized tokens should be persisted.
    .setPropertyStore(PropertiesService.getScriptProperties())

    // Set the scope. This must match one of the scopes configured during the
    // setup of domain-wide delegation.
    .setScope(scope);

}

Thanks

CodePudding user response:

In your script, I thought that the script for requesting Calendar API is required to be modified. And, in your script, 2 requests are done. So, how about the following modification?

In this modification, your testRequest() is modified.

Modified script:

function testRequest() {
  var service = getDWDS('Calendar: ', 'https://www.googleapis.com/auth/calendar', '[email protected]');
  var requestBody = {
    method: "POST",
    headers: { 'Authorization': 'Bearer '   service.getAccessToken() },
    contentType: "application/json",
    payload: JSON.stringify({
      start: { dateTime: '2022-12-09T09:00:00Z', },
      end: { dateTime: '2022-12-09T11:30:00Z', },
      summary: "Test Request",
      conferenceData: { createRequest: { requestId: 'random' } }
    }),
  };
  var url = 'https://www.googleapis.com/calendar/v3/calendars/primary/events?conferenceDataVersion=1&sendUpdates=none';
  var res = UrlFetchApp.fetch(url, requestBody);
  return JSON.parse(res.getContentText());
}

Note:

  • In this modification, it supposes that your access token of service.getAccessToken() is a valid access token for requesting the endtpoint of 'https://www.googleapis.com/calendar/v3/calendars/primary/events. Please be careful about this. When an error related to the authorization and the permission, please confirm your setting again.

References:

  • Related