我希望我的用户有两种类型的登录选项 - “使用电子邮件登录”和“使用Google登录”。我的申请表我正在使用firebase-core:11.8.0
,firebase-auth:11.8.0'
和firebase-ui-auth:3.2.1'
。
当我在大多数物理和仿真器设备上运行它时,它运行得非常好。但是,在某些设备上(主要使用API 27),"Sign in with Google"
会失败。日志说 -
E/CredentialSignInHandler: Unexpected exception when signing in with credential google.com unsuccessful. Visit https://console.firebase.google.com to enable it.
com.google.firebase.FirebaseException: An internal error has occurred. [ Invalid id_token in IdP response:eyJhbG....., error: Id_token failed validation. ]
at com.google.android.gms.internal.zzdxm.zzao(Unknown Source:133)
at com.google.android.gms.internal.zzdwn.zza(Unknown Source:43)
at com.google.android.gms.internal.zzdxx.zzap(Unknown Source:11)
at com.google.android.gms.internal.zzdya.onFailure(Unknown Source:35)
at com.google.android.gms.internal.zzdxo.onTransact(Unknown Source:79)
at android.os.Binder.execTransact(Binder.java:697)
我可以确认在控制台中启用了两种登录方法,并且更新的google-services.json
位于app
目录中。
我在这里粘贴我的代码,万一有帮助。
package com.pustack.android.pustack;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import com.firebase.ui.auth.AuthUI;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import java.util.Arrays;
public class LoginActivity extends AppCompatActivity {
// Authentication
private FirebaseAuth mFirebaseAuth;
private FirebaseAuth.AuthStateListener mAuthStateListener;
public static final int RC_SIGN_IN = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Initialize Firebase Authentication
mFirebaseAuth = FirebaseAuth.getInstance();
mAuthStateListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser currentUser = mFirebaseAuth.getCurrentUser();
updateUI(currentUser);
}
};
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
if (resultCode == RESULT_OK) {
Toast.makeText(this, "Signed in!", Toast.LENGTH_SHORT).show();
} else if (resultCode == RESULT_CANCELED){
finish();
}
}
}
private void updateUI(FirebaseUser user) {
if (user != null) {
final Intent intent = new Intent(getApplicationContext(), MainActivity.class);
new CountDownTimer(1000,1000){
@Override
public void onTick(long millisUntilFinished){}
@Override
public void onFinish(){
startActivity(intent);
finish();
}
}.start();
} else {
startActivityForResult(
AuthUI.getInstance()
.createSignInIntentBuilder()
.setIsSmartLockEnabled(true)
.setLogo(R.mipmap.pustack_full_logo_vertical_white)
.setTheme(R.style.LoginTheme)
.setAvailableProviders(Arrays.asList(
new AuthUI.IdpConfig.EmailBuilder().build(),
new AuthUI.IdpConfig.GoogleBuilder().build()))
.build(),
RC_SIGN_IN);
}
}
@Override
protected void onPause() {
super.onPause();
if(mAuthStateListener != null)
mFirebaseAuth.removeAuthStateListener(mAuthStateListener);
}
@Override
protected void onResume() {
super.onResume();
mFirebaseAuth.addAuthStateListener(mAuthStateListener);
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
}
我正在努力解决这个问题一段时间,联系了firebase支持,他们确认一切都很好。