3
votes

We have created a Service Account in Google and Using Calendar API for adding events, it worked fine before ,after google stopped the account and reactivated it , it didn't work

We have tried new service account and debugged line by line of our code and no error and also returning created events but not showing in calendar

        CalendarService service = null;
        var serviceAccountCredentialFilePath = 
        Path.Combine(AppDomain.CurrentDomain.BaseDirectory, 
        "ServiceAccount_Key.json");
        if (Path.GetExtension(serviceAccountCredentialFilePath).ToLower() 
        == ".json")
        {
            GoogleCredential credential;

            string[] scopes = new string[] {
                CalendarService.Scope.Calendar, // Manage your calendars
                CalendarService.Scope.CalendarReadonly // View your Calendars
             };
            using (var stream = new FileStream(serviceAccountCredentialFilePath, FileMode.Open, FileAccess.Read))
            {
                credential = GoogleCredential.FromStream(stream)
                     .CreateScoped(scopes);
            }
            // Create the service.
            service = new CalendarService(new BaseClientService.Initializer
            {
                HttpClientInitializer = credential,
                ApplicationName = "Esoco"
            });
        }
        // End //

        // Insert Event //
        var myEvent = new Event
        {
            Id = guid,
            //Transparency =  "OPAQUE",
            Summary = subject,
            Location = location,
            Start = new EventDateTime { TimeZone = timezone, DateTime = start },
            End = new EventDateTime { TimeZone = timezone, DateTime = end },
            Attendees = objattendees,
            Organizer = organizerdata,
            Description = summary,

        };

        myEvent.Start.DateTimeRaw = myEvent.Start.DateTimeRaw.Replace("Z", "");
        myEvent.End.DateTimeRaw = myEvent.End.DateTimeRaw.Replace("Z", "");

        //myEvent.ICalUID

        var recurringEvent = service.Events.Insert(myEvent, "primary");
        recurringEvent.SendNotifications = true;
        try
        {
            var outputofevent = recurringEvent.Execute();
            ScriptManager.RegisterClientScriptBlock(this, typeof(string), "Alertscript", "alert('"+outputofevent.Status+"')", true);
        }

Expected Result is Inserted Event Display in Calendar but Actual Result is Not Displayed in Calendar

2
How do you know its not showing in the calendar? are you doing an event.list to check?DaImTo
Please check Trash (under settings). May be events are cancelled automatically. I am facing same issue.Vishal Panchal
@DaImTo yes its returned as 250Bharath Kumar Jayaraman
Then the event is being inserted. Have you scanned though the events returned by events.list to find the one you are after and done an event.get to see what attendees have been sent?DaImTo
@DaImTo - Yes all the events have my name in attendees and i also executed an event get method by event id and it returned the detailsBharath Kumar Jayaraman

2 Answers

4
votes

In the past few weeks, my team have the same issues, and this is what my team's workaround.

Our scenario

  1. We create a service account called [email protected]
  2. We using service account's credential from 1) for Google Calendar API
  3. Create an event into a service account's calendar like the code below
calendar.events.insert({
  calendarId: 'primary', // it will create an event into service account's calendar
  resource: data,
  sendNotifications: true
})

What we have done for a workaround.

  1. Create a new company's user (let's say [email protected])
  2. Share [email protected]'s calendar with a service account by
    • going to "Calendar > Settings > Share with specific people" then
    • adding [email protected] with "Make changes to events"
  3. Update the code from creating an event to service account's calendar to just-created-user's calendar like a below
calendar.events.insert({
  calendarId: '[email protected]', // << we changed from 'primary' to just-created-user
  resource: data,
  sendNotifications: true
})

After changing it, the created-event will be shown in Google Calendar as what it should.

Hope this helps

2
votes

remember that the following code inserts your events into the service accounts primary calendar

var recurringEvent = service.Events.Insert(myEvent, "primary");

In order to see these events you will need to either do an events.list or invite someone else to the event so that they can see it in their own calendar.

Bug issue logged.

Beyond that there is currently an issue on the forums about invites not being sent https://issuetracker.google.com/issues/140746812