Rich

Thursday, October 7, 2010

[c# .net asp.net MVC2] Import contacts gmail

In an application, you may need to import contacts from a gmail account.
The construction is very simple and based on the Google Data API, a collection of libraries that provides a simple protocol for reading and writing data from the web.

First, download the GoogleDataApi
(you find here),  install them on your PC and add them as a reference to your project (in this case we can use Google.GData.Contacts.dll, Google.GData.Client.dll, Google.GData.Extensions.dll).
Importing different namespace


using Google.GData.Contacts;
using Google.GData.Client;
using Google.GData.Extensions;

Now you are ready to implement the code to read your Gmail contacts.

string authSubUrl = AuthSubUtil.getRequestUrl("http://www.example.com/Hello.asp", "http://www.google.com/m8/feeds/", False, True);

GAuthSubRequestFactory authFactory = new GAuthSubRequestFactory("cp", "exampleCo-exampleApp-1");
authFactory.Token = (String)Session["token"];
ContactsService service = new ContactsService("exampleCo-exampleApp-1");
  service.setUserCredentials(email, password);
ContactsQuery query = new ContactsQuery(ContactsQuery.CreateContactsUri("default"));
ContactsFeed feed = service.Query(query);




At this point in the feed.Entries there are contacts.
enough to extrapolate a simple loop to read through all the contacts, for example:


foreach (ContactEntry item in feed.Entries)

{

      // in item.Name we have various "names" of the contact, for example:
      //item.Name.FullName
      //item.Name.FamilyName
      //item.Name.GivenName
      //item.Name.NamePrefix
      //item.Name.NameSuffix
      //item.Name.AdditonalName
     // item.Emails while inside we have all the addresses associated with the various properties,
    // So here just a simple read cycle to extract all the data:

             foreach (EMail email in entry.Emails)

                {
                    // Email.Address you get in your email address
                    
// In email.Home, email.Work, email.Other is a bool that
                    
// Indicate if this address is
                    
// Classified as Home, Work or Other

                 }
}

Of course this is just an example of using these bees, for the simple extrapolation of the contacts from Gmail, the functions provided are varied and you can do much more.

No comments:

Post a Comment

Program PTC...earning reading advertise