Directly Using Access Token by googleapis for Node.js

Gists

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.

 Share!