猿问

Google 日历与 Google App Engine 的集成

我在 Google 帐户 A 上设置并运行了 Google App Engine 服务。我想做的是在我的企业的 Google 日历帐户 B 上预订活动。我将此作为一般指南。以下是我为此执行的步骤(注意:AppEngine 已经在运行并且可以正常工作。

  1. 在我的项目中为 Google 帐户 A 启用日历 API

  2. 转到我帐户 B 的管理控制台,然后转到安全性 -> 高级设置 -> 管理 API 客户端访问权限,并将范围授权https://www.googleapis.com/auth/calendar给我的帐户 A 中我的 App Engine 的服务 ID。

  3. Go中的以下代码

注意:在 中getCalendarService,我在这个repo 的README之后使用ADC获取凭据

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)

}

当我查看日志和错误报告时,没有错误,并且日志显示以下消息:


已创建事件:日历 URL


但是当我复制链接并转到我的 Google 帐户 B 并加载它时,Google 日历显示“找不到请求的事件”。值得一提的是,如果我将 url 加载到 Account A 中,它会说同样的事情。


由于某种原因没有错误,但该事件不起作用。我认为这不是我的代码中的问题,而是凭据中的问题,但我可能是错的。


三国纷争
浏览 99回答 1
1回答

慕姐4208626

如果我没有记错的话,您使用服务帐户创建日历事件并将此事件插入到服务帐户的主日历中这可能不是您想要的,相反,如果您想将事件插入到您的主日历中您需要将此日历的 id 指定为calendarId而不是主要的 - 这意味着您事先与服务帐户共享您的日历或者,您使用impersonation,即构建服务帐户服务,使其充当您(或您的域的另一个用户指定为ServiceAccountUser
随时随地看视频慕课网APP

相关分类

Go
我要回答