代码之家  ›  专栏  ›  技术社区  ›  user9245787

google登录中的AuthResult任务在任务中返回false。isSuccess语句

  •  0
  • user9245787  · 技术社区  · 8 年前

    我已经使用Firebase在我的应用程序中实现了Google登录。 用户登录后,MainActivity应启动。 但我的firebaseAuthWithGoogle()函数中的这条语句返回false为:

      if (task.isSuccessful()) {  
              //Start The activity from here
              }
    

    这是我的登录名。java导致了以下情况:

    public class login extends AppCompatActivity {
    
    private SignInButton signInButton;
    private TextView textView;
    private static final int RC_SIGN_IN = 1;
    private GoogleApiClient mGoogleApiClient;
    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener mAuthListener;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
    
        mAuth = FirebaseAuth.getInstance();
        mAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                if (firebaseAuth.getCurrentUser() != null) {
                    Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_LONG).show();
                    startActivity(new Intent(login.this, MainActivity.class));
                }
            }
        };
    
        signInButton = findViewById(R.id.SignIn);
        textView = findViewById(R.id.negativetext);
        textView.setText(getString(R.string.negativetext));
    
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(), MainActivity.class));
            }
        });
    
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.default_web_client_id))
                .requestEmail()
                .build();
    
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
                    @Override
                    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
                        // Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
                    }
                })
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();
    
        signInButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                signIn();
            }
        });
    
    }
    
    
    @Override
    protected void onStart() {
        super.onStart();
        mAuth.addAuthStateListener(mAuthListener);
    }
    
    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) {
            Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
            try {
                // Google Sign In was successful, authenticate with Firebase
                GoogleSignInAccount account = task.getResult(ApiException.class);
                //Toast.makeText(getApplicationContext(), ""+account, Toast.LENGTH_LONG).show();
                firebaseAuthWithGoogle(account);
            } catch (ApiException e) {
                // Google Sign In failed, update UI appropriately
                Log.w("Signing in: ", "Google sign in failed", e);
                // ...
            }
        }
    }
    
    private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
        AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
        Toast.makeText(getApplicationContext(), "" + credential, Toast.LENGTH_LONG).show();
        //mAuth = FirebaseAuth.getInstance();
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        Toast.makeText(getApplicationContext(), "" + task.isSuccessful(), Toast.LENGTH_LONG).show();
                        if (task.isSuccessful()) {
                            // Sign in success, update UI with the signed-in user's information
                            //Log.d(TAG, "signInWithCredential:success");
                            //FirebaseUser user = mAuth.getCurrentUser();
                            //Log.i("Test:", " Compiled ?");
                            //startActivity(new Intent(login.this, MainActivity.class));
                            Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_LONG).show();
                            startActivity(new Intent(login.this, MainActivity.class));
                            //updateUI(user);
                        } else {
                            Toast.makeText(getApplicationContext(), "Not Success", Toast.LENGTH_LONG).show();
    
    
                            //Log.w(TAG, "signInWithCredential:failure", task.getException());
                            //Snackbar.make(findViewById(R.id.main_layout), "Authentication Failed.", Snackbar.LENGTH_SHORT).show();
                            //updateUI(null);
                        }
    
                        // ...
                    }
                });
    
        // Toast.makeText(getApplicationContext(), ""+acct, Toast.LENGTH_LONG).show();
    
    }
    

    }

    有人能建议我这里做错了什么吗? 我检查了这些陈述,所以我对其中的一些行进行了评论。 如有任何相关建议,将不胜感激:)

    1 回复  |  直到 8 年前
        1
  •  0
  •   Alex Mamo    8 年前

    这是因为您两次使用以下代码行:

    startActivity(new Intent(login.this, MainActivity.class));
    

    在您的 mAuthListener 在你的 firebaseAuthWithGoogle() 方法您需要将用户重定向到 MainActivity 仅当 firebaseAuth.getCurrentUser() != null ,这意味着用户已通过身份验证。我已经在我的 tutorials 所有你需要遵循的步骤,以实现你想要的东西。

    推荐文章