Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Created November 18, 2018 23:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanaikech/ca11c53356c5466e60109f79d9e4d9c9 to your computer and use it in GitHub Desktop.
Save tanaikech/ca11c53356c5466e60109f79d9e4d9c9 to your computer and use it in GitHub Desktop.
Directly Using Access Token by googleapis for Node.js

Directly Using Access Token by googleapis for Node.js

This sample script is for directly using the refreshed access token by googleapis for Node.js. When oauth2Client.refreshAccessToken((err, tokens) => {}); is used to retrieve the refreshed access token, the following error occurs.

DeprecationWarning: The refreshAccess Token method has been deprecated, and will be removed in the 3.0 release of goo gle-auth-library. Please use the getRequestHeaders method instead.

It is required to use getRequestHeaders(). But I couldn't find the sample script using getRequestHeaders(). So I created this sample script. If this was useful for you, I'm glad.

function listFiles(auth) {
  const request = require('request');

  auth.getRequestHeaders()
  .then((authorization) => {
    let qs = {
      "orderBy": "name",
      "pageSize": 5,
      "q": "mimeType='application/vnd.google-apps.spreadsheet'",
      "fields": "files(id,name)",
    };
    let options = {
      url: "https://www.googleapis.com/drive/v3/files",
      qs: qs,
      method: "get",
      headers: authorization,
    };
    request(options, (err, res, result) => {
      if (err) {
        console.log(err);
        return;
      }
      console.log(result);
    });
  });
}
  • In this sample script, listFiles(auth) in the quickstart script is used. When you use the quickstart script, please replace listFiles(auth) to the above script.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment