Golang日历库事件提醒功能实现指南
Golang日历库事件提醒功能实现指南
我正在尝试覆盖事件提醒。事件提醒结构中的设置 UseDefault: false 似乎没有效果。仅发送包含覆盖设置的提醒结构会导致以下错误:
{“conferenceData”:{“createRequest”:{“conferenceSolutionKey”:{“type”:“hangoutsMeet”},“requestId”:“rrrr”}},“description”:“创建笔记的链接,预约下次会议的链接。HTML格式的说明”,“end”:{“dateTime”:“2018-11-01T21:00:00-04:00”,“timeZone”:“America/Toronto”},“reminders”:{“overrides”:[{“method”:“email”,“minutes”:120},{“method”:“email”,“minutes”:90},{“method”:“popup”,“minutes”:30}]},“start”:{“dateTime”:“2018-11-01T20:00:00-04:00”,“timeZone”:“America/Toronto”},“summary”:“客户名称 服务类型”}2018/10/31 08:14:27 错误 googleapi: 错误 400: 不能同时指定默认提醒和覆盖设置。, cannotUseDefaultRemindersAndSpecifyOverride
我知道这与 googleapi、MarshalJSON 和设置 ForceSendFields 有关,但似乎没有任何方法有效。使用 API 资源管理器时操作简单且工作正常。
更多关于Golang日历库事件提醒功能实现指南的实战教程也可以访问 https://www.itying.com/category-94-b0.html
找到了!!!库本身没有问题 问题出在,一如既往地:用户身上 ::
newEvent.ForceSendFields = []string{“UseDefault”}
应该是
newEvent.Reminders.ForceSendFields = []string{"UseDefault"}
使用 ForceSend 不是我每天都会用到的功能(自从我开始使用 Go 不到六个月以来,这是第二次使用),而且它存在于大多数库结构中,所以请务必小心。
希望这对其他人有所帮助
更多关于Golang日历库事件提醒功能实现指南的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
在使用Google Calendar API时,当您设置自定义提醒覆盖(overrides)时,必须确保不包含默认提醒设置。错误信息明确指出不能同时指定默认提醒和覆盖设置。
问题在于即使您设置了UseDefault: false,API可能仍将其视为默认提醒存在。以下是正确的实现方式:
package main
import (
"context"
"log"
"google.golang.org/api/calendar/v3"
"google.golang.org/api/option"
)
func main() {
ctx := context.Background()
// 初始化日历服务
srv, err := calendar.NewService(ctx, option.WithCredentialsFile("credentials.json"))
if err != nil {
log.Fatalf("无法创建日历服务: %v", err)
}
// 创建事件
event := &calendar.Event{
Summary: "客户名称 服务类型",
Description: "创建笔记的链接,预约下次会议的链接。HTML格式的说明",
Start: &calendar.EventDateTime{
DateTime: "2018-11-01T20:00:00-04:00",
TimeZone: "America/Toronto",
},
End: &calendar.EventDateTime{
DateTime: "2018-11-01T21:00:00-04:00",
TimeZone: "America/Toronto",
},
ConferenceData: &calendar.ConferenceData{
CreateRequest: &calendar.CreateConferenceRequest{
ConferenceSolutionKey: &calendar.ConferenceSolutionKey{
Type: "hangoutsMeet",
},
RequestId: "rrrr",
},
},
Reminders: &calendar.EventReminders{
Overrides: []*calendar.EventReminder{
{
Method: "email",
Minutes: 120,
},
{
Method: "email",
Minutes: 90,
},
{
Method: "popup",
Minutes: 30,
},
},
// 不设置UseDefault字段,或者明确设置为nil
UseDefault: false,
ForceSendFields: []string{"UseDefault"},
},
}
// 插入事件
_, err = srv.Events.Insert("primary", event).Do()
if err != nil {
log.Fatalf("无法创建事件: %v", err)
}
log.Println("事件创建成功")
}
关键点:
- 当使用
Overrides时,不要设置UseDefault: true - 使用
ForceSendFields确保UseDefault字段被显式发送 - 或者,完全省略
UseDefault字段:
Reminders: &calendar.EventReminders{
Overrides: []*calendar.EventReminder{
{
Method: "email",
Minutes: 120,
},
{
Method: "email",
Minutes: 90,
},
{
Method: "popup",
Minutes: 30,
},
},
// 不包含UseDefault字段
},
如果问题仍然存在,可以尝试使用NullFields来显式设置字段为null:
Reminders: &calendar.EventReminders{
Overrides: []*calendar.EventReminder{
{
Method: "email",
Minutes: 120,
},
{
Method: "email",
Minutes: 90,
},
{
Method: "popup",
Minutes: 30,
},
},
NullFields: []string{"UseDefault"},
},
这样可以确保API正确处理提醒设置,避免默认提醒和覆盖设置的冲突。

