Access Google Contacts using Google GDATA api in JAVA

We can access google contacts for particular user credentials very easily now using java. Thanks to Google GDATA api. Google GDATA provides lots of library modules to access google data from client side. Google GDATA api is available for various platforms such as JAVA, .NET, PHP, Python etc.

We will be using JAVA for this particular example. Eclipse will be used as the java IDE.

Before we start, we need to download the api package (ie gdata-src.java-1.45.0.zip) from the below link:

http://code.google.com/p/gdata-java-client/downloads/list

Follow the below steps:

1. Create a new java project in Eclipse IDE.

2. Create any package structure as of your need.

3. Create a new class lets say, GoogleContactsAccess.

4. Add the following jar files to project build path. You can find those jar files in, gdata-src.java-1.45.0\gdata\java\lib\ folder.

And google-collect-1.0-rc1.jar is avaiable in gdata-src.java-1.45.0\gdata\java\deps\ folder.

a. gdata-core-1.0.jar b. gdata-client-meta-1.0.jar c. gdata-client-1.0.jar d. gdata-contacts-meta-3.0.jar e. gdata-contacts-3.0.jar f. google-collect-1.0-rc1.jar 5. Go through the below code and add it to the class file.

import com.google.gdata.client.contacts.ContactsService; import com.google.gdata.data.Link; import com.google.gdata.data.PlainTextConstruct; import com.google.gdata.data.contacts.ContactEntry; import com.google.gdata.data.contacts.ContactFeed; import com.google.gdata.data.contacts.GroupMembershipInfo; import com.google.gdata.data.extensions.Email; import com.google.gdata.data.extensions.ExtendedProperty; import com.google.gdata.data.extensions.FamilyName; import com.google.gdata.data.extensions.FullName; import com.google.gdata.data.extensions.GivenName; import com.google.gdata.data.extensions.Im; import com.google.gdata.data.extensions.Name;

import com.google.gdata.util.ServiceException;

