如何使用google-api-go-client将日程安排到Google日历?

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

How to set the schedule to Google Calendar from google-api-go-client?

问题

我想要使用google-api-go-client来设置Google日历的日程安排。

我已经厌倦了使用Google日历应用程序来设置日程 如何使用google-api-go-client将日程安排到Google日历?

有没有示例代码?

英文:

I wanna set the schedule to google calendar from google-api-go-client.

I got tired set the schedule with google calendar application 如何使用google-api-go-client将日程安排到Google日历?

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)
}

huangapple
  • 本文由 发表于 2017年4月21日 13:30:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/43534606.html
匿名

发表评论

匿名网友

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

确定