最新公告
  • 欢迎您光临码农资源网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!加入我们
  • 如何使用服务帐户为 Google Calendar API 设置 golang 客户端

    如何使用服务帐户为 google calendar api 设置 golang 客户端

    如何使用服务帐户为Google Calendar API设置golang客户端?这是许多开发者在使用Google Calendar API时经常遇到的问题。在这篇文章中,php小编香蕉将为你详细介绍如何使用服务帐户来配置golang客户端,以便你能够顺利地与Google Calendar API进行交互。无论你是初学者还是有一定经验的开发者,本文都将为你提供清晰的指导,帮助你快速上手这个过程。让我们一起来探索吧!

    问题内容

    我看过很多有关用户的 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:  "<a href="https://www.codesou.cn/" class="__cf_email__" data-cfemail="82e3e1e1edf7ecf6b3b0b1c2f2f0ede8e7e1f6afb6b7b4acebe3eface5f1e7f0f4ebe1e7e3e1e1edf7ecf6ace1edef">[email protected]</a>",
            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()
    想要了解更多内容,请持续关注码农资源网,一起探索发现编程世界的无限可能!
    本站部分资源来源于网络,仅限用于学习和研究目的,请勿用于其他用途。
    如有侵权请发送邮件至1943759704@qq.com删除

    码农资源网 » 如何使用服务帐户为 Google Calendar API 设置 golang 客户端
    • 18会员总数(位)
    • 16045资源总数(个)
    • 1160本周发布(个)
    • 297 今日发布(个)
    • 113稳定运行(天)

    提供最优质的资源集合

    立即查看 了解详情