Reputation: 27
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
Reputation: 201378
I believe your goal and situation as follows.
{role: "reader", type: "anyone"}
using ajax.JSON.stringify({role: "reader", type: "anyone"})
can be directly used like data: JSON.stringify({role: "reader", type: "anyone"})
of the request header.application/json
as the content type of the request header.When above points are reflected to your script, it becomes as follows.
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
});
Upvotes: 3