I think you get the idea there are a lot of possibilities for us with Google drive. My plans are to show you how to most of these work.
Fivepart Tutorial Series
- Google Drive API with C# .net – Authentication
- Google Drive API with C# .net – Files – Files, directories, search
- Google Drive API with C# .net – Upload, update, patch, delete, trash
- Google Drive API with C# .net – Download
- Google Drive API with C# .net – File Permissions
- Google Drive API with C# .net – Sample project
Restringing your application.
Like with most of the Google APIs you need to be authenticated in order to connect to them. To do that you must first register your application on Google Developer console. Under APIs be sure to enable the Google Drive API and Google Drive SDK, as always don’t forget to add a product name and email address on the consent screen form.
Make sure your project is at least set to .net 4.0.
Add the following NuGet Package
PM> Install-Package Google.Apis.Drive.v2
This tutorial assumes that you have already read the tutorial about Google Drive API with C# .net – Authentication. I will not be going into how to create a valid Drive service if you haven’t created one already please go read that tutorial then come back here.
Google Drive File Permissions
File permissions with in Google drive are quite simply permission for a file, to be specific they are a users permissions to a file.
Tip: Remember in Google Drive a directory or a folder is also a file, so file permissions also relates to permissions to a folder on Google drive
When you upload a file to Google drive the owner of the file is the account that did the uploading. This can cause some problems if you have a service account uploading to a folder on your personal Google Drive, you wont have permissions to it. To solve this problem we insert for our self to the permission on the file after we have uploaded it.
If you remember from the file upload tutorial, after your file is inserted to Google Drive it returns a file resource with in the file resource you can find the file id for the new file that was uploaded. you can use that to call the above method
InsertPermission(service, fileId, "firstname.lastname@example.org", "user", "owner");
You should now have owner access to the file.
I am just thinking out loud here but if the service account in question has access to the folder, it would be smart to have the service account to check the folder and see who has access to the folder and then add permissions to the new file to match the file that was just uploaded. That is probably up to the developer working on the project.
When a file is first inserted / uploaded to Google drive the owner of the file is the account that did the uploading. With C# we can programmatic grant people permissions to files or folders on Google drive using the Google Drive API. On Google drive permissions relates back to who has access to a file or folder.
It is also possible to update or patch permissions on a file in Google Drive let me know if you would like me to extend this tutorial to include update and patch.
Thanks for the guide.
newPermission.Value = value;
newPermission.Value = who;
I guess, it should be:
newPermission.EmailAddress = who;
I am facing a problem with the case you mentioned: “This can cause some problems if you have a service account uploading to a folder on your personal Google Drive, you wont have permissions to it. To solve this problem we insert for our self to the permission on the file after we have uploaded it.”
I have the service account where users file gets uploaded. Now I want incoming users to have their file open in google drive console.
1) Do I need each user’s gmail account to grant them access to the file uploaded in service account? In my case, this is not possible.
2) Also, my application that is integrating with google drive api has some file restrictions (download/view only etc) to each user that I want google drive to implement as well. How can I implement that in permissions object?
Please help. i am working on this stuff for long but couldn’t solve these issues.
1. yes you do
2. I think you should post your question on Stackoverflow.com that forum is better for larger questions like this and you will get more then just my opinions.
What permission i need to give to a file stored on service account that any user ca view and edit that file.
Your probably going to have to have the service account grant the user write permissions on the file.
My self Ankit varma from India , I want to develop a tool to access google drive file upload/download from my tool . In This process google drive ask permission . Can i save this permission to my local db.
You should be saving the users refresh token to your database then you will be able to access their data at a later date.
Please check the post on Stackoverflow Google Drive file insert permssion
One more fix:
return service.Permissions.Create(newPermission, fileId).Execute()
And I think there should be a check somewhere to prevent permissions duplication – in this case Update() should be invoked instead.
Hi, I can able to retrieve folder/file permission in My Drive using ‘PermissionsResource.ListRequest’. But unable to retrieve list of Team Drive permissions/members and Team Drive folders/files permissions using the below code.
PermissionsResource.ListRequest permissionReq = driveService.Permissions.List(drive.Id); // team drive Id or team file Id
permissionReq.SupportsTeamDrives = true;
permissionReq.UseDomainAdminAccess = true;
permissionReq.Fields = “id”;
var permissionResult = permissionReq.Execute();
Thanks in advance.
I would like you show me more abouth to update or patch permissions on a file in Google Drive, can you extend this tutorial to include update and patch.
its it not working?