【问题标题】:getIdForEmail() not a function in Google Drive API PermissionsgetIdForEmail() 不是 Google Drive API 权限中的函数
【发布时间】:2017-08-04 18:09:12
【问题描述】:

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

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

}

我得到一个错误,getIdForEmail is not a function。

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

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

【问题讨论】:

    标签: javascript google-api google-drive-api google-api-js-client


    【解决方案1】:

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

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

    使用 q 参数执行 files.list。在 q 参数中提供您希望更改其权限的用户。您可以在此处查看如何使用search 这将找到 someuser 是所有者的所有文件。

    所有者中的“someuser@gmail.com”

    然后您将获得file resources 的列表,然后您可以使用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 获取有关用户、用户的 Drive 和系统功能的信息

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

    这可能是您正在寻找的同一个权限 ID?

    【讨论】:

    【解决方案2】:

    我使用的方法是基于在 script.google.com 上发布的the OAuth2 library。这是为具有域范围委派的 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

    解决方案评论中的字段格式:How to add 'field' property to a Google Drive API v3 call in JavaScript?

    【讨论】:

      猜你喜欢
      • 2016-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-09
      相关资源
      最近更新 更多