[英]Android Firebase Authentication for Google Signin fails
我從上到下跟蹤了firebase文檔 ,並為我的應用程序實現了一個簡單的谷歌登錄選項。 但是,當我嘗試登錄時,該流程在選擇Google帳戶后停止,並導致onActivityResult方法內部出錯:
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 {
//THE CODE BREAKS HERE
// Google Sign In failed, update UI appropriately
}
}
代碼在if (result.isSuccess())
條件下中斷。 進一步詳細說明,這是我在完成文檔后實現的完整代碼:
的onCreate:
public class TestLogin extends AppCompatActivity {
private SignInButton mGoogleBtn;
private static int RC_SIGN_IN = 226;
private GoogleApiClient mGoogleApiClient;
private String TAG = "Checkmate";
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_login);
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if(firebaseAuth.getCurrentUser() != null){
//success
}
}
};
mGoogleBtn = (SignInButton) findViewById(R.id.googleBtn);
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken("MY_CLIENT_ID-MY_CLIENT_ID.apps.googleusercontent.com")
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
.enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
//Failed
}
})
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
mGoogleBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
statusMessage.setText("Loading..");
signIn();
}
});
}
signIn方法:
單擊Google Signin按鈕時會調用SignIn方法:
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
onActivityResult:
單擊按鈕后收到結果時,將調用onActivityResult。 這是發生錯誤的地方:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
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 {
//THE CODE BREAKS HERE
// Google Sign In failed, update UI appropriately
}
}
}
處理Google的Firebase登錄代碼:
永遠不會調用此方法,因為代碼在onActivityResult條件下中斷。
private void firebaseAuthWithGoogle(final 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(TestLogin.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
清單 :
是的,我將google_services.json文件添加到/ app目錄
是的,我在Google憑據頁面中創建了一個新的OAuth客戶端密鑰
這是我的傻瓜:
dependencies {
compile 'com.google.android.gms:play-services:9.6.1'
compile 'com.google.firebase:firebase-core:9.6.1'
compile 'com.google.firebase:firebase-database:9.6.1'
compile 'com.google.firebase:firebase-auth:9.6.1'
compile 'com.google.firebase:firebase-storage:9.6.1'
}
apply plugin: 'com.google.gms.google-services'
和..
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.3'
classpath 'com.google.gms:google-services:3.0.0'
}
}
在我之前使用Firebase FriendlyChat的測試應用中,我在登錄時遇到了類似的失敗,但是我已經通過Google API控制台( https://console.developers.google.com/apis/credentials?project= )解決了這個問題,然后下載了“client_secret.json”(在我的情況下,我需要為您的Android客戶端重命名下載的文件名“client_secret_xxxxx.json”),將此文件放在AndroidStudio項目的“/ src / main / resources”下(創建“resources”文件夾)如果不存在,那么嘗試再次編譯並運行您的Android應用程序。
摘要:您需要2個json文件:Firebase Console中的“google-services.json”和Google API控制台中的“client_secret.json”。
希望這能解決你的問題。
檢查是否在Google API控制台中啟用了“Identity Toolkit API”
轉到firebase控制台並首先啟用登錄提供程序。 https://console.firebase.google.com/u/2/project/ipay-71f01/authentication/providers
我有同樣的問題,但這是我的解決方案。 謝謝
確保您已輸入簽名密鑰庫的SHA1。 以下是https://developers.google.com/android/guides/client-auth指南
您必須找到兩個目錄,1。已安裝JDK的目錄2. .android
如果您使用的是Windows,'。android'文件夾可能放在'C:\\ Users \\ username \\'中,而JDK目錄可能放在'C:\\ Users \\ Program files \\ java \\'中。
在我的情況下,我的操作系統是Windows,所以首先我正在尋找我的jdk文件夾。
在'jdk_folder \\ bin \\'中你會找到'keytool.exe'並且我使用了這個命令
'。\\ keytool -exportcert -list -v -alias androiddebugkey -keystore C:\\ Users \\ sgc109 \\ .android \\ debug.keystore'。
然后,復制SHA1密鑰並粘貼到firebase控制台(設置 - 一般 - 我的應用程序 - SHA證書指紋(?)'(我住在韓國,所以菜單的名稱可能與我不同),它的工作原理:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.