简体   繁体   English

缺少结束时间 - Google 日历 API 调用以插入事件

[英]Missing End Time - Google Calendar API call to insert event

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.我已经尝试过在请求中使用和不使用 calendarId 和资源,以及在开始/结束对象中使用和不使用时区。 Everything else I've searched draws a blank.我搜索过的其他所有内容都是空白。

function testRequest(){

  var service = getDWDS('Calendar: ', 'https://www.googleapis.com/auth/calendar', 'user@example.com');

  var requestBody = 
    {
      calendarId: "user@example.com",
      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谢谢

In your script, I thought that the script for requesting Calendar API is required to be modified.在你的脚本中,我认为请求日历 API 的脚本需要修改。 And, in your script, 2 requests are done.并且,在您的脚本中,完成了 2 个请求。 So, how about the following modification?那么,下面的修改呢?

In this modification, your testRequest() is modified.在这个修改中,你的testRequest()被修改了。

Modified script:修改脚本:

function testRequest() {
  var service = getDWDS('Calendar: ', 'https://www.googleapis.com/auth/calendar', 'user@example.com');
  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 .在此修改中,它假设您的service.getAccessToken()访问令牌是用于请求'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:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google日历API更新“缺少结束时间” - Google calendar API update “Missing end time” 使用nodejs谷歌日历API插入事件会返回400:“缺少结束时间” - Inserting an event with the nodejs google calendar API returns 400: “Missing end time” Node.js - Google 日历 API 在插入事件时显示错误“缺少结束时间” - Node.js - Google Calendar API showing error "Missing end time" when inserting an event 缺少结束时间 Google 日历 - Missing End Time Google Calendar 如何在事件 object 中为谷歌日历 api 设置开始和结束时间 - How to set start and end time in event object for google calendar api Google 日历 Api 插入事件返回错误“开始时间无效” - Google Calendar Api insert event return error “Invalid start time” 将Google Calendar事件结束时间添加到脚本中 - Adding Google Calendar event end time to script 未经授权的Google Calendar API插入事件 - Google Calendar API Insert event without authorization 为什么 Google Calendar API 会比我使用 Node.js 提供的日期/时间早 4 小时插入我的活动? - Why does Google Calendar API insert my event 4 hours earlier than the date/time that I've given it using Node.js? 插入Google Calendar API v3可使时间提前1小时 - Insert into google calendar API v3 puts time 1 hour ahead
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM