Gabe
Gabe

Reputation: 6865

Google Calendar Integration with Google App Engine

I have a Google App Engine Service set up and running on a Google account A. What I would like to be able to do is book events on my business's Google Calendar account B. I followed this as a general guide. Here are the steps I have done to do this (Note: the AppEngine is already running and functional.

  1. Enabled the Calendar API in my Project for Google Account A
  2. Gone to my account B's admin console and went to Security -> Advanced Settings -> Manage API Client access and authorized the scope https://www.googleapis.com/auth/calendar to the id of my service for my App Engine in account A.
  3. Code below in Go

Note: in the getCalendarService, I'm getting the credentials using ADC following this repo's README

func getCalendarService() (*calendar.Service, error) {
    ctx := context.Background()
    return calendar.NewService(ctx, option.WithScopes(calendar.CalendarScope))
}

// code adapted from https://developers.google.com/calendar/create-events/
func bookEvent() {
    srv, err := getCalendarService()
    if err != nil {logAndPrintError(err)}
    event := &calendar.Event{
        Summary: "Test Title",
        Description: "Lorem ipsum",
        Start: &calendar.EventDateTime{
            DateTime: "2020-02-12T09:00:00-07:00",
            TimeZone: "America/Chicago",
        },
        End: &calendar.EventDateTime{
            DateTime: "2020-02-12T17:00:00-07:00",
            TimeZone: "America/Chicago",
        },
    }
    calendarId := "primary"
    event, err = srv.Events.Insert(calendarId, event).Do()
    if err != nil {
        logAndPrintError(err)
    }
    log.Printf("Event created: %s\n", event.HtmlLink)
}

When I look in log and error reporting there is no error, and the logs show this message:

Event created: a Calendar URL

but when I copy the link and go to my Google Account B and load it, Google Calendar says "Could not find the requested event". It is worth mentioning that if I load the url into Account A, it says the same thing.

For some reason there is no error, but the event does not work. I don't think it is a problem in my code but a problem in the credentials, but I could be wrong.


Note: I'm not downloading any keys, I'm using ADC with App Engine to get my credentials.

Upvotes: 0

Views: 276

Answers (1)

ziganotschka
ziganotschka

Reputation: 26796

If I am not mistaken,

you creating a calendar event with a Service account and insert this event into the primary calendar OF THE SERVICE ACCOUNT

This is probably not what you wanted,instead, if you want to insert the event into your primary calendar either

  • You need to specify the id of this calendar as calendarId instead of primary - this implies that your share your calendar with the service account beforehand
  • Or, you use impersonation, that is build the the service account service in such a way that it acts as you (or another user of your domain specified as the ServiceAccountUser

Upvotes: 1

Related Questions