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.

 

  1. <form id="form1" runat="server">  
  2. <div>  
  3. <asp:ListBox ID="lstDocuments" runat="server" Height="131px" Width="160px"></asp:ListBox>  
  4. <br />  
  5. <br />  
  6. <asp:Button ID="btnGetDocuments" runat="server" Text="GetDocuments" OnClick="btnGetDocuments_Click" />  
  7. </div>  
  8. </form>

And write the following code in the GoogleOauthCallback.cs file

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using Google.GData.Client;  
  8. using Google.GData.Documents;  
  9. namespace AccessGoogleDriveData  
  10. {  
  11.     public partial class GoogleOAuthCallback : System.Web.UI.Page  
  12.     {  
  13.         protected void Page_Load(object sender, EventArgs e)  
  14.         {  
  15.             string state = Request.QueryString["state"];  
  16.             // creating oauthparameter with client id and secret key  
  17.             OAuth2Parameters parameters = new OAuth2Parameters()  
  18.             {  
  19.                 ClientId = " Please enter ur registerd client id ",  
  20.                 ClientSecret = " please enter secret key ",  
  21.                 RedirectUri = "http://localhost:16615/GoogleOAuthCallback.aspx",  
  22.                 Scope = "https://docs.google.com/feeds/ ",  
  23.                 State = "documents",  
  24.                 AccessType = "offline"  
  25.             };  
  26.             lstDocuments.Visible = false;  
  27.             if (state != null)  
  28.             {  
  29.                 parameters.AccessCode = Request.QueryString["code"];  
  30.                 // it gets accesstoken from google  
  31.                 Google.GData.Client.OAuthUtil.GetAccessToken(parameters);  
  32.                 GOAuth2RequestFactory requestFactory = new GOAuth2RequestFactory(null"MyDocumentsListIntegration-v1", parameters);  
  33.                 DocumentsService service = new DocumentsService("MyDocumentsListIntegration-v1");  
  34.                 service.RequestFactory = requestFactory;  
  35.                 DocumentsListQuery query = new DocumentsListQuery();  
  36.                 // Make a request to the API and get all documents.  
  37.                 DocumentsFeed feed = service.Query(query);  
  38.                 lstDocuments.Visible = true;  
  39.                 if (feed.Entries.Count > 0)  
  40.                 {  
  41.                     var documentsList = from entry in feed.Entries select entry.Title.Text;  
  42.                     lstDocuments.DataSource = documentsList;  
  43.                     lstDocuments.DataBind();  
  44.                 }  
  45.             }  
  46.         }  
  47.         protected void btnGetDocuments_Click(object sender, EventArgs e)  
  48.         {  
  49.             OAuth2Parameters parameters = new OAuth2Parameters()  
  50.             {  
  51.                 ClientId = " Please enter ur registerd client id ",  
  52.                 ClientSecret = " please enter secret key ",  
  53.                 RedirectUri = "http://localhost:16615/GoogleOAuthCallback.aspx",  
  54.                 Scope = "https://docs.google.com/feeds/ ",  
  55.                 State = "documents",  
  56.                 AccessType = "offline" // offline means it creats a refreshtoken  
  57.             };  
  58.             string url = Google.GData.Client.OAuthUtil.CreateOAuth2AuthorizationUrl(parameters);  
  59.             Session["oauthDocumentsParameters"] = parameters;  
  60.             // it redirct to google login page  
  61.             Response.Redirect(url);  
  62.         }  
  63.     }  
  64. } 

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.


Similar Articles