You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

74 lines
2.0 KiB

  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package main
  5. import (
  6. "fmt"
  7. "log"
  8. "net/http"
  9. "os"
  10. calendar "google.golang.org/api/calendar/v3"
  11. )
  12. func init() {
  13. registerDemo("calendar", calendar.CalendarScope, calendarMain)
  14. }
  15. // calendarMain is an example that demonstrates calling the Calendar API.
  16. // Its purpose is to test out the ability to get maps of struct objects.
  17. //
  18. // Example usage:
  19. // go build -o go-api-demo *.go
  20. // go-api-demo -clientid="my-clientid" -secret="my-secret" calendar
  21. func calendarMain(client *http.Client, argv []string) {
  22. if len(argv) != 0 {
  23. fmt.Fprintln(os.Stderr, "Usage: calendar")
  24. return
  25. }
  26. svc, err := calendar.New(client)
  27. if err != nil {
  28. log.Fatalf("Unable to create Calendar service: %v", err)
  29. }
  30. c, err := svc.Colors.Get().Do()
  31. if err != nil {
  32. log.Fatalf("Unable to retrieve calendar colors: %v", err)
  33. }
  34. log.Printf("Kind of colors: %v", c.Kind)
  35. log.Printf("Colors last updated: %v", c.Updated)
  36. for k, v := range c.Calendar {
  37. log.Printf("Calendar[%v]: Background=%v, Foreground=%v", k, v.Background, v.Foreground)
  38. }
  39. for k, v := range c.Event {
  40. log.Printf("Event[%v]: Background=%v, Foreground=%v", k, v.Background, v.Foreground)
  41. }
  42. listRes, err := svc.CalendarList.List().Fields("items/id").Do()
  43. if err != nil {
  44. log.Fatalf("Unable to retrieve list of calendars: %v", err)
  45. }
  46. for _, v := range listRes.Items {
  47. log.Printf("Calendar ID: %v\n", v.Id)
  48. }
  49. if len(listRes.Items) > 0 {
  50. id := listRes.Items[0].Id
  51. res, err := svc.Events.List(id).Fields("items(updated,summary)", "summary", "nextPageToken").Do()
  52. if err != nil {
  53. log.Fatalf("Unable to retrieve calendar events list: %v", err)
  54. }
  55. for _, v := range res.Items {
  56. log.Printf("Calendar ID %q event: %v: %q\n", id, v.Updated, v.Summary)
  57. }
  58. log.Printf("Calendar ID %q Summary: %v\n", id, res.Summary)
  59. log.Printf("Calendar ID %q next page token: %v\n", id, res.NextPageToken)
  60. }
  61. }