Andre M
Andre M

Reputation: 7534

Fetching files from Google Drive, from command-line in Node.js?

To facilitate updating of content on our information screen, we are looking at putting the content on Google Drive and then allowing the content to be synced via a NodeJS based application.

At the moment I am trying to test this approach using folder shared from my own account.

What I have so far, based on the documentation at https://github.com/google/google-api-nodejs-client/tree/master :

var google = require('googleapis');
var OAuth2 = google.auth.OAuth2;
var readline = require('readline');

const CLIENT_ID = 'xxxxxxxx.apps.googleusercontent.com';
const CLIENT_SECRET = '7h3c13n7s3cr37';
const REDIRECT_URL = 'https://accounts.google.com/o/oauth2/auth';

var oauth2Client = new OAuth2(
CLIENT_ID, CLIENT_SECRET, REDIRECT_URL);

var scopes = [
'https://www.googleapis.com/auth/drive.readonly',
'https://www.googleapis.com/auth/drive.metadata.readonly'
];

var url = oauth2Client.generateAuthUrl({
  access_type: 'offline', // 'online' (default) or 'offline' (gets refresh_token)
  scope: scopes // If you only need one scope you can pass it as string
});

console.log('past following URL into a web browser');
console.log(url);

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

rl.question('Provide Key generated on web page ', (answer) => {

    // store response key in file?

    var drive = google.drive({ version: 'v2', auth: oauth2Client });

    var folderId = 'mif01d3r';

    drive.children.list({
      auth: answer,
      folderId: folderId,
    }, function(error, response) {
       if (error) {
          console.log('err: ', error);
          return;
       }
       console.log(response);
    });

});

The current issue here is that the value for 'REDIRECT_URL' does not seem suitable. What should I be putting here for a command line application?

Upvotes: 0

Views: 844

Answers (2)

pinoyyid
pinoyyid

Reputation: 22286

There is a much easier way. Manually generate a refresh token and then reference that (securely of course) from your app. In this way there is no auth required, no redirect URL's, etc etc.

See How do I authorise an app (web or installed) without user intervention? (canonical ?)

Upvotes: 2

some1
some1

Reputation: 867

https://developers.google.com/api-client-library/python/auth/installed-app#choosingredirecturi is more for Python, but the below is applicable to all "Installed Applications"

urn:ietf:wg:oauth:2.0:oob

This value signals to the Google Authorization Server that the authorization code should be returned in the title bar of the browser, with the page text prompting the user to copy the code and paste it in the application. This is useful when the client (such as a Windows application) cannot listen on an HTTP port without significant client configuration.

See also: https://developers.google.com/identity/protocols/OAuth2InstalledApp#choosingredirecturi


so it should be:

const REDIRECT_URL = 'urn:ietf:wg:oauth:2.0:oob';

Upvotes: 1

Related Questions