Reputation: 50
I'm using an example from Google API Docs v3 (This is a live example to see the error). When trying to update a file permission, the following error appears :
The resource body includes fields which are not directly writable.
The file permission needs to be updated to allow access to the URL for anyone, so we can use the 'uploaded' file as a sharing tool. If you remove the 'type' option the script runs. Read other questions regarding this, including this, but not helped solve this issue. Overall, the goal is to make a file the user selects from the google drive picker shareable. Any pointers would be great.
Code below (Same as URL above).
<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>
To summarise, I have attempted to update the user role/type with the below, but even though there are no errors, the current fields do not update.
<script src="https://apis.google.com/js/api.js"></script>
<script>
/**
* Sample JavaScript code for drive.permissions.list
* 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 https://www.googleapis.com/auth/drive.metadata https://www.googleapis.com/auth/drive.metadata.readonly https://www.googleapis.com/auth/drive.photos.readonly https://www.googleapis.com/auth/drive.readonly"})
.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 updatePermission(fileId, permissionId, newRole) {
var newRole = "owner";
// First retrieve the permission from the API.
var request = gapi.client.drive.permissions.get({
"fileId": "xxxxx",
'permissionId': "xxxxx",
});
request.execute(function(resp) {
resp.role = newRole;
var updateRequest = gapi.client.drive.permissions.update({
'fileId': "xxxxx",
'permissionId': "xxxxx",
'resource': resp
});
updateRequest.execute(function(resp) {
});
});
}
gapi.load("client:auth2", function() {
gapi.auth2.init({client_id: "xxx"});
});
</script>
<button onclick="authenticate().then(loadClient)">authorize and load</button>
<button onclick="updatePermission()">execute</button>
This now runs, but isn't actually updating the information when you check it against this
Upvotes: 2
Views: 2991
Reputation: 117146
If you check the documentation for permission update post body. You will notice that there are only two fields that are writable
You are trying to update some columns which are not writable.
"removeExpiration": false,
"supportsTeamDrives": true,
"transferOwnership": true,
Try doing:
/**
* 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) { });
});
}
Upvotes: 1