[英]How to set up golang client for Google Calendar APIs using a service account
I've seen lots of documentation on Google API clients for users, but very little on using a service account.我看过很多关于用户的 Google API 客户端的文档,但关于使用服务帐户的文档很少。 This isn't on behalf of a user, I'm just trying to get a client working with the Calendar APIs using a client ID and client secret, which would be provided via environment variables for me (I'd prefer to not read from a file).
这不代表用户,我只是想让一个客户端使用客户端 ID 和客户端密码来使用日历 API,这将通过环境变量为我提供(我不想从一份文件)。
Here's what I have so far:这是我到目前为止所拥有的:
package main
import (
"context"
clientCredentials "golang.org/x/oauth2/clientcredentials"
google "golang.org/x/oauth2/google"
calendar "google.golang.org/api/calendar/v3"
apiOption "google.golang.org/api/option"
)
func main() {
config := &clientCredentials.Config{
ClientID: "<my_id>",
ClientSecret: "-----BEGIN PRIVATE KEY-----\n...",
TokenURL: google.Endpoint.TokenURL,
}
ctx := context.Background()
client := config.Client(ctx)
service, _ := calendar.NewService(ctx, apiOption.WithHTTPClient(client))
calendarList, err := service.CalendarList.List().Do()
}
But I'm getting the following error:但我收到以下错误:
Get "https://www.googleapis.com/calendar/v3/users/me/calendarList?alt=json&prettyPrint=false": oauth2: cannot fetch token: 400 Bad Request
Response: {
"error": "unsupported_grant_type",
"error_description": "Invalid grant_type: client_credentials"
}
Any help here is greatly appreciated, I'm new to Golang, Oauth2: and Google APIs :)非常感谢这里的任何帮助,我是 Golang、Oauth2: 和 Google API 的新手:)
I believe your goal is as follows.我相信你的目标如下。
In this case, how about the following modification?在这种情况下,如何进行以下修改?
package main
import (
"context"
"fmt"
calendar "google.golang.org/api/calendar/v3"
"google.golang.org/api/option"
)
func main() {
ctx := context.Background()
v := `{"type": "service_account", "private_key": "-----BEGIN PRIVATE KEY-----\n###\n-----END PRIVATE KEY-----\n", "client_email": "###"}`
service, _ := calendar.NewService(ctx, option.WithCredentialsJSON([]byte(v)))
calendarList, _ := service.CalendarList.List().Do()
fmt.Println(calendarList)
}
If you want to use the file including the credential information, please modify it as follows.如果要使用包含凭据信息的文件,请按如下方式修改。
service, _:= calendar.NewService(ctx, option.WithCredentialsFile(credentialFile))
If you want to retrieve the Google Calendar of your account, please share your Google Calendar with the email of the service account.如果您想找回您账户的谷歌日历,请将您的谷歌日历分享给服务账户的email。
Linda Lawton - DaImTo's comment: Google calendar doesn't support service account for standard google users. You need to configure domain wide delegation and impersonate a user on your google workspace account. and how to add the sub to this code.
Linda Lawton - DaImTo 的评论:
Google calendar doesn't support service account for standard google users. You need to configure domain wide delegation and impersonate a user on your google workspace account. and how to add the sub to this code.
Google calendar doesn't support service account for standard google users. You need to configure domain wide delegation and impersonate a user on your google workspace account. and how to add the sub to this code.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.