public class GoogleContactsAccess{

/* This method will authenticate the user credentials passed to it and returns an instance of ContactService class.*/

public ContactsService authenticateId(String userid, String password){

ContactsService contactService = null; try{ contactService = new ContactsService(“RupalMindfire-AddressApp”); contactService.setUserCredentials(userid, password); this.userId = userid; }catch(Exception e){ System.out.println(e); }

return contactService;

}

/* This method will print details of all the contacts available in that particular Google account. */

public void printAllContacts(ContactsService myService)throws ServiceException, IOException {

// Request the feed
URL feedUrl = new URL(“https://www.google.com/m8/feeds/contacts/default/full”);

ContactFeed resultFeed = myService.getFeed(feedUrl, ContactFeed.class);

// Print the results System.out.println(resultFeed.getTitle().getPlainText()); for (int i = 0; i < resultFeed.getEntries().size(); i++) { ContactEntry entry = resultFeed.getEntries().get(i); if (entry.hasName()) { Name name = entry.getName(); if (name.hasFullName()) { String fullNameToDisplay = name.getFullName().getValue(); if (name.getFullName().hasYomi()) { fullNameToDisplay += ” (” + name.getFullName().getYomi() + “)”; } System.out.println(“\t\t” + fullNameToDisplay); } else { System.out.println(“\t\t (no full name found)”);

}

if (name.hasNamePrefix()) { System.out.println(“\t\t” + name.getNamePrefix().getValue()); } else { System.out.println(“\t\t (no name prefix found)”); } if (name.hasGivenName()) { String givenNameToDisplay = name.getGivenName().getValue(); if (name.getGivenName().hasYomi()) { givenNameToDisplay += ” (” + name.getGivenName().getYomi() + “)”; } System.out.println(“\t\t” + givenNameToDisplay); } else { System.out.println(“\t\t (no given name found)”);

}

if (name.hasAdditionalName()) { String additionalNameToDisplay = name.getAdditionalName().getValue(); if (name.getAdditionalName().hasYomi()) { additionalNameToDisplay += ” (” + name.getAdditionalName().getYomi() + “)”; } System.out.println(“\t\t” + additionalNameToDisplay); } else { System.out.println(“\t\t (no additional name found)”);

}

if (name.hasFamilyName()) { String familyNameToDisplay = name.getFamilyName().getValue(); if (name.getFamilyName().hasYomi()) { familyNameToDisplay += ” (” + name.getFamilyName().getYomi() + “)”; } System.out.println(“\t\t” + familyNameToDisplay); } else { System.out.println(“\t\t (no family name found)”);

}

if (name.hasNameSuffix()) { System.out.println(“\t\t” + name.getNameSuffix().getValue()); } else { System.out.println(“\t\t (no name suffix found)”);

}

} else { System.out.println(“\t (no name found)”);

}

System.out.println(“Email addresses:”);

for (Email email : entry.getEmailAddresses()) {

System.out.print(” ” + email.getAddress()); if (email.getRel() != null) { System.out.print(” rel:” + email.getRel()); } if (email.getLabel() != null) { System.out.print(” label:” + email.getLabel()); } if (email.getPrimary()) { System.out.print(” (primary) “); }

System.out.print(“\n”);

}

System.out.println(“IM addresses:”);
for (Im im : entry.getImAddresses()) {

System.out.print(” ” + im.getAddress()); if (im.getLabel() != null) { System.out.print(” label:” + im.getLabel()); } if (im.getRel() != null) { System.out.print(” rel:” + im.getRel()); } if (im.getProtocol() != null) { System.out.print(” protocol:” + im.getProtocol()); } if (im.getPrimary()) { System.out.print(” (primary) “); }

System.out.print(“\n”);

}

System.out.println(“Groups:”); for (GroupMembershipInfo group : entry.getGroupMembershipInfos()) { String groupHref = group.getHref(); System.out.println(” Id: ” + groupHref);

}

System.out.println(“Extended Properties:”);
for (ExtendedProperty property : entry.getExtendedProperties()) {

if (property.getValue() != null) { System.out.println(” ” + property.getName() + “(value) = ” + property.getValue()); } else if (property.getXmlBlob() != null) { System.out.println(” ” + property.getName() + “(xmlBlob)= ” + property.getXmlBlob().getBlob());

}

}

Link photoLink = entry.getContactPhotoLink(); String photoLinkHref = photoLink.getHref();

System.out.println(“Photo Link: ” + photoLinkHref);

if (photoLink.getEtag() != null) { System.out.println(“Contact Photo’s ETag: ” + photoLink.getEtag());

}

System.out.println(“Contact’s ETag: ” + entry.getEtag()); } } /* This method will add a contact to that particular Google account */

public void createContact(ContactsService myService,String fullName, String givenName, String familyName, String notes,String emailId,String favSports) throws ServiceException, IOException { // Create the entry to insert ContactEntry contact = new ContactEntry(); Name name = new Name(); final String NO_YOMI = null; name.setFullName(new FullName(fullName, NO_YOMI)); name.setGivenName(new GivenName(givenName, NO_YOMI)); name.setFamilyName(new FamilyName(familyName, NO_YOMI)); contact.setName(name);

contact.setContent(new PlainTextConstruct(notes));

Email primaryMail = new Email(); primaryMail.setAddress(emailId); primaryMail.setRel(“http://schemas.google.com/g/2005#home”); primaryMail.setPrimary(true);

contact.addEmailAddress(primaryMail);

ExtendedProperty sportsProperty = new ExtendedProperty(); sportsProperty.setName(“sports”); sportsProperty.setValue(favSports);

contact.addExtendedProperty(sportsProperty);

// Ask the service to insert the new entry URL postUrl = new URL(“https://www.google.com/m8/feeds/contacts/default/full”); myService.insert(postUrl, contact);

}

public static void main(String ar[]){
try {

GoogleContactsAccess googleContactsAccess = new GoogleContactsAccess();

ContactsService contactSrv = googleContactsAccess.authenticateId(“username”, “password”);

googleContactsAccess.printAllContacts(contactSrv);

googleContactsAccess.createContact(contactService, “fullname”, “givenname”, “familyname”, “notes”,”[email protected]”,”fav sports”);

} catch (MalformedURLException ex) { System.out.println(ex); } catch (IOException ex) { System.out.println(ex); }catch (Exception ex) { System.out.println(ex); }

}

}

150 150 Burnignorance | Where Minds Meet And Sparks Fly!