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.
Five part 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
Types of Authentication
Authentication is the key to everything when you want to access Google Drive API. If you want to be able to access data, you will need to be authenticated. There are two types of Authentication OAuth2 which will allow you to access another users data, and a service account which can be set up to access your own data. The code for them is slightly different.
If you want to be able to access Google Drive though the API owned by someone else you will need to be authenticated. The code for authentication simply displays a webpage for your user asking them to give you permission to access there data. If the user gives you permission a file is then stored on the machine in the %AppData% directory containing all the information you will need in the future to access there data. For my testing purpose I use Environment. Environment.UserName as the name of the user, but if you are doing this web based you could use it as a session var of some kind.
That’s just fine but how does Google know what kind of permission you want to ask the user for? That’s where scopes come in, with scopes you define what permissions you need.
The code below will basically ask for everything, you should only include the scopes you NEED.
Assuming the user clicks Accept and gives you permission you will then have a valid user credential you can build upon. The main problem you may have with this is that it is using FileDataStore which stores the data in the users %AppData% directory while this is probably fine for an installed application this is probably not the optimal solution for a web application. If you want to store the data in the database you need to create your own implementation of IDataStore. You are in luck I have a working version of that on GitHub.
If you are only accessing your own Google Drive Account you own this data already, so there is no reason to ask a user for permission to access it. In this instance you should use a Service account. A service account is just like any other user who has been granted access to your Google drive data with one exception, its automatic. There will be no prompt for access. You can also grant the service account direct access to a folder you already have on your google drive account it can then upload to that folder, but again it will have to give you permissions to access the files as well. Just add the service account email to the folder like you would any other user you want to give access.
A service account also has its own drive account, so you could use a service account and upload data to it. The problem with uploading data to a service account directly is that you cant see it on the website. In this instance you can create a directory on the service account then grant your personal google account full access to the directory. Then you will be able to see the files on your web site version of google drive. This can be very tricky when the service account uploads the files it owns the files it will have to give you permission to access them as well.
In order to send requests to Google Drive you need a “BaseClientService” to preform those request though, notice how we are passing it our credentials.
If the file is set to public then you don’t need to be authenticated. You can access it using a public API key.
Now we have a drive service that we can preform perform though.
You should now understand how to access the Google Drive API with an authenticated user. Join me for the next tutorial where we will look at how to list Files, directories, search using an authenticated Google drive service.
If you had any problems with this tutorial you can check the a sample project for working with Google Drive API on GitHub as part of the Google-Dotnet-Samples project. With in that project you will find a helper class for Authentication with Google Drive.