无法使用服务帐户将与会者添加到Google日历

时间:2020-10-03 13:24:58

标签: google-api google-calendar-api

使用Google日历API和服务帐户将与会者添加到我的日历时,我遇到了一些问题。 我的服务帐户在云控制台中具有域范围的委派票证,而且我可以创建/更新活动而无需添加参与者。

当我尝试与参加者发送请求时,我得到以下响应:

Google.Apis.Requests.RequestError
Service accounts cannot invite attendees without Domain-Wide Delegation of Authority. [403]
Errors [Message[Service accounts cannot invite attendees without Domain-Wide Delegation of Authority.] Location[ - ] Reason[forbiddenForServiceAccounts] Domain[calendar]

这是我的代码的样子:

var eventDetails = _calendarService.GetEvent(eventDto.EventId).GetAwaiter().GetResult();
eventDetails.Attendees.Add( new EventAttendee 
   {
      DisplayName = "Test Name",
      Email = "testemail@example.com" // I tried with few emails but results is same
   });
_credential = AuthorizeServiceAccount();
_calendarService = new CalendarService(new BaseClientService.Initializer()
   {
      HttpClientInitializer = _credential,
      ApplicationName = _calendarOptions.AplicationName,
   });
var request = _calendarService.Events.Patch(eventRequest,_calendarOptions.CalendarId, eventId);
var result = await request.ExecuteAsync();
    
public GoogleCredential AuthorizeServiceAccount()
{
    
   var serviceAccountCredential = new ServiceAccountCredential(
      new ServiceAccountCredential.Initializer(_calendarOptions.Id) // I'm using service account email here, tried with clientId but same result
      {
         ProjectId = _calendarOptions.ProjectId,
         KeyId = _calendarOptions.KeyId,
         User = _calendarOptions.User, // I tried with email from request or null value - for both same result
         Scopes = _calendarOptions.Scopes, // ["https://www.googleapis.com/auth/gmail.send", "https://www.googleapis.com/auth/calendar.readonly","https://www.googleapis.com/auth/calendar.events","https://www.googleapis.com/auth/calendar" ],
      }
      .FromPrivateKey(_calendarOptions.Key));
   return GoogleCredential.FromServiceAccountCredential(serviceAccountCredential);
}

可以请您帮我,这样我就可以将参加者添加到我的活动中吗?我不需要向参与者发送电子邮件,但是Google API需要电子邮件。我也不想拥有付费的G Suite域帐户。

0 个答案:

没有答案