ID | Title | Duration (ms) |
---|---|---|
1 | Google API Drive ListFiles | 1080 |
Line | Lint | Hits | Source |
---|---|---|---|
1 | 'use strict'; | ||
2 | // configFilePath should be a string with a path to a client_secret.json. This folder needs to be writable in order to have a token saved/managed | ||
3 | // process.env.PATH_TO_CLIENT_SECRET_JSON is expected to have a valid path to a OAuth/Other/client_secret.json | ||
4 | // as provided by the steps at https://console.cloud.google.com/apis/credentials. A detailed tutorial can be | ||
5 | // found in Step 1 at https://developers.google.com/drive/v3/web/quickstart/nodejs. | ||
6 | |||
7 | 1 | module.exports = function(configFilePath) { | |
8 | |||
9 | const README = '\n\nThis script is intended to help you use a google API \ | ||
10 | client_secret.json file to obtain an OAUTH token (json data) and \ | ||
11 | store it next to the source .json for future use. \n\n\ | ||
12 | To acomplish this, you will be given an URL that you can follow to \ | ||
13 | get a "code" which can then be used to get a token. As an I/O solution \ | ||
14 | I run this script with `node --inspect-brk` and then I catch the debugger \ | ||
15 | after the URL is logged to the console. Once I have the code, I can run \ | ||
16 | `code="{codeFromUrl}"` and resume the script. \n\n\n'; | ||
17 | |||
18 | 1 | const path = require('path'), | |
19 | fs = require('fs'), | ||
20 | keyfile = path.normalize(configFilePath) ? path.normalize(configFilePath) : ( process.env.PATH_TO_CLIENT_SECRET_JSON && process.env.PATH_TO_CLIENT_SECRET_JSON.length > 1 ? path.normalize(process.env.PATH_TO_CLIENT_SECRET_JSON) : null ), |
||
21 | tokenfile = keyfile.replace(/\.json$/g, '') + '.token', | ||
22 | keys = JSON.parse(fs.readFileSync(keyfile)), | ||
23 | scopes = ['https://www.googleapis.com/auth/drive'], | ||
24 | {google} = require('googleapis'); | ||
25 | |||
26 | if ( process.env.NODE_ENV=='dev' ) console.log( 'Trying with keyfile: ', keyfile ); |
||
27 | |||
28 | if ( keyfile.length > 1 ) { |
||
29 | |||
30 | 1 | return new Promise((resolve, reject) => { | |
31 | |||
32 | let client = new google.auth.OAuth2( | ||
33 | keys.web.client_id, | ||
34 | keys.web.client_secret, | ||
35 | keys.web.redirect_uris[0] | ||
36 | ), | ||
37 | code = null; | ||
38 | |||
39 | if ( fs.existsSync(tokenfile) ) { |
||
40 | 1 | let token = JSON.parse(fs.readFileSync(tokenfile)); | |
41 | if ( token.refresh_token && token.access_token) { |
||
42 | 1 | client.credentials = token; | |
43 | if ( process.env.NODE_ENV=='dev' ) console.log('Loaded existing tokenfile: ' + tokenfile); |
||
44 | } | ||
45 | } | ||
46 | |||
47 | if ( client.credentials && ( client.credentials.access_token || client.credentials.refresh_token ) ) { |
||
48 | 1 | resolve(client); | |
49 | } else { | ||
50 | if ( process.env.NODE_ENV=='dev' ) console.log(README); | ||
51 | //Start with node --inspect-brk so you have time to hit this then retrieve the code and `code='{yournewcode}'` in the console | ||
52 | console.log('\nGet code from:\n\n' + client.generateAuthUrl({ access_type: 'offline', scope: scopes })); | ||
53 | console.log('\nWhen you have that code, return and run `code="yourNewCode"` in the console and resume the debugger.\n\n'); | ||
54 | debugger; | ||
55 | |||
56 | client.getTokenAsync({ code: code }) | ||
57 | .then(res => { | ||
58 | if (res.tokens) { |
||
59 | let tokens = res.tokens; | ||
60 | fs.writeFile(tokenfile, JSON.stringify(tokens), err => { | ||
61 | if (err) { |
||
62 | console.error('Error writing token file.'); | ||
63 | console.dir(err); | ||
64 | } |
||
65 | if ( process.env.NODE_ENV=='dev' ) console.log("Token file written to:" + tokenfile); | ||
66 | client.credentials = tokens; | ||
67 | resolve(client); | ||
68 | }); | ||
69 | } else { | ||
70 | console.error('Error getting oAuth tokens. See res object for more details'); | ||
71 | console.dir(res); | ||
72 | } |
||
73 | }) | ||
74 | .catch(err => { | ||
75 | console.error('Error getting oAuth tokens'); |
||
76 | console.dir(err); | ||
77 | }); | ||
78 | |||
79 | } | ||
80 | |||
81 | }); | ||
82 | |||
83 | } else { | ||
84 | throw error('configFilePath not provided'); | ||
85 | } | ||
86 | |||
87 | } |