Google Drive file permissions 5


Please Share

Google Drive APIHave 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.



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.


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, "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.

Please Share


Linda Lawton

About Linda Lawton

My name is Linda Lawton I have more than 20 years experience working as a developer and a database expert. I have been working with several of the Google APIs, since 2012. I helping others in the On-Line community to develop with the Google APIs, by creating my own blog www.daimto.com. This and my presence on a number of On-Line developer forums lead me to be noticed by the Google Analytics API development team. I was nominated for and recently became one of the first Google Developer Experts for Google Analytics.


Leave a comment

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

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