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