2

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

TylerH
  • 20,799
  • 66
  • 75
  • 101
Jonny
  • 84
  • 1
  • 11

1 Answers1

1

If you check the documentation for permission update post body. You will notice that there are only two fields that are writable

enter image description here

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) { });
  });
}
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • Thank you for this. When you say not writable, the documentation for the given link actually added that code when you select them. In regards to adding the new type for the user, how would you get around that? – Jonny Feb 12 '19 at 11:55
  • Those fields are only writable for inserts not for updates. – Linda Lawton - DaImTo Feb 12 '19 at 12:36
  • Makes sense. So how would we go about adding the type? This is the key we need in order to change the users permission of a file they select? – Jonny Feb 12 '19 at 13:18
  • Again if you want to update type you will have to delete the row thats there with the type and insert it again. Type is not writable fo ran update only role – Linda Lawton - DaImTo Feb 12 '19 at 13:34
  • Hi Dalm, I've added some code to the OP, based from your answer. Getting an error for 401 when trying to 'execute' after the auth process. – Jonny Feb 12 '19 at 14:09
  • Dont remove your original code, but update your answer and add your new code as well so we can see what you are doing. – Linda Lawton - DaImTo Feb 12 '19 at 14:10
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/188286/discussion-between-jonny-and-daimto). – Jonny Feb 12 '19 at 14:16
  • change "fileId": "xxxxx", to 'fileId': 'xxxxx', – Linda Lawton - DaImTo Feb 12 '19 at 14:16