question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Drive download returning token JSON object

See original GitHub issue

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?

Issue Analytics

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

github_iconTop GitHub Comments

5reactions
robotpapiercommented, Jul 3, 2017

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.

4reactions
jmdobrycommented, Aug 23, 2016

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!
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Google Drive API returning [Object] - node.js
You want to achieve this using googleapis@39.0.0 for Node.js. You have already been able to download a file from Google Drive using Drive...
Read more >
Download files | Google Drive
To download a file stored on Google Drive, use the files.get method with the ID of the file to download ... import com.google.api.client.googleapis.json....
Read more >
HTTP status and error codes for JSON | Cloud Storage
Cloud Storage uses the standard HTTP error reporting format for the JSON API. Successful requests return HTTP status codes in the 2xx range....
Read more >
Authorize send requests | Firebase Cloud Messaging - Google
To mint this token, you can use Google Application Default Credentials (in ... need to download a service account JSON file from your...
Read more >
Net-Google-Drive-Simple-0.17.readme
The script will then receive an access token from Google Drive and store it in ... is an object composed of the JSON...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found