Click here to Skip to main content
15,746,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I view the files uploaded into the Google Drive service Account from a ASP.NET web application ? I have used Google Drive API v3 and I have provided here the code which I have used to upload the files. Kindly help me regarding this. Thank You !

What I have tried:

using System;
using Google.Apis.Drive.v3;
using Google.Apis.Auth.OAuth2;
using System.Threading;
using Google.Apis.Util.Store;
using Google.Apis.Services;
using Google.Apis.Drive.v3.Data;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using System.Web.UI;

namespace Sample
public partial class testing : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)

private static string GetMimeType(string fileName)
string mimeType = "application/unknown";
string ext = System.IO.Path.GetExtension(fileName).ToLower();
Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
if (regKey != null && regKey.GetValue("Content Type") != null)
    mimeType = regKey.GetValue("Content Type").ToString();
return mimeType;
protected void Button2_Click(object sender, EventArgs e)
public void connect()
string[] scopes = new string[] { DriveService.Scope.Drive }; // Full access

var keyFilePath = @"D:\kk\****46bb0790d147.p12";    // Downloaded from
var serviceAccountEmail = "k******";  // found

//loading the Key file
var certificate = new X509Certificate2(keyFilePath, "notasecret", X509KeyStorageFlags.Exportable);
var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail)
    Scopes = scopes

var service = new DriveService(new BaseClientService.Initializer()
    HttpClientInitializer = credential,
    ApplicationName = "Drive API Sample",
if (System.IO.File.Exists(@"C:\Project\Uploads\Eidea.txt"))
    File body = new File();
    body.Name = System.IO.Path.GetFileName(@"C:\Project\Uploads\Eidea.txt");
    body.Description = "File uploaded";
    body.MimeType = GetMimeType(@"C:\Project\Uploads\Eidea.txt");

    // File's content.
    byte[] byteArray = System.IO.File.ReadAllBytes(@"C:\Project\Uploads\Eidea.txt");
    System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
        FilesResource.CreateMediaUpload request = service.Files.Create(body, stream, GetMimeType(@"C:\Project\Uploads\Eidea.txt"));
     //  Console.WriteLine(request.ResponseBody);
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('" + request.ResponseBody + "')", true);
    catch (Exception e)
        Console.WriteLine("An error occurred: " + e.Message);

Updated 9-Mar-17 20:17pm

1 solution

Quickly browsing through your code sample, it does not look correct. Uploading files to Google Drive is file upload a bit more involved. Here is a link to the documentation if you want to implement your own: Uploading Files  |  Drive REST API  |  Google Developers[^].

You might be better off using this instead: API Client Library for .NET  |  Google Developers[^]
Share this answer
jkkr 10-Mar-17 3:07am    
Can you please correct the given code or at least mention the places of mistakes ? Thank You !
Graeme_Grant 10-Mar-17 3:11am    
No, the code requires major rewriting. Please take the time to read and follow the Google Drive API documentation - it is very clear what is required. If you don't have the time to do that, then use their client library that Google supports - it works.
jkkr 11-Mar-17 2:28am    
Thanks Graeme_Grant !!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900