How to setup golang client for Google Calendar API using service account? This is a common problem that many developers encounter when using the Google Calendar API. In this article, PHP editor Banana will introduce you in detail how to use a service account to configure the golang client so that you can interact with the Google Calendar API smoothly. Whether you are a beginner or an experienced developer, this article will provide you with clear guidance to help you quickly get started with the process. Let’s explore together!
I've seen a lot of documentation about the google api client for users, but very little about using service accounts. This does not represent the user, I just want the client to use the calendar api with a client id and client secret, which will be provided to me via environment variables (I don't want to start from a file).
This is 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 get 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 api :)
@tanaike's answer put me on the right track. This is what I ended up using:
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.php.cn/link/89fee0513b6668e555959f5dc23238e9" 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))) }
Note that I did not have to configure domain-wide delegation or impersonate the user; after adding the service account to the calendar, it worked just fine.
After adding the account email to the calendar, the service account still needs to accept the calendar invitation. This can be done by:
entry := calendar.CalendarListEntry{Id: calendarID} service.CalendarList.Insert(&entry).Do()
The above is the detailed content of How to setup golang client for Google Calendar API using service account. For more information, please follow other related articles on the PHP Chinese website!