Skip to content

Commit

Permalink
docs: add example and readme for using fromJSON (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored Dec 27, 2017
1 parent 6b55320 commit 525a74f
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,51 @@ main();

The parameters for the JWT auth client including how to use it with a `.pem` file are explained in [samples/jwt.js](samples/jwt.js).

#### Loading credentials from environment variables

Instead of loading credentials from a key file, you can also provide them using an environment variable and the `GoogleAuth.fromJSON()` method. This is particularly convenient for systems that deploy directly from source control (Heroku, App Engine, etc).

Start by exporting your credentials:

```
$ export CREDS='{
"type": "service_account",
"project_id": "your-project-id",
"private_key_id": "your-private-key-id",
"private_key": "your-private-key",
"client_email": "your-client-email",
"client_id": "your-client-id",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "your-cert-url"
}'
```
Now you can create a new client from the credentials:

```js
const {GoogleAuth} = require('google-auth-library');

// load the environment variable with our keys
const keysEnvVar = process.env['CREDS'];
if (!keysEnvVar) {
throw new Error('The $CREDS environment variable was not found!');
}
const keys = JSON.parse(keysEnvVar);

async function main() {
const auth = new GoogleAuth();
// load the JWT or UserRefreshClient from the keys
const client = auth.fromJSON(keys);
client.scopes = ['https://www.googleapis.com/auth/cloud-platform'];
await client.authorize();
const url = `https://www.googleapis.com/dns/v1/projects/${keys.project_id}`;
const res = await client.request({url});
console.log(res.data);
}

main().catch(console.error);
```

### Questions/problems?

Expand Down
59 changes: 59 additions & 0 deletions examples/fromJSON.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2017, Google, Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

const {GoogleAuth} = require('../build/src/index');

/**
* Instead of loading credentials from a key file, you can also provide
* them using an environment variable and the `fromJSON` method. This
* is particularly convenient for systems that deploy directly from source
* control (Heroku, App Engine, etc).
*
* To run this program, you can create an environment variables that contains
* the keys:
*
* $ export CREDS='{
* "type": "service_account",
* "project_id": "your-project-id",
* "private_key_id": "your-private-key-id",
* "private_key": "your-private-key",
* "client_email": "your-client-email",
* "client_id": "your-client-id",
* "auth_uri": "https://accounts.google.com/o/oauth2/auth",
* "token_uri": "https://accounts.google.com/o/oauth2/token",
* "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
* "client_x509_cert_url": "your-cert-url"
* }'
* $ node fromJSON.js
*
**/

const keysEnvVar = process.env['CREDS'];
if (!keysEnvVar) {
throw new Error('The $CREDS environment variable was not found!');
}
const keys = JSON.parse(keysEnvVar);

async function main() {
const auth = new GoogleAuth();
const client = auth.fromJSON(keys);
client.scopes = ['https://www.googleapis.com/auth/cloud-platform'];
await client.authorize();
const url = `https://www.googleapis.com/dns/v1/projects/${keys.project_id}`;
const res = await client.request({url});
console.log(res.data);
}

main().catch(console.error);

0 comments on commit 525a74f

Please sign in to comment.