隨着新的Firebase,他們已經改變了大量的代碼和做事的方式。
現在,當您設置Firebase項目時,他們會給您一個選項來輸入您的AuthId,您可以下載JSON並將其粘貼到您的應用程序中。
設置完ClientId後,您可以添加此代碼讓您的Google登錄重新恢復工作。
Button signInButton;
//Google Variables
private static final int RC_SIGN_IN = 9001;
private GoogleApiClient mGoogleApiClient;
//Firebase Variables
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthStateListener;
//Inside OnCreate();
//Google SignIn Methods
// [START config_signin]
// Configure Google Sign In
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
// [END config_signin]
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
mAuth = FirebaseAuth.getInstance();
mAuthStateListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
updateUI(user);
}
};
@Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthStateListener);
mGoogleApiClient.connect();
}
@Override
protected void onStop() {
super.onStop();
if(mAuthStateListener != null) {
mAuth.removeAuthStateListener(mAuthStateListener);
}
mGoogleApiClient.disconnect();
}
// [START onactivityresult]
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = result.getSignInAccount();
firebaseAuthWithGoogle(account);
} else {
// Google Sign In failed, update UI appropriately
// [START_EXCLUDE]
updateUI(null);
// [END_EXCLUDE]
}
}
}
// [END onactivityresult]
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Toast.makeText(LoginActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
}
});
}
// [END auth_with_google]
// [START signin]
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
// [END signin]
private void updateUI(FirebaseUser user) {
if (user != null) {
//write what you want to do after login here
}
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
// An unresolvable error has occurred and Google APIs (including Sign-In) will not
// be available.
Toast.makeText(this, "Google Play Services error.", Toast.LENGTH_SHORT).show();
}
public void GoogleSignIn(View v){
signIn();
}
@Override
protected void onRestart() {
super.onRestart();
mGoogleApiClient.connect();
}
這個工作完全沒在我的應用程序
謝謝你的答案,但這不完全是我所要求的。我想使用[Firebaseui](https://github.com/firebase/FirebaseUI-Android)庫,它應該可以簡化一些事情。 – CaptainNemo
試圖執行答案,但我收到相同的錯誤: 發生內部錯誤。[找不到服務器配置中的OAuth2客戶端ID。 ] – CaptainNemo
您是否可以嘗試在開發人員控制檯中重新配置OAuth2,然後在Firebase項目中使用新ID?我曾經遇到同樣的問題,事實證明我的OAuth客戶端ID已損壞。 –