google-api-nodejs-client: Drive download returning token JSON object

Hello, I’m having a problem downloading from the Drive using this API (v3). The data returned from the API is of the form:

{
  "access_token" : "HUGE_TOKEN_STRING",
  "token_type" : "Bearer",
  "expires_in" : 3600
}

My code is roughly as follows (parts ommited due to confidentiality reasons):

  var buffer = new Buffer(0);
  drive.files.get({
      auth: auth,
      alt: 'media',
      fileId: id,
  })
  .on('error', function (err) {
    logger.error("Error " + err);
    callback(err, null);
  })
  .on('data', function (data) {
    logger.info("Got data!");
    buffer = Buffer.concat([buffer, data], buffer.length + data.length);
  })
  .on('end', function () {
    logger.info("End event!");
    callback(null, buffer);
  });

auth is an object created using a derivation of this example: https://developers.google.com/drive/v3/web/quickstart/nodejs#step_3_set_up_the_sample

Am I doing something wrong, or has the API changed somehow?

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 5
  • Comments: 27 (8 by maintainers)

Commits related to this issue

Most upvoted comments

Hi, I have similar issue. Turned out my problem was the access token getting expired. So I added a manual refresh before the oauth2Client callback in the node.js quickstart google api example link

replaced

function authorize(credentials, callback) {
  var clientSecret = credentials.installed.client_secret;
  var clientId = credentials.installed.client_id;
  var redirectUrl = credentials.installed.redirect_uris[0];
  var auth = new googleAuth();
  var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);

  // Check if we have previously stored a token.
  fs.readFile(TOKEN_PATH, function(err, token) {
    if (err) {
      getNewToken(oauth2Client, callback);
    } else {
      oauth2Client.credentials = JSON.parse(token);
      callback(oauth2Client);
    }
  });
}

by

function authorize(credentials, callback) {
  var clientSecret = credentials.installed.client_secret;
  var clientId = credentials.installed.client_id;
  var redirectUrl = credentials.installed.redirect_uris[0];
  var auth = new googleAuth();
  var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);

  // Check if we have previously stored a token.
  fs.readFile(TOKEN_PATH, function(err, token) {
    if (err) {
      getNewToken(oauth2Client, callback);
    } else {

      oauth2Client.credentials = JSON.parse(token);

      // === ALWAYS refresh token and do the callback only when it is refreshed
      oauth2Client.refreshAccessToken(function(err, token) {
        // your access_token is now refreshed and stored in oauth2Client
        // store these new tokens in a safe place (e.g. database)
        callback(oauth2Client);
      });
    }
  });
}

Don’t know if it’s the best way but hope it helps.

I may need to re-open this issue, but I’d like to see if 12.4.0 works for anybody as far as the encoding goes. You should now be able to do:

drive.files.get({
  fileId: 'asxKJod9s79',
  alt: 'media'
}, {
  encoding: null // Make sure we get the binary data
}, function (err, buffer) {
  // Nice, the buffer is actually a Buffer!
});

Agreed, pipe should not leak credentials. Opened #625