Google-cloud-java: How to use google application credentials json file with 12factor app on heroku or elsewhere?

Created on 15 Jun 2018  路  3Comments  路  Source: googleapis/google-cloud-java

We like to stick to the 12factor best practices of using environment variables for any credentials. Using Heroku, we don't want to commit this file to our repo, but we can't include the whole file as an env variable.

Is there an option using this SDK that allows us to authenticate with only the necessary values from the provided json file, set only as environment variables?

core auth p2 question

All 3 comments

Hi @dfuentes77,

ServiceAccountCredentials.newBuilder() might be helpful. With this you should be able to build a ServiceAccountCredentials after setting all the entries in the json files pointed by GOOGLE_APPLICATION_CREDENTIALS to envrionment variables and then pass that to a ServiceOptions or a Client:

ServiceAccountCredentials credentials = ServiceAccountCredentials.newBuilder()...;
ServiceOptions options = ServiceOptions.newBuilder()
      .setCredentials(credentials)
      .setProjectId("[project-id]")
      .build();

Let me know if that helps.

@hzyi-google > ServiceAccountCredentials.newBuilder()

How do we actually use this in our code? Do we need to import anything other than '@google-cloud'? Is there a Node.js example?

Hi @ttfreeman, this repo is about Java. For Node.js please check out google-cloud-node.

Was this page helpful?
0 / 5 - 0 ratings