Skip to content

Commit

Permalink
examples: use WithServiceAccountFile
Browse files Browse the repository at this point in the history
Use the new WithServiceAccountFile instead of constructing a TokenSource
with a jwt config.

Change-Id: I5eeb9dc3b8369f3dbd2ef2cee4661bab9bc821ce
Reviewed-on: https://code-review.googlesource.com/7407
Reviewed-by: Chris Broadfoot <cbro@google.com>
  • Loading branch information
rakyll committed Sep 6, 2016
1 parent a47b182 commit 2487288
Showing 1 changed file with 4 additions and 14 deletions.
18 changes: 4 additions & 14 deletions authexample_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@
package cloud_test

import (
"io/ioutil"

"cloud.google.com/go/datastore"
"golang.org/x/net/context"
"golang.org/x/oauth2/google"
"google.golang.org/api/option"
)

Expand All @@ -41,7 +38,7 @@ func Example_applicationDefaultCredentials() {
_ = client
}

func Example_serviceAccount() {
func Example_serviceAccountFile() {
// Warning: The better way to use service accounts is to set GOOGLE_APPLICATION_CREDENTIALS
// and use the Application Default Credentials.
ctx := context.Background()
Expand All @@ -52,16 +49,9 @@ func Example_serviceAccount() {
//
// Note: The example uses the datastore client, but the same steps apply to
// the other client libraries underneath this package.
key, err := ioutil.ReadFile("/path/to/service-account-key.json")
if err != nil {
// TODO: handle error.
}
cfg, err := google.JWTConfigFromJSON(key, datastore.ScopeDatastore)
if err != nil {
// TODO: handle error.
}
client, err := datastore.NewClient(
ctx, "project-id", option.WithTokenSource(cfg.TokenSource(ctx)))
client, err := datastore.NewClient(ctx,
"project-id",
option.WithServiceAccountFile("/path/to/service-account-key.json"))
if err != nil {
// TODO: handle error.
}
Expand Down

0 comments on commit 2487288

Please sign in to comment.