Google Firebase登录无效

时间:2016-11-14 14:37:36

标签: android firebase-authentication

我制作了一个通过谷歌登录的脚本,然后从谷歌登录中获取用户名,并将其传递给下一个活动。

问题是,我想制作一个Facebook登录按钮,试图将它添加到我的脚本但它不起作用。现在我试图扭转我为使Facebook登录按钮工作所做的一切,但现在我的谷歌登录也失败了。

我现在已经测试并尝试了4个多小时了,我找不到答案。

到目前为止,我所做的是测试它出错的地方。 这个问题的答案是; onActivityResult 我的日志中有“firebaseauthwithgoogle:+ acct.getId”。 但我没有收到“mAuth.signInWithCredential(凭证).addOnCompleteListener”的回复

它不会执行“onComplete”方法。我真的不明白。 请帮助我重新开始工作:)

这是我的剧本:

    mAuth = FirebaseAuth.getInstance();
    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {
                // User is signed in
                Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
            } else {
                // User is signed out
                Log.d(TAG, "onAuthStateChanged:signed_out");
            }
            // ...
        }
    };

    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, this)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();

    /*mGoogleBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            signIn();

        }
    });*/
    findViewById(R.id.sign_in_button).setOnClickListener(this);

}


@Override
protected void onStart() {
    super.onStart();
    mAuth.addAuthStateListener(mAuthListener);


}

@Override
protected void onStop() {
    super.onStop();
    if(mAuthListener != null)
        mAuth.removeAuthStateListener(mAuthListener);
}

@Override
protected void onPause() {
    super.onPause();
    AppEventsLogger.deactivateApp(this);
}

@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()) {
            System.out.println("ALEX: Result is Succes");
            // Google Sign In was successful, authenticate with Firebase
            GoogleSignInAccount account = result.getSignInAccount();
            firebaseAuthWithGoogle(account);
        } else {
            System.out.println("ALEX: RESULT IS FAILED!!");
            // Google Sign In failed, update UI appropriately
            // ...
        }
    }
}

private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
    Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());

    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());
                    System.out.println("Current user : " + mAuth.getCurrentUser());
                    // 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()) {
                        Log.w(TAG, "signInWithCredential", task.getException());
                        Toast.makeText(MainActivity.this, "Authentication failed.",
                                Toast.LENGTH_SHORT).show();
                    }
                    // ...
                }


            });
}



private void logIn() {


    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    Intent myIntent = new Intent(MainActivity.this, LoggedInIndex.class);


    if (user != null) {
        myIntent.putExtra("uid", user.getDisplayName());
        myIntent.putExtra("email", user.getEmail());
        myIntent.putExtra("pic", user.getPhotoUrl().toString());
    } else {
        myIntent.putExtra("uid", "failed");
        myIntent.putExtra("email", "failed");
    }


    startActivity(myIntent);

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


}

public static void signOut(){
    FirebaseAuth.getInstance().signOut();
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    Log.d(TAG, "Connection failed.");
}



@Override
public void onClick(View view) {
    switch(view.getId()){
        case R.id.sign_in_button:
            signIn();

            break;
    }
}

1 个答案:

答案 0 :(得分:0)

我得到了它的工作,

这是造成麻烦的模拟器。 到目前为止,牛轧糖并不是我的成功。回到6.0