【问题标题】:Android Google Oauth2 ContactServiceAndroid Google Oauth2 ContactService
【发布时间】:2013-06-28 03:51:30
【问题描述】:

我正在尝试在我的 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();

【问题讨论】:

  • 是我在 android 上使用 gdata jars 而不是 google_api-java-client jars 的问题吗?

标签: android oauth oauth-2.0 google-oauth


【解决方案1】:

我认为我遇到的问题是忘记在对 ContactService 的 GET 查询中包含某些项目,例如“nonce”和“timestamp”。我在http://androidwarzone.blogspot.com/2011/07/android-oauth-full-example-with-source.html 在线找到了一个可行的解决方案。希望这可以帮助将来的人。

【讨论】:

    【解决方案2】:

    在使用oauthParameters 的任何属性之前...检查if 条件是否为null

    尤其是在这句话之前:

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

    并检查 client 是否为 null

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

    【讨论】:

    • 感谢您的回答,但这对我不起作用。我认为如果其中任何一个为空,那么我会在 com.example.GmailOauthActivity$GmailEmailAddressTask 中收到错误,但我在 google api 类中收到错误。
    • 嘿@7bluephoenix,这曾经对我有用,但就在几天前它停止了工作,现在它抛出了这个错误 com.google.android.gms.auth.GoogleAuthException: Unknown。知道为什么吗?我的瞄准镜有问题吗? gist.github.com/lawloretienne/7351151
    猜你喜欢
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    • 2013-12-19
    • 1970-01-01
    • 1970-01-01
    • 2012-11-19
    • 2014-06-18
    • 1970-01-01
    相关资源
    最近更新 更多