0

I would like to access my own google drive, and sync its files to my server. For example, its easy for our office to edit collectively and add files on Drive, and if I can just get them on the server, I would like to use them on our web page. I know I can access individual files with the file id, but I need to see the parent "folders" (a file in drive) entire contents, and go from there.

It seems that requires Oauth, or similar, but if it's a publicly shared folder, and I'm not trying to access the users drive, (right, I'm accessing my own) I cannot figure out how to do that. Any help would be most appreciated!

Robert Jackson
  • 131
  • 2
  • 9
  • And what's wrong with Oauth? You have your credentials. If you make an app and register it, then give permission for it to access your drive, you should be able to use Oauth to let the app get what you want it to get. So maybe you should spell it out, what's the problem? – joshp Dec 03 '15 at 04:17
  • is your question how to use the api without oauth since its a public folder? – Zig Mandel Dec 03 '15 at 05:38
  • I may just not be understanding it correctly. But I was thinking that Oauth as is, or as google examples explain, would want to check which user is accessing the system, generate a new token based on the credentials, once they allow access. Since it's server to server I'm a little lost as to how that process goes. – Robert Jackson Dec 03 '15 at 15:28
  • @ZigMandel, Not necessarily. My question is more or less how can I use Oauth without being a user actively accepting the request for access? It seems trivial when I say it, but I'm not seeing anything in the examples that does anything remotely like that. In addition there are a number of credential types, that I can set up in the dev console, but, aren't really clear to me how to use them in this way. – Robert Jackson Dec 03 '15 at 15:55

2 Answers2

1

You can use the Google Drive API

Sample From Google Drive API Reference Page.

/**
 * Print a file's metadata.
 *
 * @param {String} fileId ID of the file to print metadata for.
 */
 function printFile(fileId) {
   var request = gapi.client.drive.files.get({
     'fileId': fileId
   });
  request.execute(function(resp) {
    console.log('Title: ' + resp.title);
    console.log('Description: ' + resp.description);
    console.log('MIME type: ' + resp.mimeType);
  });
}

/**
 * Download a file's content.
 *
 * @param {File} file Drive File instance.
 * @param {Function} callback Function to call when the request is complete.
 */
function downloadFile(file, callback) {
  if (file.downloadUrl) {
    var accessToken = gapi.auth.getToken().access_token;
    var xhr = new XMLHttpRequest();
    xhr.open('GET', file.downloadUrl);
    xhr.setRequestHeader('Authorization', 'Bearer ' + accessToken);
    xhr.onload = function() {
      callback(xhr.responseText);
    };
    xhr.onerror = function() {
      callback(null);
    };
    xhr.send();
  } else {
    callback(null);
  }
}

Or you can use a Google Apps Script to create a rest api that returns file information. Using the Execution API

Sample Code:

function GetFiles() {

var result = [];

var files = DriveApp.getFiles();
while (files.hasNext()) {
  var file = files.next();
  var fileInfo = new Object();
  fileInfo.id = file.getId();
  fileInfo.name = file.getName();
  fileInfo.size = file.getSize();
  fileInfo.url = file.getDownloadUrl();
  result.push(fileInfo);
}
  return result;
}

The above code returns a list of Objects that contain the file's id, name, size, and the download url.

Hope that helps.

Eric Dunaway
  • 406
  • 6
  • 14
1

"I'm not trying to access the users drive, (right, I'm accessing my own)".

But 1/ how does Google know it's you? 2/ how does Google know that the app is one that you have granted permissions to?

The answer to both questions is OAuth, assuming that you want to access the files using an app.

So the pseudo code for your app will look something like. - get an Access token - use that access token to get the parent folder. save its ID - use the ID of the parent folder to get the contents of all of its children

pinoyyid
  • 21,499
  • 14
  • 64
  • 115
  • Yes, this makes sense. I think I lack some understanding as to how my app is supposed to identify as me using Oauth, when I'm doing this in a program, but not clicking the screen accepting that access request. – Robert Jackson Dec 03 '15 at 15:35
  • This might help http://stackoverflow.com/questions/19766912/how-do-i-authorise-an-app-web-or-installed-without-user-intervention-canonic – pinoyyid Dec 04 '15 at 10:32