I developped a Python backend service which connects to the Google Calendar API (v3), retrieves calendar entries and exposes them (they are retrieved by via an HTTP call). In order to do so, I use OAuth 2.0 for Server to Server Applications.
When developping my application a few years ago, the module provided by Google was not yet ported to Python 3 and I went the JWT way ("HTTP/REST" in the documentation linked above).
I now would like to learn Go and start by porting this application.
The Google Calendar API quickstart for Go provides a full example, but it assumes that the authentication will include a consent screen from the user (3-legged oAuth). This is similar to the Python example, but there is also a Python version using the service to service approach.
Is this service to service authentication funtionality available in the Go library?
If it is not I will manually generate a JWT (as I do it now with the Python version of my code) but since I am just starting with Go, I would prefer using libraries as much as possible while I learn on the fly.
I'm not setup to test this at the moment but I believe something like this should work:
package main
import (
"context"
"io/ioutil"
"log"
"golang.org/x/oauth2/google"
calendar "google.golang.org/api/calendar/v3"
)
func main() {
cred, err := ioutil.ReadFile("service_account.json")
if err != nil {
log.Fatalf("Unable to read JSON credentials config %v", err)
}
conf, err := google.JWTConfigFromJSON(cred, "https://www.googleapis.com/auth/calendar")
if err != nil {
log.Fatalf("Unable to obtain JWT conf %v", err)
}
client := conf.Client(context.Background())
srv, err := calendar.New(client)
if err != nil {
log.Fatalf("Unable to retrieve calendar Client %v", err)
}
...
}
The contents of service_account.json
should be obtained from the service account configuration somehow, I'm not 100% sure about that part though.