gdata oauth2 authorization NullPointerException:没有身份验证标头信息

时间:2015-01-27 01:28:53

标签: java oauth-2.0 google-api gdata

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

{  
   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

但它对我不起作用。

你能指点我做错了什么吗? 任何帮助将不胜感激

由于

2 个答案:

答案 0 :(得分:2)

问题在于JS方面的范围 - 它没有设置。在谷歌的Oauth游乐场试玩,获得了一个令牌并简单地硬编码 - 它有效。将范围添加到JS端(任何JS Oauth库支持它)后,我成功进行了身份验证。我建议每个遇到此类问题的人尝试使用在Oauth操场上生成的令牌进行身份验证,这将有助于排除故障并找到问题。 感谢

答案 1 :(得分:0)

将此变通方法添加到ContactService init块

myService.getRequestFactory().setHeader("User-Agent", applicationName);
相关问题