In this article I will explain how to upload files to Google Drive using Google Drive API in ASP.Net with C# and VB.Net and the ASPSnippets.GoogleAPI.
 
 
Getting Google Client ID and Client Secret
 
In order to use Google Drive API for uploading files to Google Drive, you will need to create an Application in Google Console and get Client ID and Client Secret. For details please refer the following article.
 
 
Downloading ASPSnippets.GoogleAPI DLL
 
You can download the ASPSnippets.GoogleAPI API DLL using the following download link.
 
 
HTML Markup
 
The HTML Markup consist of an ASP.Net FileUpload control, a TextBox and a Button for uploading the File along with its Description to the Google Drive.
There’s also an HTML Table containing some Label controls for displaying the details of the uploaded file.
 
File:
<asp:FileUpload ID="FileUpload1" runat="server" />
<br />
<br />
Description:
<asp:TextBox ID="txtDescription" runat="server" Width="300"></asp:TextBox>
<hr />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="UploadFile" />
<hr />
<table id="tblFileDetails" runat="server" visible="false" border="0" cellpadding="0"
cellspacing="0">
<tr>
    <td>
        Title
    </td>
    <td>
        <asp:Label ID="lblTitle" runat="server" />
    </td>
</tr>
<tr>
    <td>
        Id
    </td>
    <td>
        <asp:Label ID="lblId" runat="server" />
    </td>
</tr>
<tr>
    <td>
        Icon
    </td>
    <td>
        <asp:Image ID="imgIcon" runat="server" />
    </td>
</tr>
<tr id="rowThumbnail" runat="server" visible="false">
    <td valign="top">
        Thumbnail
    </td>
    <td>
        <asp:Image ID="imgThumbnail" runat="server" Height="60" Width="60" />
    </td>
</tr>
<tr>
    <td>
        Created Date
    </td>
    <td>
        <asp:Label ID="lblCreatedDate" runat="server" />
    </td>
</tr>
<tr>
    <td>
        Download
    </td>
    <td>
        <asp:HyperLink ID="lnkDownload" Text="Download" runat="server" />
    </td>
</tr>
</table>
 
 
Namespaces
 
You will need to import the following namespaces.
 
Note: You will need place the ASPSnippets.GoogleAPI DLL inside the BIN folder of your project and add its reference.
 
C#
using ASPSnippets.GoogleAPI;
using System.Web.Script.Serialization;
 
VB.Net
Imports ASPSnippets.GoogleAPI
Imports System.Web.Script.Serialization
 
 
Data Class
 
You will need to create the following class which will be used to hold the Google Drive file details returned from Google API after the file is uploaded.
The structure of this class is same as that of the JSON string returned from the Google API so that the JSON string can be easily deserialized to its object.
 
C#
public class GoogleDriveFile
{
    public string Id { get; set; }
    public string Title { get; set; }
    public string OriginalFilename { get; set; }
    public string ThumbnailLink { get; set; }
    public string IconLink { get; set; }
    public string WebContentLink { get; set; }
    public DateTime CreatedDate { get; set; }
    public DateTime ModifiedDate { get; set; }
}
 
VB.Net
Public Class GoogleDriveFile
    Public Property Id() As String
        Get
            Return m_Id
        End Get
        Set(value As String)
            m_Id = Value
        End Set
    End Property
    Private m_Id As String
    Public Property Title() As String
        Get
            Return m_Title
        End Get
        Set(value As String)
            m_Title = Value
        End Set
    End Property
    Private m_Title As String
    Public Property OriginalFilename() As String
        Get
            Return m_OriginalFilename
        End Get
        Set(value As String)
            m_OriginalFilename = Value
        End Set
    End Property
    Private m_OriginalFilename As String
    Public Property ThumbnailLink() As String
        Get
            Return m_ThumbnailLink
        End Get
        Set(value As String)
            m_ThumbnailLink = Value
        End Set
    End Property
    Private m_ThumbnailLink As String
    Public Property IconLink() As String
        Get
            Return m_IconLink
        End Get
        Set(value As String)
            m_IconLink = Value
        End Set
    End Property
    Private m_IconLink As String
    Public Property WebContentLink() As String
        Get
            Return m_WebContentLink
        End Get
        Set(value As String)
            m_WebContentLink = Value
        End Set
    End Property
    Private m_WebContentLink As String
    Public Property CreatedDate() As DateTime
        Get
            Return m_CreatedDate
        End Get
        Set(value As DateTime)
            m_CreatedDate = Value
        End Set
    End Property
    Private m_CreatedDate As DateTime
    Public Property ModifiedDate() As DateTime
        Get
            Return m_ModifiedDate
        End Get
        Set(value As DateTime)
            m_ModifiedDate = Value
        End Set
    End Property
    Private m_ModifiedDate As DateTime
