Vilmcode
Vilmcode

Reputation: 27

How to change file permission with API google drive

I tried to send the permission type field in the body (data ), but I have not been successful.

The documentation indicates: send on the body the permission and role.

Documentation Links: https://developers.google.com/drive/api/v3/reference/permissions/create

I got this answer.

{
 "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."
 }
}

This is the structure of my code, I will be grateful.

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
        });
    }

Upvotes: 1

Views: 996

Answers (1)

Tanaike
Tanaike

Reputation: 201378

I believe your goal and situation as follows.

  • You want to create the permission as {role: "reader", type: "anyone"} using ajax.
  • Your access token can be used for creating the permissions using Drive API.

Modification points:

  • In this case, JSON.stringify({role: "reader", type: "anyone"}) can be directly used like data: JSON.stringify({role: "reader", type: "anyone"}) of the request header.
  • Please use application/json as the content type of the request header.

When above points are reflected to your script, it becomes as follows.

Modified script:

From:
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
});
To:
$.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
});

Reference:

Upvotes: 3

Related Questions