[英]Android Google Oauth2 ContactService
I am trying to implement Oauth2 for google services in my Android app. 我正在尝试在我的Android应用中为Google服务实现Oauth2。 I need to obtain a user's email address.
我需要获取用户的电子邮件地址。 Currently I can obtain the access token and token secret, but when I try to use these to obtain the user's email address from the contactservice, I get the following error:
当前,我可以获取访问令牌和令牌密钥,但是当我尝试使用这些令牌和令牌密钥从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
Here is my code for obtaining the email address. 这是我获取电子邮件地址的代码。 If there is a simpler method, then that would be great too.
如果有一个更简单的方法,那也将很棒。 Thanks :).
谢谢 :)。
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();
I believe that the issue I was having was with forgetting to include certain items like "nonce" and "timestamp" in my GET query to the ContactService. 我认为我遇到的问题是忘记在对ContactService的GET查询中包括“ nonce”和“ timestamp”之类的某些项目。 I found a working solution online at http://androidwarzone.blogspot.com/2011/07/android-oauth-full-example-with-source.html .
我在http://androidwarzone.blogspot.com/2011/07/android-oauth-full-example-with-source.html上找到了一个可行的解决方案。 Hope this can help someone in the future.
希望这可以在将来对某人有所帮助。
Before you using any of the properties of oauthParameters
... check in an if
condition if it is null
. 在使用
oauthParameters
任何属性oauthParameters
,请检查if
条件是否为null
。
Especially before this statement: 特别是在此声明之前:
client.setOAuthCredentials(oauthParameters, new OAuthHmacSha1Signer());
and check if client
is null
for this: 并检查
client
是否为此null
:
ContactFeed resultFeed = client.getFeed(feedUrl, ContactFeed.class);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.