1
votes

according to the Google Drive SDK Update sharing permissions and https://developers.google.com/drive/v2/reference/permissions I want to change the whole folder content to be available for viewing (the folder contains only images and I show them on my page with <img alt='' src='https://drive.google.com/uc?export=view&id="fileID"'/>)

so, I'm trying to use that code

PermissionList permissions = Google_DriveService.Permissions.List(fileId).Fetch();
var filePermissions =  permissions.Items;


Permission permission = Google_DriveService.Permissions.Get(fileId, filePermissions[0].Id).Fetch();
permission.Role = "reader";
permission.Value = "me";
permission.Type =  "anyone";
permission.WithLink = true;

var updated = Google_DriveService.Permissions.Update(permission, fileId, filePermissions[0].Id).Fetch();

but I get the error message

Google.Apis.Requests.RequestError Invalid permission type specified [400] Errors [ Message[Invalid permission type specified] Location[ - ] Reason[invalid] Domain[global] ] 

what am I doing wrong ?

3

3 Answers

3
votes

Tony's answer is correct and here is an addon to his instruction if anyone else is looking to test this. Note that I did not write up the code for this due to time constraints but I tested it using the Google API supplied on the Google Drive web api page. I tested this by doing the following:

  1. Go to the following link and list all the files in your drive. https://developers.google.com/drive/v2/reference/files/list

  2. Select one file that you want to share for read only access and copy out the "id" as per the screenshot below:

drive id

Go to the insert permission page here - Tony was correct, you do not delete the existing permission but instead, you add onto that permission: https://developers.google.com/drive/v2/reference/permissions/insert

  1. In the API explorer, type in "role" as "reader" and "type" as "anyone".

  2. Go to your Drive: https://drive.google.com/drive/#my-drive and you will see that the file is now shared - if you hover over the green link icon in the screenshot below, it will tell you that it is now shared.

Drive shared

You can also copy the link for the doc and paste it into Incognito mode in Chrome to test whether you can access the read-only version of the doc in Incognito mode and it should work.

Additional:

The code should not be too hard, here it is:

 Permission newPermission = new Permission();

    newPermission.setType("anyone");
    newPermission.setRole("reader");
    try {
      return service.permissions().insert(fileId, newPermission).execute();
    } catch (IOException e) {
      System.out.println("An error occurred: " + e);
    }
2
votes

You can't change permission types with update requests, insert a new permission and delete the existing. You should also not use me as a principal related to an anyone permission. Anyone permissions dont expect value attributes to be set.