2013-07-01 73 views
2

我想在我的Android應用程序中爲谷歌服務實現Oauth2。我需要獲取用戶的電子郵件地址。目前我能獲得訪問令牌和令牌密鑰,但是當我嘗試使用這些來自的ContactService獲取用戶的電子郵件地址,我得到以下錯誤:Android谷歌Oauth2聯繫服務

Caused by: java.lang.NullPointerException 
    at com.google.gdata.client.authn.oauth.OAuthUtil.normalizeParameters(OAuthUtil.java:163) 
    at com.google.gdata.client.authn.oauth.OAuthUtil.getSignatureBaseString(OAuthUtil.java:81) 
    at com.google.gdata.client.authn.oauth.TwoLeggedOAuthHelper.addCommonRequestParameters(TwoLeggedOAuthHelper.java:79) 
    at com.google.gdata.client.authn.oauth.TwoLeggedOAuthHelper.addParametersAndRetrieveHeader(TwoLeggedOAuthHelper.java:121) 
    at com.google.gdata.client.authn.oauth.TwoLeggedOAuthHelper.getAuthorizationHeader(TwoLeggedOAuthHelper.java:112) 
    at com.google.gdata.client.GoogleAuthTokenFactory$OAuthToken.getAuthorizationHeader(GoogleAuthTokenFactory.java:204) 
    at com.google.gdata.client.http.HttpGDataRequest.<init>(HttpGDataRequest.java:331) 
    at com.google.gdata.client.http.GoogleGDataRequest.<init>(GoogleGDataRequest.java:456) 
    at com.google.gdata.client.http.GoogleGDataRequest$Factory.createRequest(GoogleGDataRequest.java:93) 
    at com.google.gdata.client.http.HttpGDataRequest$Factory.getRequest(HttpGDataRequest.java:165) 
    at com.google.gdata.client.Service.createRequest(Service.java:760) 
    at com.google.gdata.client.GoogleService.createRequest(GoogleService.java:525) 
    at com.google.gdata.client.Service.createFeedRequest(Service.java:1156) 
    at com.google.gdata.client.Service.getFeed(Service.java:997) 
    at com.google.gdata.client.GoogleService.getFeed(GoogleService.java:631) 
    at com.google.gdata.client.Service.getFeed(Service.java:1017) 
    at com.example.GmailOauthActivity$GmailEmailAddressTask.doInBackground(GmailOauthActivity.java:239) 
    at com.example.GmailOauthActivity$GmailEmailAddressTask.doInBackground(GmailOauthActivity.java:220) 
    at android.os.AsyncTask$2.call(AsyncTask.java:287) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:234) 
    ... 4 more 

這是我獲得的電子郵件地址碼。如果有一個更簡單的方法,那麼這也會很棒。謝謝 :)。

GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters(); 
      oauthParameters.setOAuthConsumerKey(CONSUMER_KEY); 
      oauthParameters.setOAuthConsumerSecret(consumer.getTokenSecret()); 
      oauthParameters.setScope(SCOPE); 
      oauthParameters.setOAuthVerifier(verifier); 
      oauthParameters.setOAuthToken(consumer.getToken()); 
      oauthParameters.setOAuthTokenSecret(consumer.getTokenSecret()); 
      ContactsService client=new ContactsService ("Service"); 
      client.setOAuthCredentials(oauthParameters, new OAuthHmacSha1Signer()); 
      oauthParameters.setOAuthType(OAuthParameters.OAuthType.TWO_LEGGED_OAUTH); 
      client.setHeader("Authorization", "Bearer " + consumer.getToken()); 
      URL feedUrl = new URL("https://www.google.com/m8/feeds/contacts/default/full"); 
      com.google.gdata.client.Query myQuery = new com.google.gdata.client.Query(feedUrl); 
      myQuery.setStartIndex(1); 
      myQuery.setMaxResults(1); 
      ContactFeed resultFeed = client.getFeed(feedUrl, ContactFeed.class); 
      return resultFeed.getAuthors().get(0).getEmail(); 
+0

是我在android上使用gdata jar而不是google_api-java-client jars的問題? –

回答

0

你之前使用任何oauthParameters屬性...檢查在if條件,如果它是null

特別是此語句前:

client.setOAuthCredentials(oauthParameters, new OAuthHmacSha1Signer()); 

,並檢查是否clientnull此:

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

感謝您的回答,但這對我無效。我認爲,如果其中任何一個爲空,那麼我會在com.example.GmailOauthActivity $ GmailEmailAddressTask中發生錯誤,但我在google api類中收到錯誤。 –

+0

嘿@ 7bluephoenix,這用於爲我工作,但只有幾天前它停止工作,現在它引發此錯誤com.google.android.gms.auth.GoogleAuthException:未知。任何想法爲什麼?我的示波器有問題嗎? https://gist.github.com/lawloretienne/7351151 – toobsco42