单击“使用谷歌登录”按钮后卡住

时间:2021-05-09 09:32:50

标签: android firebase google-signin

所以我一直在尝试使用谷歌登录作为我的应用程序的登录选项。当我单击 sigin 按钮时,该应用程序会显示可用电子邮件列表,但只要我单击任何电子邮件,该应用程序就不会让我登录。新的 google-services.json 文件。我已经重新检查了我的代码,但我一定在这里错过了一些东西。请帮我找出我的错误

编辑:我也在 firebase 中启用了 Sigin wit google

这是我用于登录的代码:

public class User_login extends AppCompatActivity {


    private static final int RC_SIGN_IN = 123;
    private FirebaseAuth mAuth;
    private GoogleSignInClient mGoogleSignInClient;



    @Override
    protected void onStart() {
        super.onStart();

        FirebaseUser user = mAuth.getCurrentUser();
        if(user != null){
            Intent intent = new Intent(getApplicationContext(), home_user.class);
            startActivity(intent);
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_login);

        mAuth = FirebaseAuth.getInstance();

    
    // ________________SIGN IN WITH GOOGLE _____________//

        createRequest();

        findViewById(R.id.google_signIn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                signIn();
            }
        });
    }

    private void createRequest() {
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.default_web_client_id))
                .requestEmail()
                .build();

        mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
    }

    private void signIn() {
        Intent signInIntent = mGoogleSignInClient.getSignInIntent();
        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);
                //Log.d(TAG, "firebaseAuthWithGoogle:" + account.getId());
                firebaseAuthWithGoogle(account.getIdToken());
            } catch (ApiException e) {
                // Google Sign In failed, update UI appropriately
                Log.w("DEBUG", "Google sign in failed", e);
                Toast.makeText(User_login.this,"Google signin failed",Toast.LENGTH_LONG).show();
            }
        }
    }

    private void firebaseAuthWithGoogle(String idToken) {
        AuthCredential credential = GoogleAuthProvider.getCredential(idToken, null);
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            // Sign in success, update UI with the signed-in user's information
                            Log.d("DEBUG", "signInWithCredential:success");

                            FirebaseUser user = mAuth.getCurrentUser();
                            Intent intent = new Intent(getApplicationContext(), home_user.class);
                            startActivity(intent);

                        } else {
                            Toast.makeText(User_login.this, "sorry something went wrong, please retry",Toast.LENGTH_LONG).show();
                            // If sign in fails, display a message to the user.
                            //Log.w(TAG, "signInWithCredential:failure", task.getException());

                        }
                    }
                });
    }



}

编辑:这是我的 logcat 错误:

<块引用>

2021-05-09 15:14:57.861 1997-1997/com.covoid.app W/DEBUG:Google 登录失败 com.google.android.gms.common.api.ApiException: 10: 在 com.google.android.gms.common.internal.ApiExceptionUtil.fromStatus(com.google.android.gms:play-services-base@@17.1.0:4) 在 com.google.android.gms.auth.api.signin.GoogleSignIn.getSignedInAccountFromIntent(com.google.android.gms:play-services-auth@@18.1.0:9) 在 com.covoid.app.User_login.onActivityResult(User_login.java:137) 在 android.app.Activity.dispatchActivityResult(Activity.java:8304) 在 android.app.ActivityThread.deliverResults(ActivityThread.java:5136) 在 android.app.ActivityThread.handleSendResult(ActivityThread.java:5184) 在 android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51) 在 android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:140) 在 android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:100) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2222) 在 android.os.Handler.dispatchMessage(Handler.java:107) 在 android.os.Looper.loop(Looper.java:228) 在 android.app.ActivityThread.main(ActivityThread.java:7782) 在 java.lang.reflect.Method.invoke(Native Method) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:981)

1 个答案:

答案 0 :(得分:0)

在 Firebase 中检查您是否必须启用谷歌登录?

转到 firebase Authentication 和 inside sigin 方法启用谷歌登录

Check the image