End Class
 
 
Authenticate user using Google account
 
On the click of the Upload button, User is redirected to the Google Authorization page where user has to provide permission to the Application to access his Google Drive.
 
For this article I am requesting access to the Google Drive of the user by passing the https://www.googleapis.com/auth/drive.file scope. User can allow and deny and in both cases he is sent back to the URL set as the RedirectUri while creating the application in Google Developer Console.
 
The uploaded File and the Description is saved in Session variable for uploading the file to Google Drive after the permission is granted.
 
C#
protected void UploadFile(object sender, EventArgs e)
{
    Session["File"] = FileUpload1.PostedFile;
    Session["Description"] = txtDescription.Text;
    GoogleConnect.Authorize("https://www.googleapis.com/auth/drive.file");
}
 
VB.Net
Protected Sub UploadFile(sender As Object, e As EventArgs)
    Session("File") = FileUpload1.PostedFile
    Session("Description") = txtDescription.Text
    GoogleConnect.Authorize("https://www.googleapis.com/auth/drive.file")
End Sub
 
Upload Files to Google Drive using Google Drive API in ASP.Net with C# and VB.Net
 
 
Uploading the File to Google Drive and displaying the details on page
The very first thing is that you need to set the Client ID and the Client Secret for the GoogleConnect class and you need to set the API as Drive as we need to use the Google Drive API.
The below code looks for access code (access token) in Query string and then this access code is passed to the PostFile function of the GoogleConnect class along with the File and its Description from the Session variable.
The PostFile function returns the uploaded Google Drive file details as JSON string which is then deserialized to the GoogleDriveFile class object.
Finally the details of the uploaded Google Drive file are displayed.
C#
protected void Page_Load(object sender, EventArgs e)
{
    GoogleConnect.ClientId = "<Google Client ID>";
    GoogleConnect.ClientSecret = "<Google Client Secret>";
    GoogleConnect.RedirectUri = Request.Url.AbsoluteUri.Split('?')[0];
    GoogleConnect.API = EnumAPI.Drive;
    if (!string.IsNullOrEmpty(Request.QueryString["code"]))
    {
        string code = Request.QueryString["code"];
        string json = GoogleConnect.PostFile(code, (HttpPostedFile)Session["File"], Session["Description"].ToString());
        GoogleDriveFile file = (new JavaScriptSerializer()).Deserialize<GoogleDriveFile>(json);
        tblFileDetails.Visible = true;
        lblTitle.Text = file.Title;
        lblId.Text = file.Id;
        imgIcon.ImageUrl = file.IconLink;
        lblCreatedDate.Text = file.CreatedDate.ToString();
        lnkDownload.NavigateUrl = file.WebContentLink;
        if (!string.IsNullOrEmpty(file.ThumbnailLink))
        {
            rowThumbnail.Visible = true;
            imgThumbnail.ImageUrl = file.ThumbnailLink;
        }
    }
    if (Request.QueryString["error"] == "access_denied")
    {
        ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('Access denied.')", true);
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        GoogleConnect.ClientId = "<Google Client ID>";
        GoogleConnect.ClientSecret = "<Google Client Secret>";
    GoogleConnect.RedirectUri = Request.Url.AbsoluteUri.Split("?"c)(0)
    GoogleConnect.API = EnumAPI.Drive
    If Not String.IsNullOrEmpty(Request.QueryString("code")) Then
        Dim code As String = Request.QueryString("code")
        Dim json As String = GoogleConnect.PostFile(code, DirectCast(Session("File"), HttpPostedFile), Session("Description").ToString())
        Dim file As GoogleDriveFile = (New JavaScriptSerializer()).Deserialize(Of GoogleDriveFile)(json)
        tblFileDetails.Visible = True
        lblTitle.Text = file.Title
        lblId.Text = file.Id
        imgIcon.ImageUrl = file.IconLink
        lblCreatedDate.Text = file.CreatedDate.ToString()
        lnkDownload.NavigateUrl = file.WebContentLink
        If Not String.IsNullOrEmpty(file.ThumbnailLink) Then
            rowThumbnail.Visible = True
            imgThumbnail.ImageUrl = file.ThumbnailLink
        End If
    End If
    If Request.QueryString("error") = "access_denied" Then
        ClientScript.RegisterClientScriptBlock(Me.[GetType](), "alert", "alert('Access denied.')", True)
    End If
End Sub
 
 
Screenshots
 
The details of the uploaded file displayed on page
Upload Files to Google Drive using Google Drive API in ASP.Net with C# and VB.Net
 
The uploaded file with description in Google Drive
Upload Files to Google Drive using Google Drive API in ASP.Net with C# and VB.Net
 
Demo
 
 
 
Downloads