Calendar Client CalendarList.List() and Get()
The API works as expected when I request the list using the REST API endpoint directly. It is only when I use the go client library that it does not work. I have also found that using the library to get a specific calendar, results in a return that is equivalent to a call to the API call to https://www.googleapis.com/calendar/v3/calendars/calendarId instead of the expected result from a call to https://www.googleapis.com/calendar/v3/users/me/calendarList/calendarId. In both cases I am using the same credentials so that is not an issue. I have verified the expected results against the API Explorer.
I am using (go version go1.10.1 linux/amd64) and the latest build of the client library.
package main
import (
"fmt"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/calendar/v3"
"io/ioutil"
"log"
"time"
)
func main() {
//var calId = "tele-metron.com_96t0c068qsov8l8ugm99ticsfk@group.calendar.google.com" //service service event calendar
var calId = "tele-metron.com_446opcvere827plrh0gpgo4gqc@group.calendar.google.com" // john more event calendar
//var calId = "[email protected]" // primary calendar
var theFile = "./testingServiceAccount.json"
cred, err := ioutil.ReadFile(theFile)
if err != nil {
log.Fatalf("Unable to read JSON credentials config %v", err)
}
conf, err := google.JWTConfigFromJSON(cred, "https://www.googleapis.com/auth/calendar")
if err != nil {
log.Fatalf("Unable to obtain JWT conf %v", err)
}
conf.Subject = "[email protected]"
conf.Expires = 3600
fmt.Printf("JWT Configuration Using %v private key file \n Email %v\n Private Key\n %s\n Scopes %v\n Token URL %v \n Subject %v\n Expires %v\n", theFile, conf.Email, conf.PrivateKey, conf.Scopes, conf.TokenURL, conf.Subject, conf.Expires)
client := conf.Client(oauth2.NoContext)
// WORKAROUND //
//this call results in the retrieval of the calendar list as expected
resp, err := client.Get("https://www.googleapis.com/calendar/v3/users/me/calendarList")
if err != nil {
log.Fatalf("Unable to retrieve calendar list %v", err)
}
defer resp.Body.Close()
bodyBytes, err := ioutil.ReadAll(resp.Body)
bodyString := string(bodyBytes)
fmt.Printf("The response body \n%v\n\n ", bodyString)
//fmt.Printf("The original request body \n%v\n \n",resp.Request)
// THE PROBLEM AREA //
// when using this method I get nothing returned. It is using the same client as the workaround which is assigned the same credentials
srv, err := calendar.New(client)
if err != nil {
log.Fatalf("Unable to retrieve calendar Client %v", err)
}
calendars, err := srv.CalendarList.List().Context(oauth2.NoContext).Do()
if err != nil {
log.Fatalf("Unable to retrieve calendar Client %v", err)
}
fmt.Printf("\n Calendars Raw %v\n", calendars)
// the Get() call problem
// SECOND WORKAROUND //
//this call results in the retrieval of the specific calendar with the expected result
resp, err = client.Get("https://www.googleapis.com/calendar/v3/users/me/calendarList/" + calId)
if err != nil {
log.Fatalf("Unable to retrieve calendar list %v", err)
}
defer resp.Body.Close()
bodyBytes, err = ioutil.ReadAll(resp.Body)
bodyString = string(bodyBytes)
fmt.Printf("The response body \n%v\n\n ", bodyString)
//fmt.Printf("The original request body \n%v\n \n",resp.Request)
// THE SECOND PROBLEM AREA //
// when using this method I get the results that are equivalent to the https://www.googleapis.com/calendar/v3/calendars/calendarId; which is a limited number of fields
//srv, err := calendar.New(client) //created above
if err != nil {
log.Fatalf("Unable to retrieve calendar Client %v", err)
}
calendar, err := srv.CalendarList.Get(calId).Context(oauth2.NoContext).Do()
if err != nil {
log.Fatalf("Unable to retrieve calendar Client %v", err)
}
fmt.Printf("\n Calendar Raw %v\n", calendar)
fmt.Printf("\n Summary %v \n Id %v \nDescription %v \n Location %v\n timeZone%v\n", calendar.Summary, calendar.Id, calendar.Description, calendar.Location, calendar.TimeZone)
}
Could you try performing a CalendarListGetCall?
Hello! We asked some clarifying questions but haven't heard back for a while. Closing this since it's stale, but please feel free to respond back and continue the conversation.