Showing posts with label Gmail. Show all posts
Showing posts with label Gmail. Show all posts

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.