NullPointerException only after I have setServiceAccountUser () for a GoogleCredential object (Grails / Java)

I get a NullPointerException with very little detail when I try to setServiceAccountUser(ACCOUNT_TO_IMPERSONATE) on my GoogleCredential , create a Google task service object and then try to send a task list request from ACCOUNT_TO_IMPERSONATE .

 def credential = new GoogleCredential.Builder() .setTransport(HTTP_TRANSPORT) .setJsonFactory(JSON_FACTORY) .setServiceAccountId(SERVICE_ACCOUNT_ID) .setServiceAccountPrivateKeyFromP12File(P12_FILE) .setServiceAccountScopes(GOOGLE_TASK_SCOPES) .setServiceAccountUser(ACCOUNT_TO_IMPERSONATE) .build() credential.refreshToken() log.debug("Google Credential Impersonating: ${credential.getServiceAccountUser()}") def service = new com.google.api.services.tasks.Tasks.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).build() return service 

Note that credential.getServiceAccountUser() outputs the expected ACCOUNT_TO_IMPERSONATE .

A similar issue occurred at https://github.com/googleads/googleads-java-lib/issues/19

 StackTrace : Caused by: java.lang.NullPointerException at com.google.api.client.repackaged.com.google.common.base.Preconditions.checkNotNull(Preconditions.java:191) at com.google.api.client.util.Preconditions.checkNotNull(Preconditions.java:127) at com.google.api.client.json.jackson2.JacksonFactory.createJsonParser(JacksonFactory.java:96) at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:85) at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:81) at com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:88) at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:287) at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:307) at com.google.api.client.auth.oauth2.Credential.executeRefreshToken(Credential.java:570) at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:247) at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489) at com.google.api.ads.common.lib.auth.OAuth2Helper.callRefreshToken(OAuth2Helper.java:70) at com.google.api.ads.common.lib.auth.OfflineCredentials.generateCredential(OfflineCredentials.java:144) ... 

The lack of detail in the error was said to be a problem in older versions of Google Tasks, but I upgraded to the latest version and see no difference.

 compile "com.google.api-client:google-api-client:1.19.0" compile 'com.google.apis:google-api-services-tasks:v1-rev41-1.19.0' 

EDIT: after processing the code in the grails console in our web application, I see that executing credential.refreshToken() calls NPE when ServiceAccountUser installed.

When I turn off ServiceAccountUser , refreshToken() will succeed. In addition, an attempt to retrieve a Google task list is successful for a service account if the ServiceAccountUser parameter ServiceAccountUser not set.

Any help would be greatly appreciated.

+7
java grails google-api google-tasks-api
source share
2 answers

I found the reason for the behavior that we experienced, but thanks for your suggestions, however, user1807337 and Srinivasan.

We need our application users to delegate domain authority to our application. This can be done by the Google Apps for Business administrator by following the instructions in the subsection entitled Delegating Domain Authority in a Service Account

https://developers.google.com/accounts/docs/OAuth2ServiceAccount

Here is the explanation given in the same resource: "If you have delegated access to the domain service account for the domain and you want to impersonate the user account, specify the email address of the user account using the setServiceAccountUser method for GoogleCredential factory ..."

+2
source share

try it

 'com.google.api.client.googleapis.auth.oauth2.GoogleCredential.Builder credential_origine_builder = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT) .setJsonFactory(JSON_FACTORY) .setServiceAccountId("[[]]") .setServiceAccountScopes(GOOGLE_TASK_SCOPES) .setServiceAccountPrivateKeyFromP12File(new File("cfg/file.p12")); 

credential_origine_builder.setServiceAccountUser (" user@domain.com "); ''

0
source share

All Articles