简体   繁体   English

如何使用服务帐户为 Google Calendar API 设置 golang 客户端

[英]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.我相信你的目标如下。

  • You want to access Google Calendar using the service account with golang.您想使用带有 golang 的服务帐户访问 Google 日历。

In this case, how about the following modification?在这种情况下,如何进行以下修改?

Modified script:修改脚本:

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))

Note:笔记:

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 显示作为服务帐户身份验证的谷歌日历事件 - Golang App - Display google calendar events authenticating as service account - Golang App 使用服务帐户在 Golang 中验证对 Cloud Function 的客户端调用 - Authenticate client call to Cloud Function in Golang using service account 无法使用 Golang 使用服务帐户凭据访问 Google 电子表格 - Unable to access Google Spreadsheets with a service account credentials using Golang 如何为用 Golang 编写的 windows 服务设置登录用户帐户? - How to set a login user account for a windows service written in Golang? 服务帐户,App引擎,Go,Google API - Service account, App engine, Go, Google APIs 在Golang中手动提供Google服务帐户凭据 - Providing Google service account credentials manually in Golang 如何使用 golang 的 kube.netes 服务帐户? - How to use kubernetes service account with golang? 如何使用 golang http 客户端在 url 中正确设置路径参数? - How to properly set path params in url using golang http client? 在 Golang 中获取谷歌云服务帐户的访问令牌? - Get access token for a google cloud service account in Golang? 如何从 google-api-go-client 将日程安排设置为 Google 日历? - How to set the schedule to Google Calendar from google-api-go-client?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM