Saturday 10 September 2011

Importing Gmail Contacts Using Gmail API

Several times we need to develop web applications which require to import or fetch Gmail Contacts or address book.
In this example i'm explaining how to fetch or import Gmail contacts in Asp.net web applications using C# and ASP.NET.
For getting started we need to download  Google Data API  and install on the system to get the desired dlls.
Create a new website and visual studio and put these 3 dlls in BIN folder of application from the location google data API has been installed on ur system.
1. Google.GData.Client
2. Google.GData.Contacts
3. Google.GData.Extensions

Now add references to these dlls in ur application by right clicking on solution explorer and select add reference.
Login.aspx:
<asp:ImageButton ID="btnGoogle" Text="Google" ImageUrl="gmail-icon.jpg.png" runat="server" ToolTip="Login to your gmail account
and import your contacts." />
Login.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Google.Contacts;
using Google.GData.Client;
using Google.GData.Contacts;
using Google.GData.Extensions;
public partial class Welcome : System.Web.UI.Page
{
    protected void Page_Load(object sender, System.EventArgs e)
    {
        if (Request.QueryString["token"] != null)
        {
            string token = Request.QueryString["token"];
            Session["token"] = AuthSubUtil.exchangeForSessionToken     (token, null).ToString();
            Response.Redirect("~/Welcome.aspx", true);
        }
        else
        {
            btnGoogle.PostBackUrl = AuthSubUtil.getRequestUrl(Request.Url.ToString(), "http://www.google.com/m8/feeds/", false, true);
        }
    }
Welcome.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Google.Contacts;
using Google.GData.Client;
using Google.GData.Contacts;
using Google.GData.Extensions;
public partial class Welcome : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["token"] != null)
        {
            RetrieveToken();
         }
}
public void RetrieveToken()
    {
        GAuthSubRequestFactory authFactory = new GAuthSubRequestFactory("cp", "myapp");
        string x = Convert.ToString(Session["token"]);
        RequestSettings rs = new RequestSettings("myapp", x);
        rs.AutoPaging = true;
        ContactsRequest cr = new ContactsRequest(rs);
        try
        {
            Feed<Google.Contacts.Contact> f = cr.GetContacts();
            List<string> y = new List<string>();
            foreach (Google.Contacts.Contact e in f.Entries)
            {
                foreach (EMail email in e.Emails)
                {
                   y.Add(email.Address);
                 }
            }
        }
        catch (GDataRequestException gdre)
        {
       }
    }
Note: If rs.AutoPaging=false then only first 25 contacts will be displyed from gmail. So to get all contacts from gmail it must be true.
I think this post will help you.
Thanks.

12 comments:

  1. Hi sir,

    How to get user profile from gmail using Gmail API (or) using OAuth?

    Please help me..!

    Thanks in advance,
    prapoorna.

    ReplyDelete
    Replies
    1. HI,
      Its better to use social Auth .net dll 2.0 version to get user Profile Details.it is a library where we can download contacts/Profile from various sources like- Gmail,Facebook, MSN,Yahoo, LinedIn etc. U can try the demo project which is attached at the url:

      http://code.google.com/p/socialauth-net/

      Delete
  2. Hi sir,

    Can u please give me the complete code for getting user profile from gmail using oauth? I have consumer key and secret but i didn't understand how to implement using C# to get userprofile. Please help me..!

    Thanks in advance,
    prapoorna.

    ReplyDelete
    Replies
    1. you can download demo of getting profile from gmail from following url:

      http://code.google.com/p/socialauth-net/downloads/detail?name=SocialAuth-net-2.0beta.zip&can=2&q=

      just download this demo and open in VS 2010 and
      study it and modify it accordingly.

      Delete
  3. I want add / modify / delete google contacts with oauth in vs 2008

    ReplyDelete
  4. hi all,
    It working fine and, very helpful to me








    hi it is very helpful to me, its working fine

    ReplyDelete
    Replies
    1. hi mr sitha ramaiah garu.
      i am also following an a same way but getting this error Execution of authentication request returned unexpected result: 404.please help me how can i overcome this..

      Delete
  5. i need Facebook and MSN and yahoo get Contact List using C#.NET/Asp.NET code
    please sent to in this emailid:emails4boopalan@gmail.com..i am awaiting for ur reply...

    ReplyDelete
  6. Hi i used this code and i got lot of errors plz tell me what to do...

    ReplyDelete
  7. i am getting a null value in f.entriries
    foreach (Google.Contacts.Contact e in f.Entries)
    {
    foreach (EMail email in e.Emails)
    {
    y.Add(email.Address);
    }
    }
    Can you please help me and i need to show the contacts in a grid.

    ReplyDelete
  8. Execution of authentication request returned unexpected result: 404
    how to overcome this error

    ReplyDelete