googleapis / googleapis/google-api-go-client

Load GOOGLE_APPLICATION_CREDENTIALS json content via an environment variable instead of a file

Open
#185 22 comments 53 reactions 0 assignees View on GitHub
type: feature request
Dominant language
Go
Stars
4.5k
Forks
1.5k
Avg merge
1d 15h
Merged PRs (30d)
32

Description

👋

What from I can see the `golang.org/x/oauth2/google` package only supports loading the JSON application credentials file from the path given in the `GOOGLE_APPLICATION_CREDENTIALS` environment variable, without digging deep into the inner workings.

When deploying go apps on Heroku or Pivotal Web Services secrets are normally kept in environment variables. Code and files get pushed together on every deploy and it's not ideal to keep the credentials JSON with the code and it's not easy to manage the credentials file alongside the code since it needs to be committed in the case of Heroku, or present at every push in the case of Pivotal Web Services,

I figured out how to do this but I had to dig through the source and multiple docs. This was the resulting code:

```go
json := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS_JSON") // `{"type": "service_account", "project_id": "my-project", ...}`
ctx := context.Background()
jwtConfig, err := google.JWTConfigFromJSON([]byte(json), datastore.ScopeDatastore)
if err != nil {
...
}
ts := jwtConfig.TokenSource(ctx)
datastoreClient, err := datastore.NewClient(ctx, projectID, option.WithTokenSource(ts))
```

I think there need to be another environment variable that will pickup the config and set default credentials without needing it to be in a file, or we need clearer documentation on how to load credentials from elsewhere.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.