// NewServiceAccountFromKey constructs the credentials using the JSON key slice // from a Google Developers service account. func NewServiceAccountFromKey(jsonKey []byte, scope ...string) (Credentials, error) { config, err := google.JWTConfigFromJSON(jsonKey, scope...) if err != nil { return nil, err } return serviceAccount{config: config}, nil }
func Context(scopes ...string) context.Context { key, projID := os.Getenv(envPrivateKey), os.Getenv(envProjID) if key == "" || projID == "" { log.Fatal("GCLOUD_TESTS_GOLANG_KEY and GCLOUD_TESTS_GOLANG_PROJECT_ID must be set. See CONTRIBUTING.md for details.") } jsonKey, err := ioutil.ReadFile(key) if err != nil { log.Fatalf("Cannot read the JSON key file, err: %v", err) } conf, err := google.JWTConfigFromJSON(jsonKey, scopes...) if err != nil { log.Fatal(err) } return cloud.NewContext(projID, conf.Client(oauth2.NoContext)) }
func ExampleJWTConfigFromJSON() { // Your credentials should be obtained from the Google // Developer Console (https://console.developers.google.com). // Navigate to your project, then see the "Credentials" page // under "APIs & Auth". // To create a service account client, click "Create new Client ID", // select "Service Account", and click "Create Client ID". A JSON // key file will then be downloaded to your computer. data, err := ioutil.ReadFile("/path/to/your-project-key.json") if err != nil { log.Fatal(err) } conf, err := google.JWTConfigFromJSON(data, "https://www.googleapis.com/auth/bigquery") if err != nil { log.Fatal(err) } // Initiate an http.Client. The following GET request will be // authorized and authenticated on the behalf of // your service account. client := conf.Client(oauth2.NoContext) client.Get("...") }