I am using the calendar client library with a service account and I can not get the CalendarList.List().Do to work as expected. If I use a direct HTTP request it works as expected. While I can move forward from this point with the work around but I would like to know if I am making a mistake in my implementation or is it a problem with the library code. Any assistance is appreciated. The code follows:
package main
import (
"io/ioutil"
"log"
"fmt"
"golang.org/x/oauth2/google"
"golang.org/x/oauth2"
"google.golang.org/api/calendar/v3"
)
func main() {
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 = "jmore@tele-metron.com"
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 ",bodyString)
fmt.Printf("The original request body \n%v\n ",resp.Request)
// THE PROBLEM AREA //
// when using this method I get nothing returned. It is using the same client to get a calendar service and I can use the calendar service
// to get a specific calendar CalendarList.Get("calendar id").Do() so the credentials are OK
srv, err := calendar.New(client)
if err != nil {
log.Fatalf("Unable to retrieve calendar Client %v", err)
}
calendars, err := srv.CalendarList.List().Fields("*").Context(oauth2.NoContext).Do()
if err != nil {
log.Fatalf("Unable to retrieve calendar list %v", err)
}
fmt.Printf("Calendars %v\n",calendars)
}