簡體   English   中英

Android Google Oauth2 ContactService

[英]Android Google Oauth2 ContactService

我正在嘗試在我的Android應用中為Google服務實現Oauth2。 我需要獲取用戶的電子郵件地址。 當前,我可以獲取訪問令牌和令牌密鑰,但是當我嘗試使用這些令牌和令牌密鑰從contactservice獲取用戶的電子郵件地址時,出現以下錯誤:

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();

我認為我遇到的問題是忘記在對ContactService的GET查詢中包括“ nonce”和“ timestamp”之類的某些項目。 我在http://androidwarzone.blogspot.com/2011/07/android-oauth-full-example-with-source.html上找到了一個可行的解決方案。 希望這可以在將來對某人有所幫助。

在使用oauthParameters任何屬性oauthParameters ,請檢查if條件是否為null

特別是在此聲明之前:

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

並檢查client是否為此null

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM