【发布时间】:2015-03-25 13:16:19
【问题描述】:
我正在尝试在我的网络应用程序中使用 gdata+Oauth2 导出/导入谷歌联系人。该应用程序在 js 和 java 服务器端有一个客户端,通过 REST API 进行通信。 js端通过google执行auathorization得到以下数据
{
state:"38c4ebb6-b763-4e98-969c-16a86221ec71",
access_token:"ya29.BwEGCaDeWTzGqIwewwlmWreAMZdgNNexN1efOVGDcyY0f-gzXUot51F-Tzy5BX39CwGpbrL3JGjQ",
token_type:"Bearer",
expires_in:"3600"
}
我正在尝试使用 access_token 通过以下方式获取联系人
ContactsService myService = new ContactsService(APP_NAME);
myService.setHeader("Authorization", "Bearer " + accessToken);
return GoogleDataUtils.getContactList(getContactFeed(myService));
在哪里
private ContactFeed getContactFeed(ContactsService myService) throws ServiceException, IOException {
URL feedUrl = new URL(URL_FOR_FEED);
Query myQuery = new Query(feedUrl);
myQuery.setMaxResults(Integer.MAX_VALUE);
ContactFeed resultFeed = myService.getFeed(myQuery, ContactFeed.class);
return resultFeed;
}
但我得到的是
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)
我为 SpreadsheetService 找到了一个已解决的问题
gdata-java-client + oauth2 + access_token secret
但它对我不起作用。
你能指出我做错了什么吗? 任何帮助将不胜感激
谢谢
【问题讨论】:
-
为什么JS要进行认证?如果你有 Java 作为服务器端,你应该遵循Using OAuth2 for Web Apps 流程。无论如何,
accessToken在Java端的输出是什么? -
JS端正在执行oauth,因为它只是一个前端,所以我不可能在只提供REST api的服务器上进行oauth。据我所知,就安全令牌转移而言,这是一个有效的解决方案
标签: java oauth-2.0 google-api gdata