Skip to content

Instantly share code, notes, and snippets.

@musketyr
Created March 6, 2014 17:24
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save musketyr/9394815 to your computer and use it in GitHub Desktop.
package com.example.com;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import java.util.Collections;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServlet;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.plus.Plus;
import com.google.api.services.plus.PlusScopes;
import com.google.gdata.client.contacts.ContactsService;
import com.google.gdata.client.http.HttpGDataRequest;
import com.google.gdata.data.contacts.ContactFeed;
import com.google.gdata.util.ServiceException;
@SuppressWarnings("serial")
public class Contacttest2Servlet extends HttpServlet {
private static final JsonFactory JSON_FACTORY = new JacksonFactory();
public static void main(String[] args) throws IOException {
try {
GoogleCredential credential = new GoogleCredential.Builder().setTransport(GoogleNetHttpTransport.newTrustedTransport())
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId("XXX@developer.gserviceaccount.com")
.setServiceAccountScopes(Arrays.asList(PlusScopes.PLUS_ME, "https://www.google.com/m8/feeds/"))
.setServiceAccountPrivateKeyFromP12File(new File("privatekey.p12 "))
.setServiceAccountUser("xxx@example.com")
.build();
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
Plus plus = new Plus.Builder(GoogleNetHttpTransport.newTrustedTransport(), JSON_FACTORY, credential)
.setApplicationName("Google-PlusServiceAccountSample/1.0")
.setHttpRequestInitializer(credential).build();
// THIS WORKS
System.out.println(plus.activities());
ContactsService service = new ContactsService("contacttest2");
service.setOAuth2Credentials(credential);
// THIS THROWS:
/* Exception in thread "main" java.lang.NullPointerException: No authentication header information
at com.google.gdata.util.AuthenticationException.initFromAuthHeader(AuthenticationException.java:96)
at com.google.gdata.util.AuthenticationException.<init>(AuthenticationException.java:67)
at com.google.gdata.client.http.HttpGDataRequest.handleErrorResponse(HttpGDataRequest.java:608)
*/
System.out.println(service.getFeed(new URL("https://www.google.com/m8/feeds/"), ContactFeed.class));
} catch (GeneralSecurityException e) {
e.printStackTrace();
} catch (ServiceException e) {
e.printStackTrace();
}
}
}
@qtxo
Copy link

qtxo commented Nov 18, 2014

Try this:
service.getFeed(new URL("https://www.google.com/m8/feeds/contacts/default/full"), ContactFeed.class)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment