Java gdata oauth2授权NullPointerException:无身份验证标头信息
我正在尝试在我的web应用程序中使用gdata+Oauth2导出/导入google联系人。该应用程序在js和java服务器端有一个客户端,通过RESTAPI进行通信。js端通过google执行auathorization,获取以下数据Java gdata oauth2授权NullPointerException:无身份验证标头信息,java,oauth-2.0,google-api,gdata,Java,Oauth 2.0,Google Api,Gdata,我正在尝试在我的web应用程序中使用gdata+Oauth2导出/导入google联系人。该应用程序在js和java服务器端有一个客户端,通过RESTAPI进行通信。js端通过google执行auathorization,获取以下数据 { state:"38c4ebb6-b763-4e98-969c-16a86221ec71", access_token:"ya29.BwEGCaDeWTzGqIwewwlmWreAMZdgNNexN1efOVGDcyY0f-gzXUot51F-T
{
state:"38c4ebb6-b763-4e98-969c-16a86221ec71",
access_token:"ya29.BwEGCaDeWTzGqIwewwlmWreAMZdgNNexN1efOVGDcyY0f-gzXUot51F-Tzy5BX39CwGpbrL3JGjQ",
token_type:"Bearer",
expires_in:"3600"
}
我正在尝试使用access_令牌以以下方式获取联系人
ContactsService myService = new ContactsService(APP_NAME);
myService.setHeader("Authorization", "Bearer " + accessToken);
return GoogleDataUtils.getContactList(getContactFeed(myService));
在哪里
但我得到的是
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)
at com.google.gdata.client.http.GoogleGDataRequest.handleErrorResponse(GoogleGDataRequest.java:564)
at com.google.gdata.client.http.HttpGDataRequest.checkResponse(HttpGDataRequest.java:560)
at com.google.gdata.client.http.HttpGDataRequest.execute(HttpGDataRequest.java:538)
at com.google.gdata.client.http.GoogleGDataRequest.execute(GoogleGDataRequest.java:536)
at com.google.gdata.client.Service.getFeed(Service.java:1135)
at com.google.gdata.client.Service.getFeed(Service.java:1077)
at com.google.gdata.client.GoogleService.getFeed(GoogleService.java:676)
at com.google.gdata.client.Service.getFeed(Service.java:1034)
线程“main”java.lang.NullPointerException中的异常:无身份验证头信息
位于com.google.gdata.util.AuthenticationException.initFromAuthHeader(AuthenticationException.java:96)
位于com.google.gdata.util.AuthenticationException。(AuthenticationException.java:67)
位于com.google.gdata.client.http.HttpGDataRequest.handleErrorResponse(HttpGDataRequest.java:608)
位于com.google.gdata.client.http.GoogleGDataRequest.handleErrorResponse(GoogleGDataRequest.java:564)
位于com.google.gdata.client.http.HttpGDataRequest.checkResponse(HttpGDataRequest.java:560)
位于com.google.gdata.client.http.HttpGDataRequest.execute(HttpGDataRequest.java:538)
位于com.google.gdata.client.http.GoogleGDataRequest.execute(GoogleGDataRequest.java:536)
位于com.google.gdata.client.Service.getFeed(Service.java:1135)
位于com.google.gdata.client.Service.getFeed(Service.java:1077)
位于com.google.gdata.client.GoogleService.getFeed(GoogleService.java:676)
位于com.google.gdata.client.Service.getFeed(Service.java:1034)
我找到了电子表格服务的已解决问题
但这对我不起作用
你能告诉我我做错了什么吗?
任何帮助都将不胜感激
谢谢将此解决方法添加到ContactService init块
myService.getRequestFactory().setHeader("User-Agent", applicationName);
问题出在JS端的作用域中-没有设置。谷歌在Oauth操场上试用过,得到了一个令牌,并简单地对其进行了硬编码——它成功了。在将作用域添加到JS端(任何JSOAuth库都支持它)之后,我成功地进行了身份验证。我建议每个面临此类问题的人尝试使用Oauth游乐场上生成的令牌进行身份验证,这将有助于解决问题并找到问题。
谢谢为什么JS要执行身份验证?如果您使用Java作为服务器端,那么应该遵循流程。无论如何,
accessToken
在Java端的输出是什么?JS端正在执行oauth,因为它只是一个前端,所以我不可能在只提供RESTAPI的服务器上执行oauth。据我所知,这是一个有效的解决方案,因为令牌传输是安全的
myService.getRequestFactory().setHeader("User-Agent", applicationName);