Вызов API контактов Google через учетную запись службы OAuth 2.0 дает AuthenticationException

Вот код, над которым я работал:

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;
    }

И я получаю следующее исключение:

java.lang.NullPointerException: нет информации заголовка аутентификации в com.google.gdata.util.AuthenticationException.initFromAuthHeader(AuthenticationException.java:96) ~[core-1.47.1.jar:na] в com.google.gdata.util. AuthenticationException.(AuthenticationException.java:67) ~[core-1.47.1.jar:na] в com.google.gdata.client.http.HttpGDataRequest.handleErrorResponse(HttpGDataRequest.java:608) ~[core-1.47.1. jar:na] на com.google.gdata.client.http.GoogleGDataRequest.handleErrorResponse(GoogleGDataRequest.java:564) ~[core-1.47.1.jar:na] на com.google.gdata.client.http.HttpGDataRequest. checkResponse(HttpGDataRequest.java:560) ~[core-1.47.1.jar:na] в com.google.gdata.client.http.HttpGDataRequest.execute(HttpGDataRequest.java:538) ~[core-1.47.1.jar :na] в com.google.gdata.client.http.GoogleGDataRequest.execute(GoogleGDataRequest.java:536) ~[core-1.47.1.jar:na] в com.google.gdata.client.Service.getFeed(Service .java:1135) ~[core-1.47.1.jar:na] в com.google.gdata.client.Servic e.getFeed(Service.java:1077) ~[core-1.47.1.jar:na] в com.google.gdata.client.GoogleService.getFeed(GoogleService.java:676) ~[core-1.47.1.jar :na] в com.google.gdata.client.Service.query(Service.java:1237) ~[core-1.47.1.jar:na] в com.google.gdata.client.Service.query(Service.java :1178) ~[ядро-1.47.1.jar: нет]

Пожалуйста помоги!


person Pritesh Lahoti    schedule 30.01.2015    source источник


Ответы (1)


Добавьте этот обходной путь в блок инициализации ContactsService.

service.getRequestFactory().setHeader("User-Agent", applicationName);
person user3686724    schedule 30.01.2015
comment
Спасибо за ответ, но на этот раз я все еще получаю другое исключение: java.lang.NullPointerException: null at com.google.api.client.repackaged.com.google.common.base.Preconditions.checkNotNull(Preconditions.java:191 ) ~[google-http-client-1.19.0.jar:1.19.0] в com.google.api.client.util.Preconditions.checkNotNull(Preconditions.java:127) ~[google-http-client-1.19. 0.jar:1.19.0] на com.google.api.client.json.jackson2.JacksonFactory.createJsonParser(JacksonFactory.java:92) ~[google-http-client-jackson2-1.19.0.jar:1.19.0 ] в com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectPa... - person Pritesh Lahoti; 31.01.2015
comment
Я использую тот же код, что и ваш, но мой использует google.com/m8/feeds/ contact/default/full в качестве областей. - person user3686724; 01.02.2015