|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +// [START docs_quickstart] |
| 18 | +const fs = require('fs'); |
| 19 | +const readline = require('readline'); |
| 20 | +const {google} = require('googleapis'); |
| 21 | + |
| 22 | +// If modifying these scopes, delete token.json. |
| 23 | +const SCOPES = ['https://www.googleapis.com/auth/documents.readonly']; |
| 24 | +// The file token.json stores the user's access and refresh tokens, and is |
| 25 | +// created automatically when the authorization flow completes for the first |
| 26 | +// time. |
| 27 | +const TOKEN_PATH = 'token.json'; |
| 28 | + |
| 29 | +// Load client secrets from a local file. |
| 30 | +fs.readFile('credentials.json', (err, content) => { |
| 31 | + if (err) return console.log('Error loading client secret file:', err); |
| 32 | + // Authorize a client with credentials, then call the Google Docs API. |
| 33 | + authorize(JSON.parse(content), printDocTitle); |
| 34 | +}); |
| 35 | + |
| 36 | +/** |
| 37 | + * Create an OAuth2 client with the given credentials, and then execute the |
| 38 | + * given callback function. |
| 39 | + * @param {Object} credentials The authorization client credentials. |
| 40 | + * @param {function} callback The callback to call with the authorized client. |
| 41 | + */ |
| 42 | +function authorize(credentials, callback) { |
| 43 | + const {client_secret, client_id, redirect_uris} = credentials.installed; |
| 44 | + const oAuth2Client = new google.auth.OAuth2( |
| 45 | + client_id, client_secret, redirect_uris[0]); |
| 46 | + |
| 47 | + // Check if we have previously stored a token. |
| 48 | + fs.readFile(TOKEN_PATH, (err, token) => { |
| 49 | + if (err) return getNewToken(oAuth2Client, callback); |
| 50 | + oAuth2Client.setCredentials(JSON.parse(token)); |
| 51 | + callback(oAuth2Client); |
| 52 | + }); |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Get and store new token after prompting for user authorization, and then |
| 57 | + * execute the given callback with the authorized OAuth2 client. |
| 58 | + * @param {google.auth.OAuth2} oAuth2Client The OAuth2 client to get token for. |
| 59 | + * @param {getEventsCallback} callback The callback for the authorized client. |
| 60 | + */ |
| 61 | +function getNewToken(oAuth2Client, callback) { |
| 62 | + const authUrl = oAuth2Client.generateAuthUrl({ |
| 63 | + access_type: 'offline', |
| 64 | + scope: SCOPES, |
| 65 | + }); |
| 66 | + console.log('Authorize this app by visiting this url:', authUrl); |
| 67 | + const rl = readline.createInterface({ |
| 68 | + input: process.stdin, |
| 69 | + output: process.stdout, |
| 70 | + }); |
| 71 | + rl.question('Enter the code from that page here: ', (code) => { |
| 72 | + rl.close(); |
| 73 | + oAuth2Client.getToken(code, (err, token) => { |
| 74 | + if (err) return console.error('Error retrieving access token', err); |
| 75 | + oAuth2Client.setCredentials(token); |
| 76 | + // Store the token to disk for later program executions |
| 77 | + fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => { |
| 78 | + if (err) console.error(err); |
| 79 | + console.log('Token stored to', TOKEN_PATH); |
| 80 | + }); |
| 81 | + callback(oAuth2Client); |
| 82 | + }); |
| 83 | + }); |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * Prints the title of a sample doc: |
| 88 | + * https://docs.google.com/document/d/195j9eDD3ccgjQRttHhJPymLJUCOUjs-jmwTrekvdjFE/edit |
| 89 | + * @param {google.auth.OAuth2} auth The authenticated Google OAuth 2.0 client. |
| 90 | + */ |
| 91 | +function printDocTitle(auth) { |
| 92 | + const docs = google.docs({version: 'v1', auth}); |
| 93 | + docs.documents.get({ |
| 94 | + documentId: '195j9eDD3ccgjQRttHhJPymLJUCOUjs-jmwTrekvdjFE', |
| 95 | + }, (err, res) => { |
| 96 | + if (err) return console.log('The API returned an error: ' + err); |
| 97 | + console.log(`The title of the document is: ${res.data.title}`); |
| 98 | + }); |
| 99 | +} |
| 100 | +// [END docs_quickstart] |
0 commit comments