尝试通过 Google Drive API 创建权限时出现“需要权限类型字段”错误

nrp*_*p31 2 node.js google-drive-api

我从此处找到的 Node.js 的 Google Drive API Quickstart 中改编了代码,以尝试为 Google Drive 中的现有文件创建新权限。

无论我在代码中做了什么更改,我总是得到相同的响应,The permission type field is required即使我已经通过resourcenpmgoogleapis 客户端库的文档和我找到的其他示例中提到的方式指定了它。

这只是不起作用还是我错过了一些明显的东西?

更新权限的代码

function updateFilePermissions(auth) {

  var drive = google.drive({
    version: 'v3',
    auth: auth
  });

  var resourceContents = {
    role: 'writer',
    type: 'user',
    emailAddress: 'user@example.com'
  };

  drive.permissions.create({

    resource: resourceContents,
    fileId: aValidFileId,
    sendNotificationEmail: false,
    fields: 'id',

  }, function(err, res) {

    if (err) {

      // Handle error...
      console.error(err);

    } else {

      console.log('Permission ID: ', res.id);

    }

  });

}
Run Code Online (Sandbox Code Playgroud)

来自 Google Drive API 的响应

code: 400,
errors: 
 [ { domain: 'global',
     reason: 'required',
     message: 'The permission type field is required.',
     locationType: 'other',
     location: 'permission.type' } ]
Run Code Online (Sandbox Code Playgroud)

Adr*_*uel 7

对于仍在查看答案的任何人,它需要像这样格式化:

  {
          fileId: fieldID, // String
          resource: {
            role: putRoleHere, //String
            type: putTypeHere //String
          }
Run Code Online (Sandbox Code Playgroud)

Google 的 API 将 Axios 用于 HTTP 客户端,因此在使用他们的方法时会为您自动字符串化 :)