英文:
How to set the schedule to Google Calendar from google-api-go-client?
问题
我想要使用google-api-go-client来设置Google日历的日程安排。
我已经厌倦了使用Google日历应用程序来设置日程
有没有示例代码?
英文:
I wanna set the schedule to google calendar from google-api-go-client.
I got tired set the schedule with google calendar application
Is there any sample?
答案1
得分: 1
你可以使用Google Calendar API的快速入门指南。详细信息请参考https://developers.google.com/google-apps/calendar/quickstart/go。
当你想要创建事件时,可以使用"Events: insert"。你可以在这里查看详细信息。
看起来你住在日本。因此,在使用示例脚本时,请注意"DateTime"和"TimeZone"。
如果你使用上述两个示例,"main()"函数将如下所示。在运行示例脚本之前,请确认Google Calendar API在Google API控制台中是否已启用。"DateTime"和"TimeZone"是为日本设置的,请查阅上述文档网站获取详细信息。
脚本:
func main() {
ctx := context.Background()
b, err := ioutil.ReadFile("client_secret.json")
if err != nil {
log.Fatalf("Unable to read client secret file: %v", err)
}
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/calendar-go-quickstart.json
config, err := google.ConfigFromJSON(b, calendar.CalendarScope)
if err != nil {
log.Fatalf("Unable to parse client secret file to config: %v", err)
}
client := getClient(ctx, config)
srv, err := calendar.New(client)
if err != nil {
log.Fatalf("Unable to retrieve calendar Client %v", err)
}
event := &calendar.Event{
Summary: "Sample event",
Location: "Sample location",
Description: "This is a sample event.",
Start: &calendar.EventDateTime{
DateTime: "2017-04-22T00:00:00+09:00",
TimeZone: "Asia/Tokyo",
},
End: &calendar.EventDateTime{
DateTime: "2017-04-22T01:00:00+09:00",
TimeZone: "Asia/Tokyo",
},
}
calendarID := "#####"
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)
}
希望对你有帮助!
英文:
You can use quicksstart for Google Calendar API. The detail information is https://developers.google.com/google-apps/calendar/quickstart/go.
And when you want to create events, you can use "Events: insert". You can see the detail here.
It seems that you live in Japan. So when you use sample script, please be careful for DateTime
and TimeZone
.
If you use above both samples, main()
becomes as follows. Before run sample script, please confirm whether Google Calendar API is enabled at Google API console. DateTime
and TimeZone
are for Japan. Please check above document sites about the detail.
Script :
func main() {
ctx := context.Background()
b, err := ioutil.ReadFile("client_secret.json")
if err != nil {
log.Fatalf("Unable to read client secret file: %v", err)
}
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/calendar-go-quickstart.json
config, err := google.ConfigFromJSON(b, calendar.CalendarScope)
if err != nil {
log.Fatalf("Unable to parse client secret file to config: %v", err)
}
client := getClient(ctx, config)
srv, err := calendar.New(client)
if err != nil {
log.Fatalf("Unable to retrieve calendar Client %v", err)
}
event := &calendar.Event{
Summary: "Sample event",
Location: "Sample location",
Description: "This is a sample event.",
Start: &calendar.EventDateTime{
DateTime: "2017-04-22T00:00:00+09:00",
TimeZone: "Asia/Tokyo",
},
End: &calendar.EventDateTime{
DateTime: "2017-04-22T01:00:00+09:00",
TimeZone: "Asia/Tokyo",
},
}
calendarID := "#####"
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)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论