Geek Culture
Published in

Geek Culture

Upload files to Google Drive with C#

In this guide, we will explain step by step how to make a console application that uses the Google Drive API to upload, delete and list files using the C # programming language. You can easily extend these examples if you want to use them in a web application.

1. Generate an Access Token.

First, we need to create some new credentials. It’s mandatory to have an account and a project on Google Cloud Platform. Navigate to https://console.cloud.google.com/apis/credentials, select your project and click Create Credentials. Make sure it is of type OAuth client ID.

https://console.cloud.google.com/apis/credentials

Next, we select Web application and include https://developers.google.com/oauthplayground as an authorized address. Then press the Create button.

Once created, save the Client ID and Client Secret. It’s important that we save these values, to be used later.

We already have everything we need to generate an Access Token. I recommend that you use OAuth 2.0 Playground.

👉 https://developers.google.com/oauthplayground

If you use this tool, look for the settings icon in the upper right corner and select the following options:

Then select Step 1 and click Authorize APIs. From here, we authenticate with our Google account and confirm access with the permissions that we have previously requested.

Then in Step 2 we click on the Exchange authorization code for tokens button. And that’s it, we will get the Access Token that we will need to connect with Google Drive from our application.

2. Prepare the project.

Create a new C# Console Application project and install Google.Apis.Drive.v3 library.

3. Initialize the DriveService.

It is time to use the Access Token, Client ID and Client Secret that we have obtained in the previous steps.

4. Create folder.

With DriveService we can easily interact with the Google Drive API. Let’s see an example of how to create folders.

5. Upload a file.

6. Delete a file.

7. List files in a folder.

In this example, we will list the files contained in a folder without including the folders. We can see the query on line 6. Also, we can request extra information in the response. An example on line 7.

What’s next

For more information and examples, you can read the official documentation here:

👉 https://developers.google.com/drive/api/v3/quickstart/dotnet.

I hope this step-by-step guide on how to interact with the Google Drive API has been helpful. Thank you very much for reading the article ❤️.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store