|
14 | 14 | 'use strict'; |
15 | 15 |
|
16 | 16 | const {google} = require('googleapis'); |
17 | | -const sampleClient = require('../sampleclient'); |
18 | 17 | const fs = require('fs'); |
19 | 18 | const os = require('os'); |
20 | 19 | const uuid = require('uuid'); |
21 | 20 | const path = require('path'); |
| 21 | +const {authenticate} = require('@google-cloud/local-auth'); |
22 | 22 |
|
23 | | -const drive = google.drive({ |
24 | | - version: 'v3', |
25 | | - auth: sampleClient.oAuth2Client, |
26 | | -}); |
| 23 | +const drive = google.drive('v3'); |
27 | 24 |
|
28 | 25 | 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 | + |
29 | 41 | // For converting document formats, and for downloading template |
30 | 42 | // documents, see the method drive.files.export(): |
31 | 43 | // https://developers.google.com/drive/api/v3/manage-downloads |
@@ -60,29 +72,11 @@ async function runSample(fileId) { |
60 | 72 | }); |
61 | 73 | } |
62 | 74 |
|
63 | | -// if invoked directly (not tests), authenticate and run the samples |
64 | 75 | if (module === require.main) { |
65 | 76 | if (process.argv.length !== 3) { |
66 | 77 | throw new Error('Usage: node samples/drive/download.js $FILE_ID'); |
67 | 78 | } |
68 | 79 | 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); |
82 | 81 | } |
83 | | - |
84 | | -// export functions for testing purposes |
85 | | -module.exports = { |
86 | | - runSample, |
87 | | - client: sampleClient.oAuth2Client, |
88 | | -}; |
| 82 | +module.exports = runSample; |
0 commit comments