在我的Android应用程序中,我请求google plus令牌,我发送给服务器。我希望能够在服务器中获取用户信息,包括他的电子邮件地址。
这是一个有效的电话,但没有给我发电子邮件:
GoogleAuthUtil.getToken(activity, Plus.AccountApi.getAccountName(googleApiClient), "oauth2:" + Scopes.PLUS_ME + " " + Scopes.PLUS_LOGIN + " " + Scopes.PROFILE);
有了它,我只收到这些信息:
{
"id": "11213535252359196332836",
"name": "Name Surname",
"given_name": "Name",
"family_name": "Surname",
"link": "https://plus.google.com/+laskdjfksdjf",
"picture": "https://asdfsdafasdfasdfasdfasdfasdfsdaf7c/photo.jpg",
"gender": "male",
"locale": "en"
}
我发现我需要请求userinfo.email权限,所以我修改了我的调用:
GoogleAuthUtil.getToken(activity, Plus.AccountApi.getAccountName(googleApiClient), "oauth2:" + Scopes.PLUS_ME + " " + Scopes.PLUS_LOGIN + " " + Scopes.PROFILE + " https://www.googleapis.com/auth/userinfo.email");
问题是,现在getToken()调用会返回错误:
09-16 18:53:57.311: W/System.err(26467): com.google.android.gms.auth.UserRecoverableAuthException: NeedPermission
09-16 18:53:57.316: W/System.err(26467): at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
09-16 18:53:57.316: W/System.err(26467): at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
09-16 18:53:57.316: W/System.err(26467): at lt.sm.discountcity.GPlus.getServerOnlineToken(GPlus.java:237)
09-16 18:53:57.316: W/System.err(26467): at lt.sm.discountcity.fragments.LoginFragment$5$1.runOnSeparateThread(LoginFragment.java:173)
09-16 18:53:57.316: W/System.err(26467): at lt.smtools.tasks.TaskManager$2.run(TaskManager.java:139)
09-16 18:53:57.316: W/System.err(26467): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
09-16 18:53:57.316: W/System.err(26467): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
09-16 18:53:57.316: W/System.err(26467): at java.lang.Thread.run(Thread.java:841)
任何想法我做错了什么以及如何解决这个问题?
答案 0 :(得分:0)
http://developer.android.com/reference/com/google/android/gms/auth/GoogleAuthUtil.html
void getAndUseAuthTokenBlocking() {
try {
// Retrieve a token for the given account and scope. It will always return either
// a non-empty String or throw an exception.
final String token = GoogleAuthUtil.getToken(Context, String, String)(context, email, scope);
// Do work with token.
...
if (server indicates token is invalid) {
// invalidate the token that we found is bad so that GoogleAuthUtil won't
// return it next time (it may have cached it)
GoogleAuthUtil.invalidateToken(Context, String)(context, token);
// consider retrying getAndUseTokenBlocking() once more
return;
}
return;
} catch (GooglePlayServicesAvailabilityException playEx) {
Dialog alert = GooglePlayServicesUtil.getErrorDialog(
playEx.getConnectionStatusCode(),
this,
MY_ACTIVITYS_AUTH_REQUEST_CODE);
...
} catch (UserRecoverableAuthException userAuthEx) {
// Start the user recoverable action using the intent returned by
// getIntent()
myActivity.startActivityForResult(
userAuthEx.getIntent(),
MY_ACTIVITYS_AUTH_REQUEST_CODE);
return;
} catch (IOException transientEx) {
// network or server error, the call is expected to succeed if you try again later.
// Don't attempt to call again immediately - the request is likely to
// fail, you'll hit quotas or back-off.
...
return;
} catch (GoogleAuthException authEx) {
// Failure. The call is not expected to ever succeed so it should not be
// retried.
...
return;
}
}