getIdForEmail() 不是 Google Drive API 权限中的函数

getIdForEmail() not a function in Google Drive API Permissions

我需要更改每个上传文件的权限。但是当我尝试添加这段代码时,

printPermissionIdForEmail(email) {
var request = gapi.client.drive.permissions.getIdForEmail({
  'email': email,
});
request.execute(function(resp) {
  return ('ID: ' + resp.id);
});

}

我收到 getIdForEmail 不是函数的错误。

gapi.client.init, gapi.auth2.getAuthInstance(), 

正在工作。但为什么 gapi.client.drive.permissions.getIdForEmail 不起作用?有什么我需要做的吗?在 Google 开发者页面?在我的代码中?

getIdForEmail 是一种仅在 Google Drive v2 中可用的方法。

对于 V3,您将不得不以另一种方式追求它。

执行 files.list with the q parameter. In the q parameter supply the user whos permissions you wish to change. You can see here how to use search 这将找到 someuser 是所有者的所有文件。

'someuser@gmail.com' in owners

然后您将获得 file resources you can then check the permissions on each file using permissions.list 的列表并使用它来更改您需要的列表。

我不是 JavaScript 开发人员,但我在文档中找到了它,它展示了如何使用搜索来列出文件。

  /**
   * Print files.
   */
  function listFiles() {
    gapi.client.drive.files.list({
      'q': "'someuser@gmail.com' in owners",
      'fields': "*"
    }).then(function(response) {
      appendPre('Files:');
      var files = response.result.files;
      if (files && files.length > 0) {
        for (var i = 0; i < files.length; i++) {
          var file = files[i];
          appendPre(file.name + ' (' + file.id + ')');
        }
      } else {
        appendPre('No files found.');
      }
    });
  }

更新:

我刚发现这个。 About.get获取有关用户、用户的驱动器和系统功能的信息

{
 "user": {
  "kind": "drive#user",
  "displayName": "Linda Lawton",
  "photoLink": "xxxx",
  "me": true,
  "permissionId": "060305882255734372",
  "emailAddress": "xxxx@gmail.com"
 }
}

这会不会是您正在寻找的相同 permissionId?

我使用的方法是基于script.google.com发表的the OAuth2 library。这是为具有 domain-wide 委托的 Google Apps 脚本编写的。这里的关键是为 UrlFetchApp.fetch(url, options) 构建有效的 urloption,然后解析结果以找到 ID 号。

function getIdForEmailv3(userEmail) {
  var service = getService(userEmail);
  if (service.hasAccess()) {
    Logger.log('getIdForEmailv3(%s) has access', userEmail);

    var url = 'https://www.googleapis.com/drive/v3/about' + '?fields=user/permissionId'
    var options = {
      'method': 'get',
      'contentType': 'application/json',
      'headers': { Authorization: 'Bearer ' + service.getAccessToken() },
      'muteHttpExceptions': true
    };

    var response = UrlFetchApp.fetch(url, options);

    var resultString = JSON.stringify(response.getContentText());
    var regex = new RegExp(/\d+/g);
    var id = regex.exec(resultString)[0];

    Logger.log('getIdForEmailv3 returned %s for %s', id, userEmail);
    return id

  } else {

    Logger.log('getIdForEmailv3 getLastError: %s', service.getLastError());
    Logger.log('getIdForEmailv3 returned %s for %s', 0, userEmail);
    return 0;
  }
}

正则表达式的想法来自:Easiest way to get file ID from URL on Google Apps Script

解决方案评论中的字段格式: