只有在为GoogleCredential对象(Grails / Java)设置了setServiceAccountUser()后,才需要NullPointerException [英] NullPointerException only after I have setServiceAccountUser() for GoogleCredential object (Grails/Java)
问题描述
当我试图在<$ c c> setServiceAccountUser(ACCOUNT_TO_IMPERSONATE)时收到一个 NullPointerException
$ c> GoogleCredential ,构建Google Tasks服务对象,然后尝试从 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()
凭证.refreshToken()
log.debug(Google Credential Impersonating:$ {credential.getServiceAccountUser()})
def service = new com.google.api.services.tasks.Tasks .Builder(HTTP_TRANSPORT,JSON_FACTORY,凭证).build()
返回系列请注意 credential.getServiceAccountUser()$ c $>
c>注销预期的 ACCOUNT_TO_IMPERSONATE
。
StackTrace:
由com.google.api.client.repackaged.com.google的java.lang.NullPointerException引起。 common.base.Preconditions.checkNotNull(Preconditions.java:191)
,位于com.google.api.client.util.Preconditions.checkNotNull(Preconditions.java:127)
位于com.google.api。 client.json.jackson2.JacksonFactory.createJsonParser(JacksonFactory.java:96)
在com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:85)
在com.google。 api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:81)
,位于com.google.api.client.auth.oauth2.TokenResponseException。从(TokenResponseException.java:88)
在com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:287)
在com.google.api.client.auth。 oauth2.TokenRequest.execute(TokenRequest.java:307)
,位于com.google.api.client.auth.oauth2.Credential.executeRefreshToken(Credential.java:570)
位于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)
,位于com.google.api.ads.common.lib.auth.OfflineCredentials.generateCredential(OfflineCredentials.java :144)
...
错误中缺少细节据说是旧版Google Tasks中存在问题,但我已升级到最新版本并且没有任何区别。
compilecom.google .API客户端:谷歌的API客户端:1.19.0
compile'com.google.apis:google-api-services-tasks:v1-rev41-1.19.0'
编辑:在我们的web应用程序的grails控制台中修改代码后,我可以看到执行 credential.refreshToken()
会导致NPE设置 ServiceAccountUser
。
当我取消设置 ServiceAccountUser
, refreshToken()
是成功的。此外,当未设置 ServiceAccountUser
时,试图检索Google Tasks列表可成功获取服务帐户。
任何帮助将不胜感激。
我发现我们遇到的行为的原因,但感谢您的建议, none-the-less,user1807337& Srinivasan。
我们需要让我们的应用程序用户将我们的应用程序的全域权限委托给我们。这可以通过组织机构的Google Apps管理员完成,请遵循标题为授权服务帐户的域名权限 p 的小节中的说明> https://developers.google.com/accounts/docs/OAuth2ServiceAccount
以下是在相同资源中给出的解释:
如果您已委派域服务帐户的域访问权限并且想要模拟用户帐户,请指定使用GoogleCredential工厂的setServiceAccountUser方法的用户帐户的电子邮件地址...
I receive a NullPointerException
with very little detail when I attempt to setServiceAccountUser(ACCOUNT_TO_IMPERSONATE)
on my GoogleCredential
, build a Google Tasks Service Object and subsequently try to send a request for a list of Tasks 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
Please note that the credential.getServiceAccountUser()
logs out the expected ACCOUNT_TO_IMPERSONATE
.
There was a similar issue reported in 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 an issue in older versions of Google Tasks but I have 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 tinkering with the code in the grails console in our web application I can see that executing credential.refreshToken()
causes the NPE when ServiceAccountUser
is set.
When I unset the ServiceAccountUser
, refreshToken()
is successful. Additionally, trying to retrieve a list of Google Tasks is succcessful for the Service account when the ServiceAccountUser
is not set.
Any help would be greatly appreciated.
I discovered the reason for the behaviour we were experiencing, but thanks for your suggestions, none-the-less, user1807337 & Srinivasan.
We need to get our application's users to delegate domain-wide authority to our application. This can be done by the organisataion's google apps admin, following the instructions in the sub-section entitled Delegating domain-wide authority to the service account
https://developers.google.com/accounts/docs/OAuth2ServiceAccount
Here is the explanation given in the same resource: "If you have delegated domain-wide access to the service account and you want to impersonate a user account, specify the email address of the user account with the setServiceAccountUser method of the GoogleCredential factory..."
这篇关于只有在为GoogleCredential对象(Grails / Java)设置了setServiceAccountUser()后,才需要NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!