Upload files to Google Drive with C#

Alejandro Dominguez
Geek Culture
Published in
4 min readApr 2, 2021

--

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.

$ Install-Package Google.Apis.Drive.v3
or
$ dotnet add package Google.Apis.Drive.v3

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

Protect Your Online Privacy with NordVPN

Disclosure: This section contains promotional content.

NordVPN is a Virtual Private Network (VPN) service that provides users with a secure and private way to access the internet.

In today’s digital age, online security and privacy are paramount. With cyber threats and data breaches on the rise, safeguarding your sensitive information has never been more crucial. That’s where NordVPN comes in — a powerful tool that can provide you with a secure and private online browsing experience.

👉 NordVPN: Get up to 65% discount with our NordVPN promotion code

👉 NordLocker: Get up to 53% discount with our NordLocker promotion code

👉 NordPass: Get up to 50% discount with our NordPass promotion code

Note: Discounts may vary over time. Please visit the respective official NordVPN websites for the most current offers.

Beyond just a VPN service, NordVPN empowers you to take control of your online presence. In an era where online privacy is increasingly under threat, we believe in the importance of staying safe online. By using NordVPN, you’re not only protecting yourself; you’re also contributing to a safer online ecosystem for everyone. If you need more info about the service you can get help here.

--

--