首页 > 文章列表 > Google Calendar API的golang客户端设置教程:使用服务账户

Google Calendar API的golang客户端设置教程:使用服务账户

286 2024-02-07
问题内容

我看过很多有关用户的 google api 客户端的文档,但有关使用服务帐户的文档却很少。这并不代表用户,我只是想让客户端使用客户端 id 和客户端密钥来使用日历 api,这将通过环境变量为我提供(我不想从一个文件)。

这是我到目前为止所拥有的:

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

但我收到以下错误:

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"
}

非常感谢这里的任何帮助!我是 golang、oauth2 和 google api 的新手 :)


正确答案


@tanaike 的回答让我走上了正轨。这就是我最终使用的:

package main

import (
    "context"
    "encoding/json"
    "fmt"

    googleoauth "golang.org/x/oauth2/google"
    calendar "google.golang.org/api/calendar/v3"
    apioption "google.golang.org/api/option"
)

var service *calendar.service

// note that some of the fields are optional:
type googleauthconfig struct {
    type                string `json:"type"`
    projectid           string `json:"project_id,omitempty"`
    clientemail         string `json:"client_email"`
    clientid            string `json:"client_id,omitempty"`
    clientsecret        string `json:"private_key"`
    clientsecretid      string `json:"private_key_id,omitempty"`
    authurl             string `json:"auth_uri,omitempty"`
    tokenurl            string `json:"token_uri,omitempty"`
    authprovidercerturl string `json:"auth_provider_x509_cert_url,omitempty"`
    clientcerturl       string `json:"client_x509_cert_url,omitempty"`
}

func main() {
    authconfig := googleauthconfig{
        type:         "service_account",
        clientemail:  "[email protected]",
        clientid:     "1234",
        clientsecret: "-----begin private key-----n...n-----end private key-----n",
        authurl:      googleoauth.endpoint.authurl,
        tokenurl:     googleoauth.endpoint.tokenurl,
    }
    authconfigjson, err := json.marshal(authconfig)

    ctx := context.background()
    service, err = calendar.newservice(ctx, apioption.withcredentialsjson([]byte(authconfigjson)))
}

请注意,我不必配置域范围的委派或模拟用户;将服务帐户添加到日历后,效果很好。

将帐户电子邮件添加到日历后,服务帐户仍需要接受日历邀请。这可以通过以下方式完成:

entry := calendar.CalendarListEntry{Id: calendarID}
service.CalendarList.Insert(&entry).Do()