Google Drive file permissions 13


Google Drive API V3
Google Drive API V3

Have you been trying to connect your website or application to Google Drive?  Would you like to see a list of files or a list of Directories stored on Google Drive?   Would you like to Upload a file to Google Drive or download a file from Google drive using C# .Net using Visual Studio?   In this tutorial series, we will be looking into the Google Drive API, using the Google .net Client library which makes accessing Google APIs much simpler.    There are a lot of things you can do with the Google Drive API, List files and folders,  Download files, Upload files, Change Files,  Copy files,  Delete Files, Trash Files,  Create directory’s, delete directory’s.  There is even a version of %appdata% on Google Drive where you can store your application specific files for each user.
[wp_ad_camp_3]
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

  1. Google Drive API with C# .net – Authentication
  2. Google Drive API with C# .net – Files –  Files, directories, search
  3. Google Drive API with C# .net – Upload, update, patch, delete, trash
  4. Google Drive API with C# .net – Download
  5. Google Drive API with C# .net – File Permissions
  6. 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.

Project Setup

Make sure your project is at least set to .net 4.0.

Add the following NuGet Package

PM> Install-Package Google.Apis.Drive.v2

Authentication

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.

f 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, "me@gmail.com", "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.

Conclusion

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.


About Linda Lawton

My name is Linda Lawton I have more than 20 years experience working as an application developer and a database expert. I have also been working with Google APIs since 2012 and I have been contributing to the Google .Net client library since 2013. In 2013 I became a a Google Developer Experts for Google Analytics.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

13 thoughts on “Google Drive file permissions

  • Akshita Gupta

    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.

    • Linda Lawton Post author

      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.

  • Ankit

    Hii
    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.

  • AntonK

    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.

  • Sairam

    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.

  • Alexandre Silva Ferreira

    Dear, lady!
    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.
    Best regards.