【问题标题】:How to change file permission with API google drive如何使用 API 谷歌驱动器更改文件权限
【发布时间】:2020-08-29 08:32:51
【问题描述】:

我尝试在正文(data)中发送权限类型字段,但没有成功。

文档说明:在body上发送权限和角色。

文档链接:https://developers.google.com/drive/api/v3/reference/permissions/create

我得到了这个答案。

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "required",
    "message": "The permission type field is required.",
    "locationType": "other",
    "location": "permission.type"
   }
  ],
  "code": 400,
  "message": "The permission type field is required."
 }
}

这是我的代码结构,我将不胜感激。

const file = this.file;

        var FileID = "16omyu1bxFk1tVVMrpIcYQDC3sNYxSIg2";

        const fr = new FileReader();
        fr.readAsDataURL(file);
        fr.onload = function() {
          const boundary = "xxxxxxxxxx";
          let data = "--" + boundary + "\n";
          data += "Content-Type: application/json; charset=UTF-8\n\n";
          data += JSON.stringify({role: "reader", type: "anyone"}) + "\n";
          
        $.ajax({
            type: "POST",
            beforeSend: function(request) {
                request.setRequestHeader("Authorization", "Bearer" + " " + localStorage.getItem("accessToken"));
                request.setRequestHeader("Content-Type", "boundary=" + boundary); 
            },
            url: "https://www.googleapis.com/drive/v3/files/" + FileID + "/permissions",
            success: function (data) {
                console.log(data);
            },
            error: function (error) {
                console.log(error);
            },
            async: true,
            data: data,
            cache: false,
            processData: false,
            timeout: 60000
        });
    }

【问题讨论】:

    标签: javascript google-drive-api


    【解决方案1】:

    我相信你的目标和情况如下。

    • 您想使用 ajax 将权限创建为 {role: "reader", type: "anyone"}
    • 您的访问令牌可用于使用 Drive API 创建权限。

    修改点:

    • 在这种情况下,JSON.stringify({role: "reader", type: "anyone"})可以像请求头的data: JSON.stringify({role: "reader", type: "anyone"})一样直接使用。
    • 请使用application/json作为请求头的内容类型。

    当以上几点反映到你的脚本中时,它变成如下。

    修改后的脚本:

    从:
    const boundary = "xxxxxxxxxx";
    let data = "--" + boundary + "\n";
    data += "Content-Type: application/json; charset=UTF-8\n\n";
    data += JSON.stringify({role: "reader", type: "anyone"}) + "\n";
    $.ajax({
        type: "POST",
        beforeSend: function(request) {
            request.setRequestHeader("Authorization", "Bearer" + " " + localStorage.getItem("accessToken"));
            request.setRequestHeader("Content-Type", "boundary=" + boundary); 
        },
        url: "https://www.googleapis.com/drive/v3/files/" + FileID + "/permissions",
        success: function (data) {
            console.log(data);
        },
        error: function (error) {
            console.log(error);
        },
        async: true,
        data: data,
        cache: false,
        processData: false,
        timeout: 60000
    });
    
    至:
    $.ajax({
      type: "POST",
      beforeSend: function(request) {
        request.setRequestHeader("Authorization", "Bearer" + " " + localStorage.getItem("accessToken"));
        request.setRequestHeader("Content-Type", "application/json"); // Modified
      },
      url: "https://www.googleapis.com/drive/v3/files/" + FileID + "/permissions",
      success: function (data) {
        console.log(data);
      },
      error: function (error) {
        console.log(error);
      },
      async: true,
      data: JSON.stringify({role: "reader", type: "anyone"}), // Modified
      cache: false,
      processData: false,
      timeout: 60000
    });
    

    参考:

    【讨论】:

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