android應用集成google登錄
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestId()
.requestProfile()
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener(){
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
})
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
之後就是登錄發起: Intent intent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); startActivityForResult(intent, RC_SIGN_IN); //RC_SIGN_IN是requestcode 在onActivityResult中攔獲取登錄的回調: GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); handleSignInResult(result); 在登錄回調中可以獲取用戶的google賬號的id,name,photourl等信息,至此整個登錄過程完成。 /**
* Google plus 登陸回調
*/
private void handleSignInResult(GoogleSignInResult result) {
Log.i(TAG, "handleSignInResult----" + result.isSuccess());
if (result.isSuccess()) {
GoogleSignInAccount account = result.getSignInAccount();
Log.i(TAG, "id--------" + account.getId() + "----name----" + account.getDisplayName() + "---photo--" + account.getPhotoUrl());
}
}
同時歡迎大家關註我的簡書博客,地址是:
https:www.jianshu.com/u/da06e00edefa
歡迎大家掃描關註我的微信公眾號,我會定期發布一些博客,分享一些知識點
android應用集成google登錄