Skip to content

Commit

Permalink
chore: cleanup samples and readme (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith committed Jan 9, 2018
1 parent b8a47ca commit 0bc61e3
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 48 deletions.
34 changes: 13 additions & 21 deletions README.md
Expand Up @@ -2,7 +2,7 @@

# Google Auth Library

[![Greenkeeper badge](https://badges.greenkeeper.io/google/google-auth-library-nodejs.svg)](https://greenkeeper.io/)
[![Greenkeeper badge][greenkeeperimg]][greenkeeper]
[![npm version][npmimg]][npm]
[![Build Status][travisimg]][travis]
[![Code Coverage][coverallsimg]][coveralls]
Expand Down Expand Up @@ -55,7 +55,7 @@ For example, a JWT auth client will be created when your code is running on your
The code below shows how to retrieve a default credential type, depending upon the runtime environment. The createScopedRequired must be called to determine when you need to pass in the scopes manually, and when they have been set for you automatically based on the configured runtime environment.

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

/**
Expand Down Expand Up @@ -114,7 +114,7 @@ For more information about OAuth2 and how it works, [see here][oauth].
Let's take a look at a complete example.

``` js
const OAuth2Client = require('google-auth-library').OAuth2Client;
const {OAuth2Client} = require('google-auth-library');
const http = require('http');
const url = require('url');
const querystring = require('querystring');
Expand Down Expand Up @@ -231,33 +231,23 @@ const tokens = await oauth2Client.refreshAccessToken();
The Google Developers Console provides `.json` file that you can use to configure a JWT auth client and authenticate your requests, for example when using a service account.

``` js
const JWTClient = require('../build/src/index').JWT;
const {JWT} = require('../build/src/index');
const keys = require('./jwt.keys.json');

async function main() {
try {
const client = await getJWTClient();
const url = `https://www.googleapis.com/dns/v1/projects/${keys.project_id}`;
const res = await client.request({url});
console.log(res.data);
} catch(e) {
console.error(e);
}
process.exit();
}

async function getJWTClient() {
const client = new JWTClient(
const client = new JWT(
keys.client_email,
null,
keys.private_key,
['https://www.googleapis.com/auth/cloud-platform'],
);
await client.authorize();
return client;
const url = `https://www.googleapis.com/dns/v1/projects/${keys.project_id}`;
const res = await client.request({url});
console.log(res.data);
}

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

```

Expand Down Expand Up @@ -340,13 +330,15 @@ This library is licensed under Apache 2.0. Full license text is available in [LI
[coverallsimg]: https://img.shields.io/coveralls/google/google-auth-library-nodejs.svg
[david-dm-img]: https://david-dm.org/google/google-auth-library-nodejs/status.svg
[david-dm]: https://david-dm.org/google/google-auth-library-nodejs
[greenkeeperimg]: https://badges.greenkeeper.io/google/google-auth-library-nodejs.svg
[greenkeeper]: https://greenkeeper.io/
[node]: http://nodejs.org/
[npmimg]: https://img.shields.io/npm/v/google-auth-library.svg
[npm]: https://www.npmjs.org/package/google-auth-library
[oauth]: https://developers.google.com/identity/protocols/OAuth2
[overflowimg]: https://googledrive.com/host/0ByfSjdPVs9MZbkhjeUhMYzRTeEE/stackoveflow-tag.png
[snyk-image]: https://snyk.io/test/github/google/google-auth-library/badge.svg
[snyk-url]: https://snyk.io/test/github/google/google-auth-library
[snyk-image]: https://snyk.io/test/github/google/google-auth-library-nodejs/badge.svg
[snyk-url]: https://snyk.io/test/github/google/google-auth-library-nodejs
[stability]: http://nodejs.org/api/stream.html#stream_stream
[stackoverflow]: http://stackoverflow.com/questions/tagged/google-auth-library-nodejs
[stream]: http://nodejs.org/api/stream.html#stream_class_stream_readable
Expand Down
2 changes: 1 addition & 1 deletion examples/adc.js
Expand Up @@ -22,7 +22,7 @@
/**
* Import the GoogleAuth library, and create a new GoogleAuth client.
*/
const GoogleAuth = require('../build/src/index').GoogleAuth;
const { GoogleAuth } = require('../build/src/index');
const auth = new GoogleAuth();

/**
Expand Down
4 changes: 2 additions & 2 deletions examples/jwt.js
Expand Up @@ -13,7 +13,7 @@

'use strict';

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

/**
* The JWT authorization is ideal for performing server-to-server
Expand All @@ -33,7 +33,7 @@ async function main() {
});
await client.authorize();
const url = `https://www.googleapis.com/dns/v1/projects/${keys.project_id}`;
const res = await client.request({url});
const res = await client.request({ url });
console.log(res.data);
}

Expand Down
2 changes: 1 addition & 1 deletion examples/keepalive.js
Expand Up @@ -23,7 +23,7 @@
/**
* Import the GoogleAuth library, and create a new GoogleAuth client.
*/
const GoogleAuth = require('../build/src/index').GoogleAuth;
const { GoogleAuth } = require('../build/src/index');
const auth = new GoogleAuth();
const https = require('https');

Expand Down
2 changes: 1 addition & 1 deletion examples/refreshAccessToken.js
Expand Up @@ -13,7 +13,7 @@

'use strict';

const OAuth2Client = require('../build/src/index').OAuth2Client;
const { OAuth2Client } = require('../build/src/index');
const http = require('http');
const url = require('url');
const querystring = require('querystring');
Expand Down
47 changes: 25 additions & 22 deletions examples/verifyIdToken.js
Expand Up @@ -13,7 +13,7 @@

'use strict';

const OAuth2Client = require('../build/src/index').OAuth2Client;
const { OAuth2Client } = require('../build/src/index');
const http = require('http');
const url = require('url');
const querystring = require('querystring');
Expand All @@ -31,13 +31,14 @@ async function main() {

// Verify the id_token, and access the claims.
const ticket = await oAuth2Client.verifyIdToken(
oAuth2Client.credentials.id_token,
keys.web.client_id);
oAuth2Client.credentials.id_token,
keys.web.client_id
);
console.log(ticket);

// You can use this info to get user information too.
const url = `https://www.googleapis.com/plus/v1/people/me`;
const res = await oAuth2Client.request({url})
const res = await oAuth2Client.request({ url });
console.log(res.data);
} catch (e) {
console.error(e);
Expand Down Expand Up @@ -72,25 +73,27 @@ function getAuthenticatedClient() {

// Open an http server to accept the oauth callback. In this simple example, the
// only request to our webserver is to /oauth2callback?code=<code>
const server = http.createServer(async (req, res) => {
if (req.url.indexOf('/oauth2callback') > -1) {
// acquire the code from the querystring, and close the web server.
const qs = querystring.parse(url.parse(req.url).query);
console.log(`Code is ${qs.code}`);
res.end('Authentication successful! Please return to the console.');
server.close();
const server = http
.createServer(async (req, res) => {
if (req.url.indexOf('/oauth2callback') > -1) {
// acquire the code from the querystring, and close the web server.
const qs = querystring.parse(url.parse(req.url).query);
console.log(`Code is ${qs.code}`);
res.end('Authentication successful! Please return to the console.');
server.close();

// Now that we have the code, use that to acquire tokens.
const r = await oAuth2Client.getToken(qs.code)
// Make sure to set the credentials on the OAuth2 client.
oAuth2Client.setCredentials(r.tokens);
console.info('Tokens acquired.');
resolve(oAuth2Client);
}
}).listen(3000, () => {
// open the browser to the authorize url to start the workflow
opn(authorizeUrl);
});
// Now that we have the code, use that to acquire tokens.
const r = await oAuth2Client.getToken(qs.code);
// Make sure to set the credentials on the OAuth2 client.
oAuth2Client.setCredentials(r.tokens);
console.info('Tokens acquired.');
resolve(oAuth2Client);
}
})
.listen(3000, () => {
// open the browser to the authorize url to start the workflow
opn(authorizeUrl);
});
});
}

Expand Down

0 comments on commit 0bc61e3

Please sign in to comment.