Home > Develop, Home, Web > Google Contacts .net Library

Google Contacts .net Library

March 28th, 2008

Something I’ve been working on for another project and until Google updates their official library here is my own version. There are few missing pieces but for the most part it’s all there. I’ll post updates to the code as I make them but for now if you are familiar with the way the current library works then this won’t be any different.

You will need a reference to the Google .Net Data API’s (duh) also in your project.

            ContactService cs = new ContactService(”contactr”);
            cs.SetAuthenticationToken(Properties.Settings.Default.AuthToken);

            ContactQuery query = new ContactQuery();
            query.Uri = new Uri(
                string.Format(”http://www.google.com/m8/feeds/contacts/{0}/base”,
                              Properties.Settings.Default.GoogleLogin)
                );
            query.NumberToRetrieve = 1000;
            query.ShowDeleted = false;

           ContactFeed feed = cs.Query(query) as ContactFeed;
           if (feed == null)
                Console.WriteLine(”Feed is empty”);
           string emailString = “”;
           foreach (ContactEntry contactEntry in feed.Entries)
           {
                if(contactEntry.FullName != null)
                {
                    ListViewItem lvi = new ListViewItem(string.Format(”{0}”, contactEntry.FullName));
                    foreach (GoogleContacts.ContactExtensions.Email address in contactEntry.Emails)
                        if (address.Primary) {
                            emailString = address.Address;
                            if (address.Label != null)
                                emailString += string.Format(” ({0})”, address.Label);
                        }
                   // write out the simplified contact item.
                    Console.WriteLine(emailString);
                }
            }

Download the bits here (including source): Local404.GoogleContacts

Questions? You can contact me through the comments section.

Develop, Home, Web

  1. brian zinn
    March 28th, 2008 at 20:22 | #1

    thanks for posting your code. definitely saved me lots of time! i had it working in 3 minutes. 1 minute to realize it wouldn’t run in vs2005! if it’s your first time with google services you can set authToken like this (it was my first use of the google api):
    cs.setUserCredentials("email@gmail.com", "paswd");
    string authToken = cs.QueryAuthenticationToken();
    cs.SetAuthenticationToken(authToken);

    thanks again for sharing.

  2. Brian Zinn
    March 28th, 2008 at 20:22 | #2

    Google posted their .NET client code today:
    http://groups.google.com/group/google-contacts-api/browse_thread/thread/357d7c8290f0e5e9

    It’s missing some of your features, such as query.NumberToRetrieve. They’ve also added PhotoUri to the ContactEntry.

    Thought you might be interested.

  1. No trackbacks yet.