'Not able to create google calendar event using service account - getting domain wide delegation error
Anyone please help me on this further.
Problem Statement: We are not able to create event on google calendar with implicit authentication method using service account. We can list the event using this service account but it throws below error while we create to add an event in other user’s google calendar with in the same domain. We already have enabled domain wide delegation and followed all the steps as mentioned above.
Unhandled Exception: Google.GoogleApiException: 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] ]
Below is the code:
enter code here
class Program
{
static void Main(string[] args)
{
CalendarService _service = GetCalendarService(@"C:\Users\anups\source\repos\gcal\gcalfinal-5fb1e194aac1.json");
CreateEvent(_service);
//GetEvents(_service);
}
private static void CreateEvent(CalendarService _service)
{
Event body = new Event();
EventAttendee a = new EventAttendee();
a.Email = "***@gc-lms.com";
List<EventAttendee> attendes = new List<EventAttendee>();
attendes.Add(a);
body.Attendees = attendes;
EventDateTime start = new EventDateTime();
start.DateTime = Convert.ToDateTime("2022-01-15T09:00:00+0530");
EventDateTime end = new EventDateTime();
end.DateTime = Convert.ToDateTime("2022-01-15T11:00:00+0530");
body.Start = start;
body.End = end;
body.Location = "Avengers Mansion";
body.Summary = "Discussion about new Spidey suit";
EventsResource.InsertRequest request = new EventsResource.InsertRequest(_service, body, "c_13*********@group.calendar.google.com");
Event response = request.Execute();
}
private static void GetEvents(CalendarService _service)
{
// Define parameters of request.
EventsResource.ListRequest request = _service.Events.List("c_13*********@group.calendar.google.com");
request.TimeMin = DateTime.Now.AddDays(-15);
request.ShowDeleted = false;
request.SingleEvents = true;
request.MaxResults = 10;
request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;
string eventsValue = "";
// List events.
Events events = request.Execute();
eventsValue = "Upcoming events:\n";
if (events.Items != null && events.Items.Count > 0)
{
foreach (var eventItem in events.Items)
{
string when = eventItem.Start.DateTime.ToString();
if (String.IsNullOrEmpty(when))
{
when = eventItem.Start.Date;
}
eventsValue += string.Format("{0} ({1})", eventItem.Summary, when) + "\n";
}
Console.WriteLine(eventsValue);
}
else
{
Console.WriteLine("No upcoming events found.");
}
}
private static CalendarService GetCalendarService(string keyfilepath)
{
try
{
string[] Scopes = {
CalendarService.Scope.Calendar,
CalendarService.Scope.CalendarEvents,
CalendarService.Scope.CalendarEventsReadonly
};
GoogleCredential credential;
using (var stream = new FileStream(keyfilepath, FileMode.Open, FileAccess.Read))
{
// As we are using admin SDK, we need to still impersonate user who has admin access
// https://developers.google.com/admin-sdk/directory/v1/guides/delegation
//
credential = GoogleCredential.FromStream(stream)
.CreateScoped(Scopes).CreateWithUser("****.iam.gserviceaccount.com");
//.Impersonate(new ImpersonatedCredential.Initializer("***@gc-lms.com"));
}
// Create Calendar API service.
var service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "gcalfinal",
});
return service;
}
catch (Exception ex)
{
throw;
}
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
