我需要获取访问令牌并将其发送到服务器。有了这个访问令牌,服务器应该可以获得所有用户的详细信息,如姓名、个人资料图片和电子邮件。
我可以使用Scopes.PLUS_LOGIN和Scopes.PRUS_ME获取访问令牌,但使用该访问令牌,服务器无法获取用户电子邮件。
这是我的代码:
@Override
public void onConnected(Bundle arg0) {
mSignInClicked = false;
AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
String token = null;
String scope = "oauth2:" + Scopes.PLUS_LOGIN + " " + Scopes.PLUS_ME;
try {
token = GoogleAuthUtil.getToken(
getApplicationContext(),
Plus.AccountApi.getAccountName(mGoogleApiClient),
scope);
appUser.setToken(token);
} catch (IOException transientEx) {
// Network or server error, try later
Log.e(TAG, transientEx.toString());
} catch (UserRecoverableAuthException e) {
// Recover (with e.getIntent())
} catch (GoogleAuthException authEx) {
// The call is not ever expected to succeed
// assuming you have already verified that
// Google Play services is installed.
Log.e(TAG, authEx.toString());
}
return token;
}
@Override
protected void onPostExecute(String token) {
Log.i(TAG, "Access token retrieved:" + appUser.getToken());
// Get user's information
}
};
}
有人知道如何解决这个问题吗?
您缺少作用域
https://www.googleapis.com/auth/userinfo.email
我测试了其他作用域,只有那个作用域会返回用户的电子邮件。你可以在这里测试不同的范围和它们返回的内容:人:得到。
注意:我不是一个android程序员,你可能会更好地找到如何使用android请求该范围。我在找,但一直找不到。
看起来范围可能只是电子邮件https://developers.google.com/+/api/oauth#电子邮件