Access Google Data Using OAuth

Introduction

Here I would like to explain how to get a list of Google Drive Documents and prompt users for access to get the documents list (using OAuth2.0 for Login).

Description

Google APIs use the OAuth 2.0 protocol for authentication and authorization. OAuth 2.0 is a relatively simple protocol. To begin, you register your application with Google.

Register your application

All applications that access the Google API must be registered through the https://cloud.google.com/console#/project.

  • Create a new Project.
  • Click on the Project earlier created.
  • Click on "Registered Apps" -> "Register App".
  • Type a name, choose Web application as the platform.

    Accss-googledata-using-outh-1.jpg

After you've registered, copy the "Client ID" and "Client secret" values, that you'll need later.

Google server provides Google Data API for reading and writing data , and it is easy to access data from this library. You can download this library from the following URL.

https://code.google.com/p/google-gdata/downloads/list.

Now copy the following HTML lines in the GoogelOauthCallback.aspx page.

<form id="form1" runat="server">

<div>

<asp:ListBox ID="lstDocuments" runat="server" Height="131px" Width="160px"></asp:ListBox>

<br />

<br />

<asp:Button ID="btnGetDocuments" runat="server" Text="GetDocuments" OnClick="btnGetDocuments_Click" />

</div>

</form>


And write the following code in the GoogleOauthCallback.cs file:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using Google.GData.Client;

using Google.GData.Documents;

 

namespace AccessGoogleDriveData

{

    public partial class GoogleOAuthCallback : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            string state = Request.QueryString["state"];

            // creating oauthparameter with client id and secret key

            OAuth2Parameters parameters = new OAuth2Parameters()

            {

                ClientId = " Please enter ur registerd client id ",

                ClientSecret = " please enter secret key ",

                RedirectUri = "http://localhost:16615/GoogleOAuthCallback.aspx",

                Scope = "https://docs.google.com/feeds/ ",

                State = "documents",

                AccessType = "offline"

            };

            lstDocuments.Visible = false;

            if (state != null)

            {

                parameters.AccessCode = Request.QueryString["code"];

                // it gets accesstoken from google

                Google.GData.Client.OAuthUtil.GetAccessToken(parameters);

                GOAuth2RequestFactory requestFactory = new GOAuth2RequestFactory(null, "MyDocumentsListIntegration-v1", parameters);

                DocumentsService service = new DocumentsService("MyDocumentsListIntegration-v1");

                service.RequestFactory = requestFactory;

                DocumentsListQuery query = new DocumentsListQuery();

 

                // Make a request to the API and get all documents.

                DocumentsFeed feed = service.Query(query);

                lstDocuments.Visible = true;

 

                if (feed.Entries.Count > 0)

                {

                    var documentsList = from entry in feed.Entries select entry.Title.Text;

                    lstDocuments.DataSource = documentsList;

                    lstDocuments.DataBind();

                }

            }

        }

 

        protected void btnGetDocuments_Click(object sender, EventArgs e)

        {

            OAuth2Parameters parameters = new OAuth2Parameters()

            {

                ClientId = " Please enter ur registerd client id ",

                ClientSecret = " please enter secret key ",

                RedirectUri = "http://localhost:16615/GoogleOAuthCallback.aspx",

                Scope = "https://docs.google.com/feeds/ ",

                State = "documents",

                AccessType = "offline" // offline means it creats a refreshtoken

            };

            string url = Google.GData.Client.OAuthUtil.CreateOAuth2AuthorizationUrl(parameters);

            Session["oauthDocumentsParameters"] = parameters;

            // it redirct to google login page

            Response.Redirect(url);

        }

    }

}

 

Now run the application; it will show as in the following screen:

Accss-googledata-using-outh-2.jpg

Click on the GetDocuments to navigate to the Google Drive Login page, enter your Google id and password. After authentication it will ask you to grant access to view your data as shown in the following screen:

Accss-googledata-using-outh-3.jpg

Click on the accept button to access your documents. In the following screen shot it shows the list documents of the Google account.

Note: Here we can use "Refresh Token" to access and manage Google documents on behalf of the user when the user is not present at the browser.

If we specify AccessType="offline"

Then this will result in your application obtaining a refresh token the first time your application exchanges an authorization code for a user.

Accss-googledata-using-outh-4.jpg

If have any doubts or problems then please comment.

Up Next
    Ebook Download
    View all
    Learn
    View all