如何修复Google Drive API中的“资源主体包含不可直接写的字段”警告?

时间:2019-02-12 10:45:37

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

我正在使用Google API Docs v3中的示例(这是一个在线示例,可查看错误)。尝试更新文件权限时,出现以下错误:

  

资源正文包含不可直接写的字段。

文件权限需要更新,以允许任何人访问URL,因此我们可以将“上载”文件用作共享工具。如果删除“ 类型”选项,脚本将运行。阅读与此有关的其他问题,包括this,但无助于解决此问题。总体而言,目标是使用户从Google Drive Picker中选择的文件可共享。任何指针都很棒。

下面的代码(与上面的URL相同)。

BenchmarkIntMapGet10-4          100000000           15.6 ns/op
BenchmarkIntMapGet100-4         100000000           17.1 ns/op
BenchmarkIntMapGet1000-4        50000000            25.7 ns/op
BenchmarkIntMapGet10000-4       50000000            32.3 ns/op
BenchmarkIntMapGet100000-4      30000000            39.2 ns/op
BenchmarkIntMapGet1000000-4     20000000            67.2 ns/op
BenchmarkIntMapGet10000000-4    20000000            82.3 ns/op

总而言之,我尝试使用以下内容更新用户角色/类型,但是即使没有错误,当前字段也不会更新。

<script src="https://apis.google.com/js/api.js"></script>
<script>
  /**
   * Sample JavaScript code for drive.permissions.update
   * See instructions for running APIs Explorer code samples locally:
   * https://developers.google.com/explorer-help/guides/code_samples#javascript
   */

  function authenticate() {
    return gapi.auth2.getAuthInstance()
        .signIn({scope: "https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/drive.file"})
        .then(function() { console.log("Sign-in successful"); },
              function(err) { console.error("Error signing in", err); });
  }
  function loadClient() {
    return gapi.client.load("https://content.googleapis.com/discovery/v1/apis/drive/v3/rest")
        .then(function() { console.log("GAPI client loaded for API"); },
              function(err) { console.error("Error loading GAPI client for API", err); });
  }
  // Make sure the client is loaded and sign-in is complete before calling this method.
  function execute() {
    return gapi.client.drive.permissions.update({
      "fileId": "xxxxxx",
      "permissionId": "xxxxxx",
      "removeExpiration": false,
      "supportsTeamDrives": true,
      "transferOwnership": true,
      "resource": {
        "type": "anyone",
        "role": "owner"
      }
    })
        .then(function(response) {
                // Handle the results here (response.result has the parsed body).
                console.log("Response", response);
              },
              function(err) { console.error("Execute error", err); });
  }
  gapi.load("client:auth2", function() {
    gapi.auth2.init({client_id: YOUR_CLIENT_ID});
  });
</script>
<button onclick="authenticate().then(loadClient)">authorize and load</button>
<button onclick="execute()">execute</button>

现在可以运行,但是在根据this进行检查时实际上并没有更新信息

1 个答案:

答案 0 :(得分:0)

如果您查看permission update帖子正文的文档。您会注意到只有两个可写字段

enter image description here

您正在尝试更新一些不可写的列。

  "removeExpiration": false,
  "supportsTeamDrives": true,
  "transferOwnership": true,

尝试做:

 /**
 * Update a permission's role.
 *
 * @param {String} fileId ID of the file to update permission for.
 * @param {String} permissionId ID of the permission to update.
 * @param {String} newRole The value "owner", "writer" or "reader".
 */
function updatePermission(fileId, permissionId, newRole) {
  // First retrieve the permission from the API.
  var request = gapi.client.drive.permissions.get({
    'fileId': fileId,
    'permissionId': permissionId
  });
  request.execute(function(resp) {
    resp.role = newRole;
    var updateRequest = gapi.client.drive.permissions.update({
      'fileId': fileId,
      'permissionId': permissionId,
      'resource': resp
    });
    updateRequest.execute(function(resp) { });
  });
}