...

Source file src/google.golang.org/api/examples/calendar.go

Documentation: google.golang.org/api/examples

     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  
     5  package main
     6  
     7  import (
     8  	"fmt"
     9  	"log"
    10  	"net/http"
    11  	"os"
    12  
    13  	calendar "google.golang.org/api/calendar/v3"
    14  )
    15  
    16  func init() {
    17  	registerDemo("calendar", calendar.CalendarScope, calendarMain)
    18  }
    19  
    20  // calendarMain is an example that demonstrates calling the Calendar API.
    21  // Its purpose is to test out the ability to get maps of struct objects.
    22  //
    23  // Example usage:
    24  //
    25  //	go build -o go-api-demo *.go
    26  //	go-api-demo -clientid="my-clientid" -secret="my-secret" calendar
    27  func calendarMain(client *http.Client, argv []string) {
    28  	if len(argv) != 0 {
    29  		fmt.Fprintln(os.Stderr, "Usage: calendar")
    30  		return
    31  	}
    32  
    33  	svc, err := calendar.New(client)
    34  	if err != nil {
    35  		log.Fatalf("Unable to create Calendar service: %v", err)
    36  	}
    37  
    38  	c, err := svc.Colors.Get().Do()
    39  	if err != nil {
    40  		log.Fatalf("Unable to retrieve calendar colors: %v", err)
    41  	}
    42  
    43  	log.Printf("Kind of colors: %v", c.Kind)
    44  	log.Printf("Colors last updated: %v", c.Updated)
    45  
    46  	for k, v := range c.Calendar {
    47  		log.Printf("Calendar[%v]: Background=%v, Foreground=%v", k, v.Background, v.Foreground)
    48  	}
    49  
    50  	for k, v := range c.Event {
    51  		log.Printf("Event[%v]: Background=%v, Foreground=%v", k, v.Background, v.Foreground)
    52  	}
    53  
    54  	listRes, err := svc.CalendarList.List().Fields("items/id").Do()
    55  	if err != nil {
    56  		log.Fatalf("Unable to retrieve list of calendars: %v", err)
    57  	}
    58  	for _, v := range listRes.Items {
    59  		log.Printf("Calendar ID: %v\n", v.Id)
    60  	}
    61  
    62  	if len(listRes.Items) > 0 {
    63  		id := listRes.Items[0].Id
    64  		res, err := svc.Events.List(id).Fields("items(updated,summary)", "summary", "nextPageToken").Do()
    65  		if err != nil {
    66  			log.Fatalf("Unable to retrieve calendar events list: %v", err)
    67  		}
    68  		for _, v := range res.Items {
    69  			log.Printf("Calendar ID %q event: %v: %q\n", id, v.Updated, v.Summary)
    70  		}
    71  		log.Printf("Calendar ID %q Summary: %v\n", id, res.Summary)
    72  		log.Printf("Calendar ID %q next page token: %v\n", id, res.NextPageToken)
    73  	}
    74  }
    75  

View as plain text