Calling Google Contacts API through OAuth 2.0 service account gives AuthenticationException

537 Views Asked by At

Here's the code I have been working on:

public String getGoogleContact(String queryStr) throws
    AuthenticationException, MalformedURLException, IOException,
    ServiceException, GeneralSecurityException{

        String emailAddress = "[email protected]";
        JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
        HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        String path = "src/main/resources/key.p12";

        GoogleCredential credential = new GoogleCredential.Builder()
                                    .setTransport(httpTransport)
                                    .setJsonFactory(JSON_FACTORY)
                                    .setServiceAccountId(emailAddress)
                                    .setServiceAccountPrivateKeyFromP12File(new java.io.File(path))
                                    .setServiceAccountScopes(Collections.singleton("https://www.google.com/m8/feeds"))
                                    .setServiceAccountUser("[email protected]").build();

        ContactsService service = new ContactsService("MYAPP");
        service.setOAuth2Credentials(credential);
        Query myQuery = new Query( new URL("https://www.google.com/m8/feeds/contacts/default/full"));
        myQuery.setFullTextQuery(queryStr);
        ContactFeed resultFeed = service.query(myQuery, ContactFeed.class);
        String result = "";

       for (ContactEntry entry : resultFeed.getEntries()) {
           result += entry.getTitle().getPlainText();
       }

       return result;
    }

And I get the following exception:

java.lang.NullPointerException: No authentication header information at com.google.gdata.util.AuthenticationException.initFromAuthHeader(AuthenticationException.java:96) ~[core-1.47.1.jar:na] at com.google.gdata.util.AuthenticationException.(AuthenticationException.java:67) ~[core-1.47.1.jar:na] at com.google.gdata.client.http.HttpGDataRequest.handleErrorResponse(HttpGDataRequest.java:608) ~[core-1.47.1.jar:na] at com.google.gdata.client.http.GoogleGDataRequest.handleErrorResponse(GoogleGDataRequest.java:564) ~[core-1.47.1.jar:na] at com.google.gdata.client.http.HttpGDataRequest.checkResponse(HttpGDataRequest.java:560) ~[core-1.47.1.jar:na] at com.google.gdata.client.http.HttpGDataRequest.execute(HttpGDataRequest.java:538) ~[core-1.47.1.jar:na] at com.google.gdata.client.http.GoogleGDataRequest.execute(GoogleGDataRequest.java:536) ~[core-1.47.1.jar:na] at com.google.gdata.client.Service.getFeed(Service.java:1135) ~[core-1.47.1.jar:na] at com.google.gdata.client.Service.getFeed(Service.java:1077) ~[core-1.47.1.jar:na] at com.google.gdata.client.GoogleService.getFeed(GoogleService.java:676) ~[core-1.47.1.jar:na] at com.google.gdata.client.Service.query(Service.java:1237) ~[core-1.47.1.jar:na] at com.google.gdata.client.Service.query(Service.java:1178) ~[core-1.47.1.jar:na]

Please help!

1

There are 1 best solutions below

2
user3686724 On

Add this workaround to your ContactsService init block

service.getRequestFactory().setHeader("User-Agent", applicationName);