我有一个有效的Android应用程序(也许更好的说HAD)。
该应用正在使用一些第三方库,包括:Google地图,Firebase,firebaseui。 转换应用程序以使用新的firebase后,我偶然发现了两个主要问题:
答案 0 :(得分:6)
出现以下错误:
com.google.firebase.FirebaseException:发生了内部错误。 [找不到服务器配置中的OAuth2客户端ID。 ]
您必须先在凭据类别(https://console.developers.google.com/apis/credentials)中转到 Google开发者控制台,然后找到名为"的自动生成的OAuth 2.0客户端ID。网络客户端(由Google服务自动创建)",保留与此客户端ID相关的数据。
转到" Auth"中的 Firebase控制台类别,然后转到"连接模式"选项卡,打开Google身份验证设置,最后展开Web SDK设置。现在粘贴以前找到的OAuth客户端ID及其密钥并保存设置。
更改的设置是关于Web设置,但它似乎由Firebase安卓组件使用。
答案 1 :(得分:1)
借助New 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();
}
此工作完全适用于在我的应用中使用Firebase进行Google身份验证