El TecnoBaúl de Kiquenet

Kiquenet boring stories

Google Contacts API

Posted by kiquenet en 26 diciembre 2014

Nuget: https://www.nuget.org/packages?q=google+contacts

Install-Package Google.GData.Contacts (version 2.2)
Install-Package Google.GData.Client (version 2.2 stable)

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

Code

      [TestMethod]
      public void Obtener_contactos_de_google()
      {
          // login
          var Service = new ContactsService("google-contacts");
          Service.setUserCredentials("xxxx@gmail.com", "xxxxx");
          Service.QueryClientLoginToken();

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

          var query = new ContactsQuery(ContactsQuery.CreateContactsUri("default"));
          query.StartIndex = 1;

          ContactsFeed feed = Service.Query(query);

          var total = 0;
          while (feed != null && feed.Entries.Count > 0)
          {
              total += feed.Entries.Count;
              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]);
          });

          Assert.AreNotEqual(0, Contacts.Count);
          Assert.IsTrue(total > 900);

      }

Source:
http://dotnetoday.blogspot.com.es/2012/10/c-google-api-getting-google-contacts.html?showComment=1382645543065

Anuncios

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s

 
A %d blogueros les gusta esto: