Reputation: 131
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!
Upvotes: 0
Views: 488
Reputation: 22296
"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
Upvotes: 1
Reputation: 413
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.
Upvotes: 1