Node using OAuth 2.0 for Server to Server Applications

I am interested in accessing my files (or just a folder) from my Google Drive - and having the access created (and granted) through server-side code. I am not interested in logging in via the Browser.

I've created the OAuth Service Account, downloaded the p12 file and converted it to a .pem file..

BUT I can't see any examples in Node.js to do this. I've seen a few examples on github that use OAuth to access Google Drive - but in fairness a lot of them do not work - which is really frustrating.

Google do not have a Node.js Client API Library for this.

I would love any help or pointing in the right direction if anyone has any thoughts.

Appreciate any help...

pandafinity

This is the guidance and code that got me through it when integrating with the Google Analytics API in the same manner. It should work for Drive too:

https://gist.github.com/PaquitoSoft/4451865

I've gotten further:

Using the Node package "google-oauth-jwt" I've managed to authenticate my request and receive back an Access Token.

var googleAuth = require('google-oauth-jwt');

// obtain a JWT-enabled version of request
var request = googleAuth.requestWithJWT();
var TokenCache = googleAuth.TokenCache;
var tokens = new TokenCache();

tokens.get({
  // use the email address of the service account, as seen in the API console
  email: 'google-generated-email',
  // use the PEM file we generated from the downloaded key
  keyFile: 'pemfile.pem',
  // specify the scopes you wish to access
  scopes: ['https://www.googleapis.com/auth/drive.readonly']
}, function (err, token) {
  console.log('Google API with JWT Request');
  console.log('---------------------------');
  console.log('The GoogleDrive Token is : ' + token);
});

googleAuth.authenticate({
  // use the email address of the service account, as seen in the API console
  email: 'google-generated-email',
  // use the PEM file we generated from the downloaded key
  keyFile: 'pemfile.pem',
  // specify the scopes you wish to access
  scopes: ['https://www.googleapis.com/auth/drive.readonly']
}, function (err, token) {
  console.log('Google API: Authenticated');
  console.log('------------------------');
  console.log('The GoogleDrive Token is : ' + token);
});

Another thing the google-oauth-jwt allows is for a request to be sent using the JWT - but I am either low on coffee or do not really understand what comes back (probably the latter).

I used the following code to make the request:

  request({
  url: 'https://www.googleapis.com/drive/v2/files',
  jwt: {
    // use the email address of the service account, as seen in the API console
    email: 'google-generated-email',
    // use the PEM file we generated from the downloaded key
    keyFile: 'pemfile.pem',
    // specify the scopes you wish to access - each application has different scopes
    scopes: ['https://www.googleapis.com/auth/drive.readonly']
  }
}, function (err, res, body) {
    console.log(JSON.parse(body));
});

The Console.log produces the following:

   { kind: 'drive#fileList',
  etag: '"fk0AzBEIhUhhdZ8fZzKcL1hA5NE/oop8mnyogpISt5nktdy1MHxUDnc"',
  selfLink: 'https://www.googleapis.com/drive/v2/files',
  items: 
   [ { kind: 'drive#file',
       id: '0B1ka-zLvU5tjc3RhcnRlcl9maWxl',
       etag: '"fk0AzBEIhUhhdZ8fZzKcL1hA5NE/MTQxMDM1MTI0MTkzOA"',
       selfLink: 'https://www.googleapis.com/drive/v2/files/0B1ka-zLvU5tjc3RhcnRlcl9maWxl',
       webContentLink: 'https://docs.google.com/uc?id=0B1ka-zLvU5tjc3RhcnRlcl9maWxl&export=download',
       alternateLink: 'https://docs.google.com/file/d/0B1ka-zLvU5tjc3RhcnRlcl9maWxl/edit?usp=drivesdk',
       iconLink: 'https://ssl.gstatic.com/docs/doclist/images/icon_10_pdf_list.png',
       thumbnailLink: 'https://lh6.googleusercontent.com/[somestuff]_ZFIkWw-che6o=s220',
       title: 'How to get started with Drive',
       mimeType: 'application/pdf',
       labels: [Object],
       createdDate: '2014-09-10T12:14:01.938Z',
       modifiedDate: '2014-09-10T12:14:01.938Z',
       markedViewedByMeDate: '1970-01-01T00:00:00.000Z',
       version: '6',
       parents: [Object],
       downloadUrl: 'https://doc-0s-b4-docs.googleusercontent.com/docs/securesc/[somestuff]/0B1ka-zLvU5tjc3RhcnRlcl9maWxl?h=16653014193614665626&e=download&gd=true',
       userPermission: [Object],
       originalFilename: 'How to get started with Drive',
       fileExtension: '',
       md5Checksum: '2f215372c903f401e9e101d1d531e5dd',
       fileSize: '3017667',
       quotaBytesUsed: '0',
       ownerNames: [Object],
       owners: [Object],
       lastModifyingUserName: '',
       lastModifyingUser: [Object],
       editable: true,
       copyable: true,
       writersCanShare: true,
       shared: false,
       appDataContents: false } ] }

I'm trying to get a 'children.list' from the folder (whether root or with folder id) - but this isn't it. Does anyone have any ideas of accessing a folder list with this approach?

Appreciate your help :)