簡體   English   中英

Google登錄身份驗證不起作用

[英]Google Sign-in authentication not working

我使用了Stackoverflow,並嘗試了建議的答案,但是它們都沒有起作用。 我正在嘗試使用Firebease和Google驗證用戶,我的代碼工作正常。 但是在這種方法中,我總是使toast消息認證失敗:

private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);

    auth.signInWithCredential(credential)
            .addOnCompleteListener(LoginActivity.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();
                    }
                    // ...
                }
            });
}

我相信這與我的google-services.json和我的google API憑據有關。 我想知道是否有人可以給我一些明確的步驟,以解決此問題。 這就是我嘗試使登錄過程正常進行的工作。

  1. 將我的應用程序與firebase鏈接,並將JSON正確添加到項目中。
  2. 添加了所有構建gradle依賴項
  3. 允許Google從Firebase身份驗證標簽登錄
  4. 轉到google API憑證網站( 此處 ),並將其復制並存儲在我的項目內的strings.xml中
  5. 回到憑證頁面並為Android創建另一個Auth令牌,然后將其輸入終端以獲取ShA1密鑰:

keystore -list -vrt -alias androiddebugkey -keystore〜/ .android / debug。

密碼為android

然后,在創建android Auth令牌時輸入了此密鑰。

我在onCreate的登錄類中也包含了所有這些代碼

 GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.server_client_id))
            .requestEmail()
            .build();

    // Build a GoogleApiClient with access to the Google Sign-In API and the
    // options specified by gso.
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();

所有這些以及偵聽器和onstart和stop方法

private void signIn() {
    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
    startActivityForResult(signInIntent, RC_SIGN_IN);
}

@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()) {
            Toast.makeText(LoginActivity.this, "Passed google login", Toast.LENGTH_SHORT).show();
            // Google Sign In was successful, authenticate with Firebase
            GoogleSignInAccount account = result.getSignInAccount();

            firebaseAuthWithGoogle(account);
        }
    }
}

/*****************************************************************************************************/
/*get an ID token from the GoogleSignInAccount object, exchange it for a Firebase credential,
 *and authenticate with Firebase using the Firebase credential
 */
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);

    auth.signInWithCredential(credential)
            .addOnCompleteListener(LoginActivity.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();
                    }
                    // ...
                }
            });
}

我已經為此堅持了很長時間,非常感謝您的幫助。 我不清楚我的問題是什么,我只是猜測是這樣。 如果需要任何其他信息,請讓我知道,我將更新此帖子。

謝謝

這更多是建議的集合,而不是答案。

更新完成偵聽器以記錄失敗的異常:

                    if (!task.isSuccessful()) {
                        Log.w(TAG, "signInWithCredential", task.getException());
                        Toast.makeText(GoogleSignInActivity.this, "Authentication failed.",
                                Toast.LENGTH_SHORT).show();
                    }

運行時,查看logcat輸出以查看異常以及與身份驗證處理相關的其他日志消息。

我想知道您發布的這一步驟以及與您的項目的SHA1指紋相關的步驟的順序:

步驟4.前往Google API憑證網站...

我在多個項目(包括Auth QuickStart)中都進行了auth 認證 ,並且不記得需要使用API​​憑證網站。 SHA1密鑰應在Firebase控制台的項目的“設置”頁面中輸入。 下載google-service.json文件之前,應先這樣做,以使其包含在文件中。 通過查看以下部分的certificate_hash的值,可以查看所使用的文件是否具有正確的指紋:

  "oauth_client": [
    {
      "client_id": "<long ID string here>",
      "client_type": 1,
      "android_info": {
        "package_name": "com.google.firebase.quickstart.auth",
        "certificate_hash": "<your SHA1 fingerprint here>"
      }
    },

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM