From 27c422ba6ce8292ceb406a1b62179f0b92a49566 Mon Sep 17 00:00:00 2001 From: Jason Dobry Date: Thu, 18 Aug 2016 17:08:14 -0700 Subject: [PATCH 1/3] Add Drive v3 API sample. --- samples/drive/README.md | 26 +++++++++++++ samples/drive/download.js | 79 +++++++++++++++++++++++++++++++++++++++ samples/sampleclient.js | 36 +++++++++++++----- 3 files changed, 131 insertions(+), 10 deletions(-) create mode 100644 samples/drive/README.md create mode 100644 samples/drive/download.js diff --git a/samples/drive/README.md b/samples/drive/README.md new file mode 100644 index 0000000000..6b2464a72a --- /dev/null +++ b/samples/drive/README.md @@ -0,0 +1,26 @@ +# Drive v3 API Samples + +This samples allows you to download a single file from Google Drive. + +## Running the sample + +Set the following values in `secret.json` (up one directory): + +* `client_id` +* `project_id` +* `client_secret` + +Run the sample: + +``` +node download.js +``` + +where `` is the id of a file in Google Drive and `localPath` is the location +where the file should be saved. + +Example: + +``` +node download.js 0B_Klegupc5gUcXhFZjZVUV9NeE0 ./file.txt +``` diff --git a/samples/drive/download.js b/samples/drive/download.js new file mode 100644 index 0000000000..d09e0e0e87 --- /dev/null +++ b/samples/drive/download.js @@ -0,0 +1,79 @@ +// Copyright 2016, Google, Inc. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +var google = require('../../'); +var sampleClient = require('../sampleclient'); +var fs = require('fs'); + +var auth = sampleClient.oAuth2Client; + +var drive = google.drive({ + version: 'v3', + auth: auth +}); + +function download (fileId, tokens) { + drive.files.get({ + fileId: fileId + }, function (err, metadata) { + if (err) { + console.error(err); + return process.exit(); + } + + console.log('Downloading %s...', metadata.name); + + auth.setCredentials(tokens); + + var dest = fs.createWriteStream(metadata.name); + + drive.files.get({ + fileId: fileId, + alt: 'media' + }) + .on('error', function(err) { + console.log('Error downloading file', err); + process.exit(); + }) + .pipe(dest); + + dest + .on('finish', function () { + console.log('Downloaded %s!', metadata.name); + process.exit(); + }) + .on('error', function (err) { + console.log('Error writing file', err); + process.exit(); + }); + }) +} + +var scopes = [ + 'https://www.googleapis.com/auth/drive.metadata.readonly', + 'https://www.googleapis.com/auth/drive.photos.readonly', + 'https://www.googleapis.com/auth/drive.readonly' +]; + +if (module === require.main) { + var args = process.argv.slice(2); + if (!args[0]) { + throw new Error('fileId required!'); + } else { + sampleClient.execute(scopes, function (tokens) { + download(args[0], tokens); + }); + } +} diff --git a/samples/sampleclient.js b/samples/sampleclient.js index 7919eca781..fcb8ad9017 100644 --- a/samples/sampleclient.js +++ b/samples/sampleclient.js @@ -26,6 +26,30 @@ var url = require('url'); var querystring = require('querystring'); var secrets = require('./secrets.json'); +var called = false; + +function callOnce (callback) { + if (!called) { + called = true; + callback(); + } +} + +function handler(request, response, server, callback) { + var self = this; + var qs = querystring.parse(url.parse(request.url).query); + self.oAuth2Client.getToken(qs.code, function (err, tokens) { + if (err) { + console.error('Error getting oAuth tokens: ' + err); + } + self.oAuth2Client.setCredentials(tokens); + self.isAuthenticated = true; + response.end('Authentication successful! Please return to the console.'); + callback(tokens); + server.close(); + }); +} + function SampleClient (options) { var self = this; self.isAuthenticated = false; @@ -48,16 +72,8 @@ function SampleClient (options) { scope: scopes.join(' ') }); var server = http.createServer(function (request, response) { - var qs = querystring.parse(url.parse(request.url).query); - self.oAuth2Client.getToken(qs.code, function (err, tokens) { - if (err) { - console.error('Error getting oAuth tokens: ' + err); - } - self.oAuth2Client.setCredentials(tokens); - self.isAuthenticated = true; - response.end('Authentication successful! Please return to the console.'); - callback.apply(); - server.close(); + callOnce(function () { + handler.call(self, request, response, server, callback); }); }).listen(8080, function () { // open the browser to the authorize url to start the workflow From 9a283104242eb6b28d1264afb3ccde08a5b4ae67 Mon Sep 17 00:00:00 2001 From: Jason Dobry Date: Thu, 18 Aug 2016 17:39:51 -0700 Subject: [PATCH 2/3] Made lint happy. --- samples/drive/download.js | 4 ++-- samples/sampleclient.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/samples/drive/download.js b/samples/drive/download.js index d09e0e0e87..3875e66cf9 100644 --- a/samples/drive/download.js +++ b/samples/drive/download.js @@ -43,7 +43,7 @@ function download (fileId, tokens) { fileId: fileId, alt: 'media' }) - .on('error', function(err) { + .on('error', function (err) { console.log('Error downloading file', err); process.exit(); }) @@ -58,7 +58,7 @@ function download (fileId, tokens) { console.log('Error writing file', err); process.exit(); }); - }) + }); } var scopes = [ diff --git a/samples/sampleclient.js b/samples/sampleclient.js index fcb8ad9017..4db062434d 100644 --- a/samples/sampleclient.js +++ b/samples/sampleclient.js @@ -35,7 +35,7 @@ function callOnce (callback) { } } -function handler(request, response, server, callback) { +function handler (request, response, server, callback) { var self = this; var qs = querystring.parse(url.parse(request.url).query); self.oAuth2Client.getToken(qs.code, function (err, tokens) { From 75f2d39f1b53bf23964002270050ecf1b64d548f Mon Sep 17 00:00:00 2001 From: Jason Dobry Date: Thu, 18 Aug 2016 21:29:09 -0700 Subject: [PATCH 3/3] Add export example. --- samples/drive/README.md | 25 +++++++++---- samples/drive/export.js | 79 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+), 6 deletions(-) create mode 100644 samples/drive/export.js diff --git a/samples/drive/README.md b/samples/drive/README.md index 6b2464a72a..d310a9a30d 100644 --- a/samples/drive/README.md +++ b/samples/drive/README.md @@ -2,7 +2,7 @@ This samples allows you to download a single file from Google Drive. -## Running the sample +## Running the samples Set the following values in `secret.json` (up one directory): @@ -10,17 +10,30 @@ Set the following values in `secret.json` (up one directory): * `project_id` * `client_secret` -Run the sample: +__Run the `download.js` sample:__ ``` -node download.js +node download.js ``` -where `` is the id of a file in Google Drive and `localPath` is the location -where the file should be saved. +where `` is the id of any file in Google Drive. Example: ``` -node download.js 0B_Klegupc5gUcXhFZjZVUV9NeE0 ./file.txt +node download.js 0B_Klegupc5gUcXhFZjZVUV9NeE0 +``` + +__Run the `export.js` sample:__ + +``` +node export.js = +``` + +where `` is the id of a _Google Doc_ in Google Drive. + +Example: + +``` +node export.js 0B_Klegupc5gUcXhFZjZVUV9NeE0 ./file.pdf ``` diff --git a/samples/drive/export.js b/samples/drive/export.js new file mode 100644 index 0000000000..882f481832 --- /dev/null +++ b/samples/drive/export.js @@ -0,0 +1,79 @@ +// Copyright 2016, Google, Inc. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +var google = require('../../'); +var sampleClient = require('../sampleclient'); +var fs = require('fs'); + +var auth = sampleClient.oAuth2Client; + +var drive = google.drive({ + version: 'v3', + auth: auth +}); + +function download (fileId, tokens) { + drive.files.get({ + fileId: fileId + }, function (err, metadata) { + if (err) { + console.error(err); + return process.exit(); + } + + console.log('Downloading %s...', metadata.name); + + auth.setCredentials(tokens); + + var dest = fs.createWriteStream(metadata.name + '.pdf'); + + drive.files.export({ + fileId: fileId, + mimeType: 'application/pdf' + }) + .on('error', function (err) { + console.log('Error downloading file', err); + process.exit(); + }) + .pipe(dest); + + dest + .on('finish', function () { + console.log('Downloaded %s!', metadata.name); + process.exit(); + }) + .on('error', function (err) { + console.log('Error writing file', err); + process.exit(); + }); + }); +} + +var scopes = [ + 'https://www.googleapis.com/auth/drive.metadata.readonly', + 'https://www.googleapis.com/auth/drive.photos.readonly', + 'https://www.googleapis.com/auth/drive.readonly' +]; + +if (module === require.main) { + var args = process.argv.slice(2); + if (!args[0]) { + throw new Error('fileId required!'); + } else { + sampleClient.execute(scopes, function (tokens) { + download(args[0], tokens); + }); + } +}