Skip to content

Instantly share code, notes, and snippets.

@psaia
Created July 27, 2020 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psaia/dd83af1819fecfdb9a96563f2022971e to your computer and use it in GitHub Desktop.
Save psaia/dd83af1819fecfdb9a96563f2022971e to your computer and use it in GitHub Desktop.
Authenticated request to a Google Cloud Function in Go.
package main
import (
"context"
"io/ioutil"
"net/http"
"github.com/kr/pretty"
"google.golang.org/api/idtoken"
"google.golang.org/api/option"
)
func main() {
saKey := "/Users/pete/Downloads/some-key.json"
url := "https://path-to-cf"
client, err := idtoken.NewClient(context.TODO(), url, option.WithCredentialsFile(saKey))
if err != nil {
panic(err)
}
req, err := http.NewRequest(http.MethodPost, url, nil)
if err != nil {
panic(err)
}
res, err := client.Do(req)
if err != nil {
panic(err)
}
// Do stuff with the response.
bodyBytes, err := ioutil.ReadAll(res.Body)
if err != nil {
panic(err)
}
pretty.Logln(string(bodyBytes))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment