2011-11-18 58 views
1

我正在使用Google數據協議客戶端庫對我的用戶進行身份驗證。這裏是我的代碼至今:java.lang.NullPointerException:無身份驗證標頭信息Google oauthentication

 GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters(); 

     oauthParameters.setOAuthConsumerKey(CONSUMER_KEY); 

     oauthParameters.setOAuthConsumerSecret(SECRET_KEy); 
     GoogleOAuthHelper oauthHelper = new GoogleOAuthHelper(new OAuthHmacSha1Signer()); 
     oauthParameters.setScope(SCOPE); 
     oauthParameters.setOAuthVerifier(oauth_verifier); 
     oauthParameters.setOAuthToken(oauth_token); 
     oauthParameters.setOAuthTokenSecret(oauth_token_secret) 
     String token = oauthHelper.getAccessToken(oauthParameters); 
     System.out.println("here"); 
     ContactsService client=new ContactsService ("myapp"); 
     client.setOAuthCredentials(oauthParameters, new hHmacSha1Signer()); 
     URL feedUrl = new URL("https://www.google.com/m8/feeds/contacts/default/full"); 
     ContactFeed resultFeed = client.getFeed(feedUrl, ContactFeed.class); 
     for (int i = 0; i < resultFeed.getEntries().size(); i++) { 
      ContactEntry entry = (ContactEntry) resultFeed.getEntries().get(i); 
      System.out.println("|\t" + (i + 1) + ": " 
        + entry.getTitle().getPlainText()); 
     } 

,但在這條線

ContactFeed resultFeed = client.getFeed(feedUrl, ContactFeed.class); 

我得到以下異常:

java.lang.NullPointerException: No authentication header information 
     com.google.gdata.util.AuthenticationException.initFromAuthHeader(AuthenticationException.java:96) 
     com.google.gdata.util.AuthenticationException.<init>(AuthenticationException.java:67) 
     com.google.gdata.client.http.HttpGDataRequest.handleErrorResponse(HttpGDataRequest.java:600) 

com.google.gdata.client.http.GoogleGDataRequest.handleErrorResponse(GoogleGDataRequest.java:563) 
     com.google.gdata.client.http.HttpGDataRequest.checkResponse(HttpGDataRequest.java:552) 
     com.google.gdata.client.http.HttpGDataRequest.execute(HttpGDataRequest.java:530) 
     com.google.gdata.client.http.GoogleGDataRequest.execute(GoogleGDataRequest.java:535) 
     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:662) 
     com.google.gdata.client.Service.query(Service.java:1237) 
     com.google.gdata.client.Service.query(Service.java:1178) 
     com.raisonne.oauth.CallBackAction.execute(CallBackAction.java:66) 
     sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
     sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    .... 

我已經申請什麼不同被提及論壇,但它沒有解決問題。我應該怎麼做才能解決這個問題?

+0

如果你真的卡住了,你的HTTP流的目標[Wireshark的(http://en.wikipedia.org/wiki/Wireshark)。 (至少,跟蹤中的'checkResponse()'方法名稱使我認爲它實際上發送了一些HTTP。) – sarnold

回答

0

設定範圍:

oauthParameters.setScope("http://www.google.com/m8/feeds/contacts/default/full"); 
+1

您正在將範圍與URL混淆。範圍是預定義的令牌(只看起來像一個URL),指示授予的訪問權限。對於聯繫人API,他們是用於讀寫訪問的「https://www.google.com/m8/feeds」和用於只讀訪問的「https://www.googleapis.com/auth/contacts.readonly」。無論您要發送的網址或Feed如何,這些都是一樣的。 –

0

我有同樣的問題,但很多的變化後,我的代碼工作。嘗試添加這兩條線:

oauthParameters.setOAuthType(OAuthType.TWO_LEGGED_OAUTH); 
client.setHeader("Authorization", "Bearer " + oauth_token);