Question

I have been struggling my brain reading the nodejs documentation for google-apis. I gathered a pretty long list of examples, but any of them helps me to do what I want to do. I just want to download a file from my drive using node js.

I have set up the OAUTH and I get an access token using this code (source: http://masashi-k.blogspot.com.es/2013/07/accessing-to-my-google-drive-from-nodejs.html )

var googleDrive = require('google-drive');
var GoogleTokenProvider = require("refresh-token").GoogleTokenProvider,
    async = require('async'),
    request = require('request'),
    _accessToken;

var tokenProvider = new GoogleTokenProvider({
  'refresh_token': REFRESH_TOKEN,
  'client_id' : CLIENT_ID,
  'client_secret': CLIENT_SECRET
});


tokenProvider.getToken(function(err, access_token) {
  console.log("Access Token=", access_token);
  _accessToken = access_token;
});

But I don't know how to continue from here. I tried with things like this with no luck:

function listFiles(token, callback) {
  googleDrive(token).files().get(callback)
}

function callback(err, response, body) {
  if (err) return console.log('err', err)
  console.log('response', response)
  console.log('body', JSON.parse(body))
}

listFiles(_accessToken,callback);

I feel like I'm very close, but I need some help here.

Thanks in advance.

Was it helpful?

Solution

There are two ways of doing that, depending on what you want to download. There is a big difference between downloading native Google Doc files and normal files:

  • Docs have to be downloaded using files.export API method, providing proper mime type to convert doc into
  • Normal files can be downloaded using files.get method, providing correct flag if you want to download file data instead of metadata

I'd suggest using GoogleApis NodeJS library (https://github.com/google/google-api-nodejs-client)

Initializing Drive API:

var Google = require('googleapis');
var OAuth2 = Google.auth.OAuth2;

var oauth2Client = new OAuth2('clientId','clientSecret','redirectUrl');
oauth2Client.setCredentials({
  access_token: 'accessTokenHere'
  refresh_token: 'refreshTokenHere'
});

var drive = Google.drive({
  version: 'v3',
  auth: this.oauth2Client
});

Importing file:

drive.files.get({
  fileId: fileId,
  alt: 'media' // THIS IS IMPORTANT PART! WITHOUT THIS YOU WOULD GET ONLY METADATA
}, function(err, result) {
  console.log(result); // Binary file content here
});

Exporting native Google docs (you have to provide mime type for conversion):

drive.files.export({
  fileId: fileId,
  mimeType: 'application/pdf' // Provide mimetype of your liking from list of supported ones
}, function(err, result) {
  console.log(result); // Binary file content here
});

Maybe it will help someone after so much time ;)

OTHER TIPS

Take a look at the officially supported NodeJS client library for Google APIs at https://github.com/google/google-api-nodejs-client

The code required to get a file from Drive will be similar to that code at the bottom of the README used for inserting a file into Google Drive. You can also test your parameters to the API by using the API Explorer: https://developers.google.com/apis-explorer/#p/drive/v2/drive.files.get

Here's an example call to get a file from Google Drive:

client
  .drive.files.get({ fileId: 'xxxx' })
  .execute(function(err, file) {
  // do something with file here
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top