Skip to content

Commit bd0accc

Browse files
refactor: use local-auth for 3 legged samples (googleapis#2058)
1 parent 0b8d04c commit bd0accc

36 files changed

+491
-605
lines changed

samples/analytics/analytics.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@
1414
'use strict';
1515

1616
const {google} = require('googleapis');
17-
const sampleClient = require('../sampleclient');
17+
const path = require('path');
18+
const {authenticate} = require('@google-cloud/local-auth');
1819

19-
const analytics = google.analytics({
20-
version: 'v3',
21-
auth: sampleClient.oAuth2Client,
22-
});
20+
const analytics = google.analytics('v3');
2321

2422
// Custom Goals must exist prior to being used as an objectiveMetric
2523
const objectiveMetric = 'ga:goal1Completions';
@@ -37,6 +35,13 @@ const variations = [
3735
];
3836

3937
async function runSample() {
38+
// Obtain user credentials to use for the request
39+
const auth = await authenticate({
40+
keyfilePath: path.join(__dirname, '../oauth2.keys.json'),
41+
scopes: 'https://www.googleapis.com/auth/analytics',
42+
});
43+
google.options({auth});
44+
4045
const res = await analytics.management.experiments.insert({
4146
accountId: 'your-accountId',
4247
webPropertyId: 'your-webPropertyId',
@@ -53,9 +58,7 @@ async function runSample() {
5358
return res.data;
5459
}
5560

56-
const scopes = ['https://www.googleapis.com/auth/analytics'];
57-
58-
sampleClient
59-
.authenticate(scopes)
60-
.then(() => runSample())
61-
.catch(console.error);
61+
if (module === require.main) {
62+
runSample().catch(console.error);
63+
}
64+
module.exports = runSample;

samples/analyticsReporting/batchGet.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,19 @@
1414
'use strict';
1515

1616
const {google} = require('googleapis');
17-
const sampleClient = require('../sampleclient');
17+
const path = require('path');
18+
const {authenticate} = require('@google-cloud/local-auth');
1819

19-
const analyticsreporting = google.analyticsreporting({
20-
version: 'v4',
21-
auth: sampleClient.oAuth2Client,
22-
});
20+
const analyticsreporting = google.analyticsreporting('v4');
2321

2422
async function runSample() {
23+
// Obtain user credentials to use for the request
24+
const auth = await authenticate({
25+
keyfilePath: path.join(__dirname, '../oauth2.keys.json'),
26+
scopes: 'https://www.googleapis.com/auth/analytics',
27+
});
28+
google.options({auth});
29+
2530
const res = await analyticsreporting.reports.batchGet({
2631
requestBody: {
2732
reportRequests: [
@@ -52,12 +57,7 @@ async function runSample() {
5257

5358
// if invoked directly (not tests), authenticate and run the samples
5459
if (module === require.main) {
55-
const scopes = ['https://www.googleapis.com/auth/analytics'];
56-
sampleClient.authenticate(scopes).then(runSample).catch(console.error);
60+
runSample.catch(console.error);
5761
}
58-
5962
// export functions for testing purposes
60-
module.exports = {
61-
runSample,
62-
client: sampleClient.oAuth2Client,
63-
};
63+
module.exports = runSample;

samples/blogger/insert.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,20 @@
1313

1414
'use strict';
1515

16+
const path = require('path');
1617
const {google} = require('googleapis');
17-
const sampleClient = require('../sampleclient');
18+
const {authenticate} = require('@google-cloud/local-auth');
1819

19-
const blogger = google.blogger({
20-
version: 'v3',
21-
auth: sampleClient.oAuth2Client,
22-
});
20+
const blogger = google.blogger('v3');
2321

2422
async function runSample() {
23+
// Obtain user credentials to use for the request
24+
const auth = await authenticate({
25+
keyfilePath: path.join(__dirname, '../oauth2.keys.json'),
26+
scopes: 'https://www.googleapis.com/auth/blogger',
27+
});
28+
google.options({auth});
29+
2530
const res = await blogger.posts.insert({
2631
blogId: '4340475495955554224',
2732
requestBody: {
@@ -35,11 +40,6 @@ async function runSample() {
3540
}
3641

3742
if (module === require.main) {
38-
const scopes = ['https://www.googleapis.com/auth/blogger'];
39-
sampleClient.authenticate(scopes).then(runSample).catch(console.error);
43+
runSample().catch(console.error);
4044
}
41-
42-
module.exports = {
43-
runSample,
44-
client: sampleClient.oAuth2Client,
45-
};
45+
module.exports = runSample;

samples/docs/create.js

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,20 @@
1313

1414
'use strict';
1515

16+
const path = require('path');
1617
const {google} = require('googleapis');
17-
const util = require('util');
18-
const sampleClient = require('../sampleclient');
18+
const {authenticate} = require('@google-cloud/local-auth');
1919

20-
const docs = google.docs({
21-
version: 'v1',
22-
auth: sampleClient.oAuth2Client,
23-
});
20+
const docs = google.docs('v1');
2421

2522
async function runSample() {
23+
// Obtain user credentials to use for the request
24+
const auth = await authenticate({
25+
keyfilePath: path.join(__dirname, '../oauth2.keys.json'),
26+
scopes: 'https://www.googleapis.com/auth/documents',
27+
});
28+
google.options({auth});
29+
2630
// The initial call to create the doc will have a title but no content.
2731
// This is a limitation of the underlying API.
2832
const createResponse = await docs.documents.create({
@@ -32,16 +36,17 @@ async function runSample() {
3236
});
3337
console.log(createResponse.data);
3438

35-
// now that we created the doc, let's add content using the
39+
// now that we created the doc, let's add content using the
3640
// documentId returned from the create call.
3741
const updateResponse = await docs.documents.batchUpdate({
3842
documentId: createResponse.data.documentId,
3943
requestBody: {
40-
requests: [{
44+
requests: [
45+
{
4146
insertText: {
4247
// The first text inserted into the document must create a paragraph,
43-
// which can't be done with the `location` property. Use the
44-
// `endOfSegmentLocation` instead, which assumes the Body if
48+
// which can't be done with the `location` property. Use the
49+
// `endOfSegmentLocation` instead, which assumes the Body if
4550
// unspecified.
4651
endOfSegmentLocation: {},
4752
text: 'Hello there!'
@@ -53,18 +58,7 @@ async function runSample() {
5358
return updateResponse.data;
5459
}
5560

56-
const scopes = [
57-
'https://www.googleapis.com/auth/documents',
58-
];
59-
6061
if (module === require.main) {
61-
sampleClient
62-
.authenticate(scopes)
63-
.then(runSample)
64-
.catch(console.error);
62+
runSample().catch(console.error);
6563
}
66-
67-
module.exports = {
68-
runSample,
69-
client: sampleClient.oAuth2Client,
70-
};
64+
module.exports = runSample;

samples/docs/get.js

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,29 @@
1313

1414
'use strict';
1515

16-
const {google} = require('googleapis');
16+
const path = require('path');
1717
const util = require('util');
18-
const sampleClient = require('../sampleclient');
18+
const {google} = require('googleapis');
19+
const {authenticate} = require('@google-cloud/local-auth');
1920

20-
const docs = google.docs({
21-
version: 'v1',
22-
auth: sampleClient.oAuth2Client,
23-
});
21+
const docs = google.docs('v1');
2422

2523
async function runSample() {
24+
// Obtain user credentials to use for the request
25+
const auth = await authenticate({
26+
keyfilePath: path.join(__dirname, '../oauth2.keys.json'),
27+
scopes: 'https://www.googleapis.com/auth/documents',
28+
});
29+
google.options({auth});
30+
2631
const res = await docs.documents.get({
27-
documentId: '1XPbMENiP5bWP_cbqc0bEWbq78vmUf-rWQ6aB6FVZJyc'
32+
documentId: '1XPbMENiP5bWP_cbqc0bEWbq78vmUf-rWQ6aB6FVZJyc',
2833
});
2934
console.log(util.inspect(res.data, false, 17));
3035
return res.data;
3136
}
3237

33-
const scopes = [
34-
'https://www.googleapis.com/auth/documents',
35-
];
36-
3738
if (module === require.main) {
38-
sampleClient
39-
.authenticate(scopes)
40-
.then(runSample)
41-
.catch(console.error);
39+
runSample().catch(console.error);
4240
}
43-
44-
module.exports = {
45-
runSample,
46-
client: sampleClient.oAuth2Client,
47-
};
41+
module.exports = runSample;

samples/drive/download.js

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,30 @@
1414
'use strict';
1515

1616
const {google} = require('googleapis');
17-
const sampleClient = require('../sampleclient');
1817
const fs = require('fs');
1918
const os = require('os');
2019
const uuid = require('uuid');
2120
const path = require('path');
21+
const {authenticate} = require('@google-cloud/local-auth');
2222

23-
const drive = google.drive({
24-
version: 'v3',
25-
auth: sampleClient.oAuth2Client,
26-
});
23+
const drive = google.drive('v3');
2724

2825
async function runSample(fileId) {
26+
// Obtain user credentials to use for the request
27+
const auth = await authenticate({
28+
keyfilePath: path.join(__dirname, '../oauth2.keys.json'),
29+
scopes: [
30+
'https://www.googleapis.com/auth/drive',
31+
'https://www.googleapis.com/auth/drive.appdata',
32+
'https://www.googleapis.com/auth/drive.file',
33+
'https://www.googleapis.com/auth/drive.metadata',
34+
'https://www.googleapis.com/auth/drive.metadata.readonly',
35+
'https://www.googleapis.com/auth/drive.photos.readonly',
36+
'https://www.googleapis.com/auth/drive.readonly',
37+
],
38+
});
39+
google.options({auth});
40+
2941
// For converting document formats, and for downloading template
3042
// documents, see the method drive.files.export():
3143
// https://developers.google.com/drive/api/v3/manage-downloads
@@ -60,29 +72,11 @@ async function runSample(fileId) {
6072
});
6173
}
6274

63-
// if invoked directly (not tests), authenticate and run the samples
6475
if (module === require.main) {
6576
if (process.argv.length !== 3) {
6677
throw new Error('Usage: node samples/drive/download.js $FILE_ID');
6778
}
6879
const fileId = process.argv[2];
69-
const scopes = [
70-
'https://www.googleapis.com/auth/drive',
71-
'https://www.googleapis.com/auth/drive.appdata',
72-
'https://www.googleapis.com/auth/drive.file',
73-
'https://www.googleapis.com/auth/drive.metadata',
74-
'https://www.googleapis.com/auth/drive.metadata.readonly',
75-
'https://www.googleapis.com/auth/drive.photos.readonly',
76-
'https://www.googleapis.com/auth/drive.readonly',
77-
];
78-
sampleClient
79-
.authenticate(scopes)
80-
.then(() => runSample(fileId))
81-
.catch(console.error);
80+
runSample(fileId).catch(console.error);
8281
}
83-
84-
// export functions for testing purposes
85-
module.exports = {
86-
runSample,
87-
client: sampleClient.oAuth2Client,
88-
};
82+
module.exports = runSample;

samples/drive/export.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,22 @@
1414
'use strict';
1515

1616
const {google} = require('googleapis');
17-
const sampleClient = require('../sampleclient');
1817
const fs = require('fs');
1918
const os = require('os');
2019
const path = require('path');
20+
const {authenticate} = require('@google-cloud/local-auth');
2121

22-
const drive = google.drive({
23-
version: 'v3',
24-
auth: sampleClient.oAuth2Client,
25-
});
22+
const drive = google.drive('v3');
2623

2724
async function runSample() {
2825
// [START main_body]
26+
// Obtain user credentials to use for the request
27+
const auth = await authenticate({
28+
keyfilePath: path.join(__dirname, '../oauth2.keys.json'),
29+
scopes: 'https://www.googleapis.com/auth/drive.readonly',
30+
});
31+
google.options({auth});
32+
2933
const fileId = '1EkgdLY3T-_9hWml0VssdDWQZLEc8qqpMB77Nvsx6khA';
3034
const destPath = path.join(os.tmpdir(), 'important.pdf');
3135
const dest = fs.createWriteStream(destPath);
@@ -43,14 +47,7 @@ async function runSample() {
4347
// [END main_body]
4448
}
4549

46-
// if invoked directly (not tests), authenticate and run the samples
4750
if (module === require.main) {
48-
const scopes = ['https://www.googleapis.com/auth/drive.readonly'];
49-
sampleClient.authenticate(scopes).then(runSample).catch(console.error);
51+
runSample().catch(console.error);
5052
}
51-
52-
// export functions for testing purposes
53-
module.exports = {
54-
runSample,
55-
client: sampleClient.oAuth2Client,
56-
};
53+
module.exports = runSample;

samples/drive/list.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,20 @@
1313

1414
'use strict';
1515

16+
const path = require('path');
1617
const {google} = require('googleapis');
17-
const sampleClient = require('../sampleclient');
18+
const {authenticate} = require('@google-cloud/local-auth');
1819

19-
const drive = google.drive({
20-
version: 'v3',
21-
auth: sampleClient.oAuth2Client,
22-
});
20+
const drive = google.drive('v3');
2321

2422
async function runSample(query) {
23+
// Obtain user credentials to use for the request
24+
const auth = await authenticate({
25+
keyfilePath: path.join(__dirname, '../oauth2.keys.json'),
26+
scopes: 'https://www.googleapis.com/auth/drive.metadata.readonly',
27+
});
28+
google.options({auth});
29+
2530
const params = {pageSize: 3};
2631
params.q = query;
2732
const res = await drive.files.list(params);
@@ -30,11 +35,6 @@ async function runSample(query) {
3035
}
3136

3237
if (module === require.main) {
33-
const scopes = ['https://www.googleapis.com/auth/drive.metadata.readonly'];
34-
sampleClient.authenticate(scopes).then(runSample).catch(console.error);
38+
runSample().catch(console.error);
3539
}
36-
37-
module.exports = {
38-
runSample,
39-
client: sampleClient.oAuth2Client,
40-
};
40+
module.exports = runSample;

0 commit comments

Comments
 (0)