2015-01-27 79 views
1

我想在我的web應用程序中使用gdata + Oauth2導出/導入谷歌聯繫人。該應用程序在js和java服務器端有一個客戶端,通過REST API進行通信。 JS的側通過谷歌進行auathorization得到以下數據gdata oauth2授權NullPointerException:沒有驗證標題信息

{ 
    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

但它沒有爲我工作。

請你指點我做錯了什麼? 任何幫助將不勝感激

感謝

+0

爲什麼JS正在執行身份驗證?如果您將Java作爲服務器端,則應遵循[使用OAuth2 for Web Apps](https://developers.google.com/accounts/docs/OAuth2WebServer)流程。無論如何,Java端的'accessToken'輸出是什麼? – 2015-01-27 10:13:22

+0

JS端執行oauth,因爲它只是一個前端,所以我不可能在服務器上只提供REST API的oauth。據我所知,只要令牌傳輸得到保證,這是一個有效的解決方案 – lopushen 2015-02-03 23:44:33

回答

2

的問題是在範圍ONN JS的一面 - 它沒有設置。谷歌試用Oauth遊樂場,得到了一個令牌,並簡單地對其進行了硬編碼 - 它工作。將範圍添加到JS端(任何JS Oauth庫支持它)後,我都成功進行了身份驗證。我建議所有面臨此類問題的人嘗試使用Oauth操場上生成的令牌進行身份驗證,這有助於排查問題並找出問題。 謝謝

0

此解決方法添加到您的ContactService初始化模塊

myService.getRequestFactory().setHeader("User-Agent", applicationName);