import com.google.gdata.client.contacts.ContactsService;
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.ExtendedProperty;
import com.google.gdata.data.extensions.Im;
import com.google.gdata.util.ServiceException;
import java.io.IOException;
import java.net.Authenticator;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.URL;
import org.apache.commons.codec.binary.Base64;
public class GoogleContactsAccess {
private String userId;
public ContactsService authenticateId(String userid, String
password){
ContactsService contactService = null;
try{
String encoded = new
String(Base64.encodeBase64(("proxyusername:proxypassword").getBytes()));
contactService = new ContactsService("RupalMindfire-
AddressApp");
System.out.print(contactService.getServiceVersion());
contactService.getRequestFactory().setPrivateHeader("Proxy-
Authorization","Basic "+encoded);
contactService.setUserCredentials(userid, password);
this.userId = userid;
}catch(Exception e){
e.printStackTrace();
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()) {
com.google.gdata.data.extensions.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:");
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());
}
}
}
}
static class ProxyAuthenticator extends Authenticator {
private String userName, password;
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName,
password.toCharArray());
}
public ProxyAuthenticator(String userName, String password) {
this.userName = userName;
this.password = password;
}
}
public static void main(String ar[]){
try {
System.setProperty("http.proxyHost", "proxyhostname");
System.setProperty("http.proxyPort", "80");
System.setProperty("http.proxyUser", "proxyusername");
System.setProperty("http.proxyPassword", "proxypassword");
System.setProperty("https.proxyHost", "proxyhostname");
System.setProperty("https.proxyPort", "80");
System.setProperty("https.proxyUser", "proxyusername");
System.setProperty("https.proxyPassword", "proxypassword");
System.setProperty("proxySet", "true");
Authenticator.setDefault(new
ProxyAuthenticator("proxyusername", "proxypassword"));
System.setProperty("http.proxySet", "true");
System.setProperty("https.proxySet", "true");
GoogleContactsAccess googleContactsAccess = new
GoogleContactsAccess();
ContactsService contactSrv =
googleContactsAccess.authenticateId("emilid", "password");
googleContactsAccess.printAllContacts(contactSrv);
//googleContactsAccess.createContact(contactService,
"fullname", "givenname", "familyname",
"notes","example@example.com","fav sports");
} catch (MalformedURLException ex) {
System.out.println(ex);
} catch (IOException ex) {
System.out.println(ex);
}catch (Exception ex) {
System.out.println(ex);
}
}
}
I used above gode it throws below exception:
GContacts-Java/3.0.1 GData-Java/
1.41.4(gzip)java.lang.ArrayIndexOutOfBoundsException
at java.lang.System.arraycopy(Native Method)
java.lang.ArrayIndexOutOfBoundsException
at
sun.net.www.protocol.http.NTLMAuthentication.buildType3Msg(NTLMAuthentication.java:
368)
at
sun.net.www.protocol.http.NTLMAuthentication.setHeaders(NTLMAuthentication.java:
225)
at
sun.net.www.protocol.http.HttpURLConnection.doTunneling(HttpURLConnection.java:
1557)
at
sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:
183)
at
sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:
979)
at
sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:
250)
at
com.google.gdata.client.GoogleAuthTokenFactory.makePostRequest(GoogleAuthTokenFactory.java:
551)
at
com.google.gdata.client.GoogleAuthTokenFactory.getAuthToken(GoogleAuthTokenFactory.java:
487)
at
com.google.gdata.client.GoogleAuthTokenFactory.setUserCredentials(GoogleAuthTokenFactory.java:
346)
at
com.google.gdata.client.GoogleService.setUserCredentials(GoogleService.java:
362)
at
com.google.gdata.client.GoogleService.setUserCredentials(GoogleService.java:
317)
at
com.google.gdata.client.GoogleService.setUserCredentials(GoogleService.java:
301)
at
googleex.GoogleContactsAccess.authenticateId(GoogleContactsAccess.java:
43)
at
googleex.GoogleContactsAccess.main(GoogleContactsAccess.java:229)
java.lang.ArrayIndexOutOfBoundsException
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.ExtendedProperty;
import com.google.gdata.data.extensions.Im;
import com.google.gdata.util.ServiceException;
import java.io.IOException;
import java.net.Authenticator;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.URL;
import org.apache.commons.codec.binary.Base64;
public class GoogleContactsAccess {
private String userId;
public ContactsService authenticateId(String userid, String
password){
ContactsService contactService = null;
try{
String encoded = new
String(Base64.encodeBase64(("proxyusername:proxypassword").getBytes()));
contactService = new ContactsService("RupalMindfire-
AddressApp");
System.out.print(contactService.getServiceVersion());
contactService.getRequestFactory().setPrivateHeader("Proxy-
Authorization","Basic "+encoded);
contactService.setUserCredentials(userid, password);
this.userId = userid;
}catch(Exception e){
e.printStackTrace();
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()) {
com.google.gdata.data.extensions.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:");
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());
}
}
}
}
static class ProxyAuthenticator extends Authenticator {
private String userName, password;
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName,
password.toCharArray());
}
public ProxyAuthenticator(String userName, String password) {
this.userName = userName;
this.password = password;
}
}
public static void main(String ar[]){
try {
System.setProperty("http.proxyHost", "proxyhostname");
System.setProperty("http.proxyPort", "80");
System.setProperty("http.proxyUser", "proxyusername");
System.setProperty("http.proxyPassword", "proxypassword");
System.setProperty("https.proxyHost", "proxyhostname");
System.setProperty("https.proxyPort", "80");
System.setProperty("https.proxyUser", "proxyusername");
System.setProperty("https.proxyPassword", "proxypassword");
System.setProperty("proxySet", "true");
Authenticator.setDefault(new
ProxyAuthenticator("proxyusername", "proxypassword"));
System.setProperty("http.proxySet", "true");
System.setProperty("https.proxySet", "true");
GoogleContactsAccess googleContactsAccess = new
GoogleContactsAccess();
ContactsService contactSrv =
googleContactsAccess.authenticateId("emilid", "password");
googleContactsAccess.printAllContacts(contactSrv);
//googleContactsAccess.createContact(contactService,
"fullname", "givenname", "familyname",
"notes","example@example.com","fav sports");
} catch (MalformedURLException ex) {
System.out.println(ex);
} catch (IOException ex) {
System.out.println(ex);
}catch (Exception ex) {
System.out.println(ex);
}
}
}
I used above gode it throws below exception:
GContacts-Java/3.0.1 GData-Java/
1.41.4(gzip)java.lang.ArrayIndexOutOfBoundsException
at java.lang.System.arraycopy(Native Method)
java.lang.ArrayIndexOutOfBoundsException
at
sun.net.www.protocol.http.NTLMAuthentication.buildType3Msg(NTLMAuthentication.java:
368)
at
sun.net.www.protocol.http.NTLMAuthentication.setHeaders(NTLMAuthentication.java:
225)
at
sun.net.www.protocol.http.HttpURLConnection.doTunneling(HttpURLConnection.java:
1557)
at
sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:
183)
at
sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:
979)
at
sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:
250)
at
com.google.gdata.client.GoogleAuthTokenFactory.makePostRequest(GoogleAuthTokenFactory.java:
551)
at
com.google.gdata.client.GoogleAuthTokenFactory.getAuthToken(GoogleAuthTokenFactory.java:
487)
at
com.google.gdata.client.GoogleAuthTokenFactory.setUserCredentials(GoogleAuthTokenFactory.java:
346)
at
com.google.gdata.client.GoogleService.setUserCredentials(GoogleService.java:
362)
at
com.google.gdata.client.GoogleService.setUserCredentials(GoogleService.java:
317)
at
com.google.gdata.client.GoogleService.setUserCredentials(GoogleService.java:
301)
at
googleex.GoogleContactsAccess.authenticateId(GoogleContactsAccess.java:
43)
at
googleex.GoogleContactsAccess.main(GoogleContactsAccess.java:229)
java.lang.ArrayIndexOutOfBoundsException