java - gdata oauth2 授权 NullPointerException : No authentication header information

标签 java oauth-2.0 google-api gdata

我正在尝试在我的网络应用程序中使用 gdata+Oauth2 导出/导入 google 联系人。该应用程序有一个js客户端和java服务器端,通过REST API进行通信。 js端通过google获取以下数据进行授权

{  
   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 端的范围内 - 未设置。在 Google 的 Oauth Playground 上进行了尝试,获得了一个 token 并简单地对其进行了硬编码 - 它起作用了。将范围添加到 JS 端(任何 JS Oauth 库都支持它)后,我成功进行了身份验证。我建议遇到此类问题的每个人都尝试使用 Oauth Playground 上生成的 token 进行身份验证,这将有助于排除故障并找到问题。 谢谢

关于java - gdata oauth2 授权 NullPointerException : No authentication header information,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28161759/

相关文章:

java - 获取给定时区的当前时间 : android

java - 如何从 JavaFX2 的父 Controller 获取 Controller 的特定实例?

java - 在 Spring 中为不同数据源设置事务的正确方法?

c# - 如何在 RestSharp 中使用 OAuth2

python - 使用 Google 日历了解事件在哪几天进行?

c# - 谷歌云端硬盘。上传文件时刷新授权 token

jquery - Google map 未完全加载

java - 如何通过使用 scala 类扩展我的 java 类使其可写?

facebook-graph-api - Grails Spring Security如何在自定义身份验证中记住Me?

php - 使用 Laravel Passport 获取经过身份验证的用户并授予密码