Are you working on a C# .net application that needs to upload files to Google Drive? Do you want to upload to an account that you personally have control of? You don’t need the users to authenticate themselves with Google. What you need to use is the Google Drive API with a service account.
In this tutorial we are going to go though the steps needed to connect to the Google Drive API with a service account. Using Visual Studio C# and the Google dot Net Client library with the Nuget package for Google Drive. Please not to use the nuget package you must have your project set to either .net 4.0 or 4.5.
Accessing the Google APIs using a service account is quite useful. Some times you just want to access your own data and not data owned by other users. In this instance there is no reason to use OAuth2 and prompt a user to give you access to there information, its your information you already have access. This is why we use a service account. This tutorial uses the Google Calendar API to walk you though the steps of creating a service account and how to then access your Google Calendar with it.
From Visual studio make sure that your project is set to .net 4 or .net 4.5. install the following nuget package.
PM> Install-Package Google.Apis.Drive.v2
Google Developer Console
On the credentials screen of the Google Developer console, you need to create Service Account Credentials. When you do this you will be prompted to download a p12 key file. Save this file it will need to be accessed by your application.
A few important things to note about a service account is that it is not you. Just because you created the service account does not mean that it has access to the files you have stored in your Google drive. You can take the service account email address and give it access to a directory on your Google drive. It will then be allowed to upload to that directory, but you wont have access to the files. You will need to complete a second step and give yourself personally permission to access those files by updating or patching the file permissions.
Another option would be to have the service account grate your Google account access to a directory on its account then you will be able to see the files on your Google drive. Again permissions will probably need to be set to allow you to access the files.
The following method will show you how to connect to a service account. You will need to pass it the full path of the key file that you downloaded from Google Developer console. There are a number of scopes that you could send but because this is a service account requesting full access is probably the best way to go.
The method returns a drive service that you can then use to access the Google Drive API.
Using a service account to access Google Drive API can be very useful but it is important to remember that a service account is not you. If you want to be able to access the files it uploads you must grant yourself access to them though the service account.