我遇到了两种创建Google登录意图的方法:
1
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
private void handleSignInResult(GoogleSignInResult result) {
Log.d(TAG, "handleSignInResult: " + result.isSuccess());
if (result.isSuccess()) {
// Signed in successfully, show authenticated UI.
GoogleSignInAccount acct = result.getSignInAccount();
String googleAccessToken = acct.getServerAuthCode();
Uri thumbnailUrl = acct.getPhotoUrl();
String firstName = acct.getGivenName();
String lastName = acct.getFamilyName();
...
}
}
2
private void signIn() {
Intent intent = AccountPicker.newChooseAccountIntent(null, null, new String[]{"com.google"},
false, null, null, null, null);
startActivityForResult(intent, RC_SIGN_IN);
}
@Override
protected void onActivityResult(final int requestCode, final int resultCode,
final Intent data) {
if (requestCode == RC_SIGN_IN && resultCode == RESULT_OK) {
String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
Log.d(TAG, "onActivityResult: " + accountName);
String mEmail = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
String mType = data.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE);
Log.d(TAG, "onActivityResult: " + mType);
}
}
使用第一种方式,我可以获得用户的帐户图片:Uri thumbnailUrl = acct.getPhotoUrl();
使用第二种方式,我可以获取用户的帐户类型:String mType = data.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE);
我需要帐户类型才能获取访问令牌:
Account account = new Account(mEmail, mType);
token = GoogleAuthUtil.getToken(SignInActivity.this, account, mScope);
我一直在为mType(“com.google”)传递一个硬编码变量,该变量有效,但感觉不合适。在使用意图的Auth.GoogleSignInApi.getSignInIntent版本时,有没有办法获取帐户类型?
此外,有没有办法在使用AccountPicker.newChooseAccountIntent版本的意图时获取帐户图片?