Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions samples/drive/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Drive v3 API Samples

This samples allows you to download a single file from Google Drive.

## Running the samples

Set the following values in `secret.json` (up one directory):

* `client_id`
* `project_id`
* `client_secret`

__Run the `download.js` sample:__

```
node download.js <fileId>
```

where `<fileId>` is the id of any file in Google Drive.

Example:

```
node download.js 0B_Klegupc5gUcXhFZjZVUV9NeE0
```

__Run the `export.js` sample:__

```
node export.js <fileId>=
```

where `<fileId>` is the id of a _Google Doc_ in Google Drive.

Example:

```
node export.js 0B_Klegupc5gUcXhFZjZVUV9NeE0 ./file.pdf
```
79 changes: 79 additions & 0 deletions samples/drive/download.js
Original file line number Diff line number Diff line change
@@ -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);
});
}
}
79 changes: 79 additions & 0 deletions samples/drive/export.js
Original file line number Diff line number Diff line change
@@ -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);
});
}
}
36 changes: 26 additions & 10 deletions samples/sampleclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down