This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Try this:
service.getFeed(new URL("https://www.google.com/m8/feeds/contacts/default/full"), ContactFeed.class)