显示谷歌日历事件,作为服务帐号进行身份验证 – Golang 应用程序

huangapple go评论81阅读模式
英文:

Display google calendar events authenticating as service account - Golang App

问题

在我的Go应用程序中,目前在我的开发机器上作为本地主机运行(最终在Linode上运行),我希望能够简单地显示我自己Google账户中一个日历中的公共Google日历事件。目前,这只在控制台上显示,因为我想在原则上使其工作,并且我在应用程序的索引页面处理程序中有以下代码:

googleCalendar, err := calendar.NewService(r.Context(), option.WithCredentialsFile("./googleAuth.json"))
if err != nil {
    app.errorLog.Println(w, err)
}

calEvents, err := googleCalendar.Events.List("primary").TimeMin(time.Now().Format(time.RFC3339)).MaxResults(5).Do()
if err != nil {
    app.errorLog.Println(err)
}

if len(calEvents.Items) > 0 {
    for _, i := range calEvents.Items {
        fmt.Fprintln(w, i.Summary, " ", i.Start.DateTime)
    }
} else {
    app.errorLog.Println("No calendar events to display")
}

我认为唯一的方法是使用服务帐号,因为我不是G Workspace(GSuite)用户(该应用程序最终将在Linode上运行),直接使用Oauth2过于繁琐,并且在我的情况下可能不可用。我已经在我的Google Dev控制台中启用了Google日历API,并设置了一个服务帐号,创建/下载了一个JSON密钥文件(如代码中所示)。我已将服务帐号电子邮件添加为必要日历的共享用户。代码可以编译和运行,但是我只得到了"No calendar events to display",因为'Items'为空(日历中确实有事件)。这里是否还需要做更多的工作?如何获取这些日历事件?谢谢。

英文:

Within my Go app, currently running as localhost on my dev machine (ultimately on a Linode), I wish to simply display public Google Calendar events from a calendar in my own Google account. At the moment this only displays to console because I want to get it working in principle and I have this code in the index page handler of my app.

googleCalendar, err := calendar.NewService(r.Context(), option.WithCredentialsFile("./googleAuth.json"))
    if err != nil {
    	app.errorLog.Println(w, err)
    }
    
    calEvents, err := googleCalendar.Events.List("primary").TimeMin(time.Now().Format(time.RFC3339)).MaxResults(5).Do()
    if err != nil {
    	app.errorLog.Println(err)
    }
    
    if len(calEvents.Items) > 0 {
    	for _, i := range calEvents.Items {
    		fmt.Fprintln(w, i.Summary, " ", i.Start.DateTime)
    	}
    } else {
    	app.errorLog.Println("No calendar events to display")
    }

The only way I think to do this is to use a service account because I am not a G Workspace (GSuite) user (the app will finally run on a Linode), Oauth2 directly is overkill and likely not available in my circumstances anyhow. I have enabled Google calendar API in my Google Dev Console and setup a service account, created / downloaded a json key file (as in the code). I have added the service account email as a shared user to the necessary calendar. The code compiles and runs but I only get my "No calendar events to display" because 'Items' is empty (the calendar does have events). Is there more here that needs to be done ? How to fetch these calendar event ? thanks

答案1

得分: 1

好的,以下是翻译好的内容:

嗯,我终于找到了解决这个问题的方法。我发布的代码在这种情况下是所需的全部内容,但是关键字"primary"的使用不起作用。我不知道为什么,根据Google的文档,它应该起作用。我只是用实际的日历ID(而不是日历名称)替换了"primary",然后我的所有事件都显示出来了。😄

英文:

Well, I finally found a solution to this issue. The code I posted is all that's needed in this case BUT it's the use of the keyword "primary" which is not working. I do not know why, it should work according to the Google documentation. I simply substituted "primary" with the actual calendar ID (not the calendar name) and all my events displayed. 🙄

huangapple
  • 本文由 发表于 2022年6月28日 21:12:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/72787092.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定