Im trying to upload file to the google drive, and I want to set 'shared' permission for this file, but I don't know how to do it... I tried to use this code, but the file is uploaded without the shared permission.
My code is:
// _drive - google drive object
Google.Apis.Drive.v2.Data.File item = new Google.Apis.Drive.v2.Data.File();
Permission permission = new Permission();
permission.Role = "reader";
permission.Type = "anyone";
permission.WithLink = true;
item.Permissions = new List<Permission>() { permission };
FilesResource.InsertMediaUpload request = _drive.Files.Insert(item, fileStream, mimeType);
request.Upload();
OK I have spent the last hour playing around with this. If you check out Files.insert documentation it doesn't really state anyplace that you should be able to set the permissions at insert time.
At the bottom if you test out try it. Setting the permissions up as you have done above under Request body.

It does upload the file. But the Json returned gives us a clue.
"shared": false,
Now if i check the file in Google drive

This leads me to believe that this is not supported by the Google Drive API. It is not possible to set the permissions at the time of upload. You are going to have to create a separate call to set the permissions after you have uploaded the file.
While it looks like the body does support the permissions it doesn't appear to be working. I am not sure if this is a bug or something that is just not supported. I am going to see if i can find the location of the issue tracker for Drive and add it as an issue.
In the mean time you are going to have to make the two calls and eat a bit of your Quota.
Issue 3717: Google drive api, upload file with shared permission
Also experienced this bug. Specifying permissions in the same file / directory upload, had to do it in a separate request, like below. The Google Drive API documentation is not clear about this (and not clear about how to handle file permissions when using a Service Account).
var NewDirRequest = DService.Files.Insert(GoogleDir);
var NewDir = NewDirRequest.Execute();
GoogleFolderID = NewDir.Id;
var NewPermissionsRequest = DService.Permissions.Insert(new Permission()
{
Kind = "drive#permission",
Value = emailAddress,
Role = "writer",
Type = "user"
}, GoogleFolderID);
DService.Permissions.Insert(new Permission()
{
Kind = "drive#permission",
Value = "mydomain.com",
Role = "reader",
Type = "domain"
}, GoogleFolderID);
NewPermissionsRequest.Execute();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With