我尝试使用Firebase和Google帐户验证我的应用。我按照官方教程进行操作,当我选择我的Google登录按钮时,它会显示我的帐户列表,然后选择其中一个并从onActivityResult方法获取该日志消息。
GoogleSignInOptions googleSignInOptions = new
GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken("here is my client id")
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.d(TAG, "onConnectionFailed: failed");
}
})
.addApi(Auth.GOOGLE_SIGN_IN_API, googleSignInOptions)
.build();
mGoogleSignIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG, "onClick:");
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
});
在onCreate方法之外:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
Log.d(TAG, "onActivityResult: requestCode = " + requestCode);
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
Log.d(TAG, "onActivityResult: successful");
GoogleSignInAccount account = result.getSignInAccount();
firebaseAuthWithGoogle(account);
} else {
Toast.makeText(getApplicationContext(), "Sign In Failed", Toast.LENGTH_LONG).show();
Log.d(TAG, "onActivityResult: failed");
}
}
}
private void firebaseAuthWithGoogle(GoogleSignInAccount account) {
Log.d(TAG, "firebaseAuthWithGoogle: " + account.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
mFirebaseAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Log.d(TAG, "onComplete: successful");
FirebaseUser user = mFirebaseAuth.getInstance().getCurrentUser();
Intent intent = new Intent(getApplicationContext(), HomeTabActivity.class);
startActivity(intent);
} else {
// If sign in fails, display a message to the user.
Log.d(TAG, "signInWithCredential:failure" + task.getException());
Toast.makeText(getApplicationContext(), "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
}
});
}
答案 0 :(得分:0)
试试这个
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken("here is my client id")
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
GLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivityForResult(
AuthUI.getInstance()
.createSignInIntentBuilder()
.setIsSmartLockEnabled(false)
.setProviders(Collections.singletonList(new AuthUI.IdpConfig.Builder(AuthUI.GOOGLE_PROVIDER).build()
))
.build(),
RC_SIGN_IN);
}
});
onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
IdpResponse response = IdpResponse.fromResultIntent(data);
if (resultCode == ResultCodes.OK) {
Intent in = new Intent(this, YourActivity.class);
startActivity(in);
finish();
return;
} else {
// Sign in failed
if (response == null) {
Log.e("Login","Login canceled by User");
return;
}
if (response.getErrorCode() == ErrorCodes.NO_NETWORK) {
Log.e("Login","No Internet Connection");
return;
}
if (response.getErrorCode() == ErrorCodes.UNKNOWN_ERROR) {
Log.e("Login","Unknown Error");
return;
}
}
Log.e("Login","Unknown sign in response");
}
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Toast.makeText(getApplicationContext(), "Connection Failed", Toast.LENGTH_SHORT).show();
}