Golang日历客户端库与服务账户的使用指南
Golang日历客户端库与服务账户的使用指南 我正在使用日历客户端库与服务账户配合,但无法让 CalendarList.List().Do 按预期工作。如果我直接使用 HTTP 请求,它可以正常工作。虽然我可以通过变通方法继续推进,但我想知道是我的实现有问题还是库代码存在问题。任何帮助都将不胜感激。代码如下:
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)
// 变通方法 //
//此调用按预期成功获取日历列表
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)
// 问题区域 //
// 使用此方法时没有返回任何内容。它使用相同的客户端获取日历服务,我可以使用日历服务
// 通过 CalendarList.Get("calendar id").Do() 获取特定日历,因此凭据没有问题
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)
}
更多关于Golang日历客户端库与服务账户的使用指南的实战教程也可以访问 https://www.itying.com/category-94-b0.html
1 回复


