我们可以使用批量请求在Google日历中添加多个事件吗?

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

Should we add multiple events in google calendar using batch request?

问题

我想使用Go语言的Google日历API在Google日历中添加多个事件。我该如何实现这个功能?同时添加多个事件是正确的吗?

我正在使用以下提到的Go语言包向Google日历发送请求:

"google.golang.org/api/calendar/v3"

在for循环中,我正在维护事件数据并使用以下代码发送请求:

service.Events.Insert(calendarId, event).Do()

如果我使用Go协程来发送事件,这样做是否正确?

英文:

I want to add multiple events in Google calendar using google calendar api with go lang. How can I achieve this? Is it correct to add multiple events simultaneously?

I am using the below mentioned pkg of golang to send request to google calendar.

	"google.golang.org/api/calendar/v3"

In for loop, I am maintaining the event data and send the request using:

 service.Events.Insert(calendarId, event).Do()

Would it be correct, if we I use go routine to send events?

答案1

得分: 2

要创建事件,请调用events.insert()方法,并提供以下参数:

  • calendarId是日历标识符,可以是要创建事件的日历的电子邮件地址,也可以是特殊关键字'primary',它将使用已登录用户的主要日历。如果您不知道要使用的日历的电子邮件地址,可以在Google日历Web界面的日历设置中检查它(在“日历地址”部分),或者您可以在calendarList.list()调用的结果中查找它。
  • event是要创建的事件,包含所有必要的详细信息,如开始时间和结束时间。唯一两个必填字段是开始时间和结束时间。

为了成功创建事件,您需要:

  • 将OAuth范围设置为https://www.googleapis.com/auth/calendar
  • 确保经过身份验证的用户对提供的calendarId具有写入访问权限(例如,通过调用calendarList.get()来获取calendarId并检查accessRole)。

以下是示例代码:

calendarId := "primary"
event, err := srv.Events.Insert(calendarId, event).Do()
if err != nil {
  log.Fatalf("Unable to create event. %v\n", err)
}
fmt.Printf("Event created: %s\n", event.HtmlLink)

根据您的问题,您正在询问如何使用批量请求添加多个事件。

据我所知,Google日历API只提供逐个添加事件的选项。

您还可以参考此页面获取更多详细信息:
https://developers.google.com/calendar/api/guides/create-events

英文:

To create an event, call the events.insert() method providing at least these parameters:

calendarId is the calendar identifier and can either be the email address of the calendar on which to create the event or a special keyword 'primary' which will use the primary calendar of the logged in user. If you don't know the email address of the calendar you would like to use, you can check it either in the calendar's settings of the Google Calendar web UI (in the section "Calendar Address") or you can look for it in the result of the calendarList.list() call.
event is the event to create with all the necessary details such as start and end. The only two required fields are the start and end times.

In order to successfully create events, you need to:

set your OAuth scope to https://www.googleapis.com/auth/calendar.
ensure the authenticated user has write access to the calendar with the calendarId you provided (for example by calling calendarList.get() for the calendarId and checking the accessRole).

calendarId := "primary"
event, err = srv.Events.Insert(calendarId, event).Do()
if err != nil {
  log.Fatalf("Unable to create event. %v\n", err)
}
fmt.Printf("Event created: %s\n", event.HtmlLink)

As per your question you are asking about adding multiple events using batch request.

Google calendar API only has the event adding one by one option in its option as far as I know.

You can also refer to this page for more details
https://developers.google.com/calendar/api/guides/create-events.

huangapple
  • 本文由 发表于 2022年11月7日 13:57:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/74342483.html
匿名

发表评论

匿名网友

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

确定