martedì 23 ottobre 2012

C# Google API - Retrieve All Google Contacts

Hi, 
a friend of mine, asked me to write a program to make a contacts archive, and synchronize it with google contact.
I'd like to share what i've learned with you, and i start with getting the all google contact, showing by the following code.
I put the login code into the method to make it more clear, but I think is better write it as globals objects.
This method gets the google contacts list with the phone numbers, but we can obtain all the info we have/need about our contacts, accessing them with ContactsFeed proprierties.

private List<string[]> GetContact()
        {
            // login 
            ContactsService Service = new ContactsService("google-contacts");
            Service.setUserCredentials("your.email@gmail.com", "yourPassword");
            Service.QueryClientLoginToken();

            List<string[]> Contacts = new List<string[]>();


            ContactsQuery query = new ContactsQuery(ContactsQuery.CreateContactsUri("default"));

            query.StartIndex = 1;

            ContactsFeed feed = Service.Query(query);


            while (feed != null && feed.Entries.Count > 0)

            {
                foreach (ContactEntry entry in feed.Entries)
                {
                    // This Example gets the phone numbers but we can obtain the info we want/need about the contact 
                    if (entry.Phonenumbers.Count > 0) 
                    {
                        string phoneNumbers = "";
                        // get all phone numbers
                        for (int a = 0; a < entry.Phonenumbers.Count; a++)
                        {
                            phoneNumbers += entry.Phonenumbers[a].Value + "; ";
                        }

                        int x = phoneNumbers.LastIndexOf(";");

                        phoneNumbers = phoneNumbers.Substring(0, phoneNumbers.LastIndexOf(";"));
                        
                        // in the array "item" there are: fullname(name and surname), phone number1;phonenumber2;...., contact url for edit
                        string[] item = { entry.Name.FullName, phoneNumbers, entry.Id.AbsoluteUri };
                        Contacts.Add(item);
                    }
                }

                // move to next page until end

                if (feed.NextChunk != null)
                {
                    query.Uri = new Uri(feed.NextChunk);
                    feed = Service.Query(query) as ContactsFeed;
                }
                else
                    feed = null;
            }

            // sorting the list for full name

            Contacts.Sort(delegate(string[] item1, string[] item2)
            {
                return item1[0].CompareTo(item2[0]);
            });

            
return Contacts;
        }


I want to remeber to go here http://support.google.com/mail/bin/answer.py?hl=it&answer=1173270 to get  Application-specific password


Here is possible to download an example!


My Two Cents...

8 commenti:

  1. Hi! this sample need password, but can I use oAuth (using AccessToken) to get the mail list?

    RispondiElimina
    Risposte
    1. sorry, do you need email address list or only email?

      Elimina
  2. Hello Ruddy,
    thank you for reading my post and for your question.
    I'm Trying a solution to your problem.

    Regards

    RispondiElimina
  3. Questo commento è stato eliminato dall'autore.

    RispondiElimina
    Risposte
    1. Hello Kiquenet,
      thank you for reading my post.
      Soon a Complete solution! :-)

      Have a Goog Day

      Elimina
    2. you can get it on codeplex: https://googlecontacts.codeplex.com/

      Elimina
  4. Questo commento è stato eliminato da un amministratore del blog.

    RispondiElimina
    Risposte
    1. Questo commento è stato eliminato da un amministratore del blog.

      Elimina