使用 Google 驱动器的 API 如何将共享的 google 驱动器文件同步到另一台服务器?

Using Google Drive's API how can I sync my shared google drive files to another server?

我想访问我自己的 google 驱动器,并将其文件同步到我的服务器。比如我们办公室集体编辑,在Drive上添加文件很容易,如果我能在服务器上拿到它们,我想在我们的网页上使用它们。我知道我可以使用文件 ID 访问单个文件,但我需要查看父 "folders"(驱动器中的文件)的全部内容,然后从那里开始。

这似乎需要 Oauth 或类似的,但如果它是一个公共共享文件夹,并且我没有尝试访问用户驱动器,(对,我正在访问我自己的驱动器)我不知道如何去做。任何帮助将不胜感激!

您可以使用 Google Drive API

示例来自 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);
  }
}

或者您可以使用 Google Apps 脚本来创建其余 api returns 文件信息。 Using the Execution API

示例代码:

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;
}

以上代码 returns 包含文件 ID、名称、大小和下载的对象列表 url。

希望对您有所帮助。

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

但是 1/ Google 怎么知道是你? 2/ Google 如何知道该应用程序是您已授予权限的应用程序?

这两个问题的答案都是 OAuth,假设您想使用应用程序访问文件。

所以您的应用的伪代码看起来像这样。 - 获取访问令牌 - 使用该访问令牌获取父文件夹。保存它的ID - 使用父文件夹的 ID 获取其所有子文件夹的内容