从firebase删除数据后无法使用google和facebook登录

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从firebase删除数据后无法使用google和facebook登录相关的知识,希望对你有一定的参考价值。

删除所有数据库并从firebase数据库验证用户后。当我试图再次使用它时,它显示下面的错误。

08-10 15:56:28.747 7377-7377/io.funswitch.gymmon D/Login Activity: signInWithCredential:failure
    com.google.firebase.auth.FirebaseAuthInvalidUserException: This user's credential isn't valid for this project. This can happen if the user's token has been tampered with, or if the user isn't for the project associated with this API key.
        at com.google.firebase.auth.api.internal.zzce.zzb(Unknown Source:213)
        at com.google.firebase.auth.api.internal.zzbb.zza(Unknown Source:42)
        at com.google.firebase.auth.api.internal.zzcy.zzc(Unknown Source:11)
        at com.google.firebase.auth.api.internal.zzdb.onFailure(Unknown Source:35)
        at com.google.firebase.auth.api.internal.zzci.dispatchTransaction(Unknown Source:83)
        at com.google.android.gms.internal.firebase_auth.zzb.onTransact(Unknown Source:22)
        at android.os.Binder.execTransact(Binder.java:692)

以下是googleSignIn和facebook登录的代码

    public class LoginActivity extends AppCompatActivity {
    LoginButton loginButton;
    public CallbackManager mCallbackManager;
    FirebaseAuth mAuth;
    GoogleSignInClient mGoogleSignInClient;
    GoogleApiClient mGoogleApiClient;
    private final static String TAG = "Login Activity";
    private static final int RC_SIGN_IN = 1000;
    GoogleSignInButton googleSignInButton;
    DatabaseReference databaseReference;
    ProgressDialog progressDialog;
    TextView tc, pp;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(this);
        GymMon.getFireBaseAnalyticsObj().setCurrentScreen(LoginActivity.this, "LoginActivity", "OnCreate");
        setContentView(R.layout.activity_login);
        progressDialog = new ProgressDialog(this);
        loginButton = findViewById(R.id.login_fb);
        googleSignInButton = findViewById(R.id.sign_in_button);
        mCallbackManager = CallbackManager.Factory.create();
        databaseReference = FirebaseDatabase.getInstance().getReference();
        mAuth = FirebaseAuth.getInstance();
        loginButton.setText("Continue with Facebook");
        tc = findViewById(R.id.tc);
        pp = findViewById(R.id.pp);
        tc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = "http://funswitch.io/gymmon-mobile-app-terms-conditions/";
                Intent intent1 = new Intent(LoginActivity.this, WebViewActivity.class);
                intent1.putExtra("terms_url", url);
                startActivity(intent1);
            }
        });

        pp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = "http://funswitch.io/gymmon-mobile-app-privacy-policy/";
                Intent intent1 = new Intent(LoginActivity.this, WebViewActivity.class);
                intent1.putExtra("privacy_url", url);
                startActivity(intent1);
            }
        });

        // Configure Google Sign In
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.default_web_client_id))
                .requestScopes(new Scope(Scopes.PLUS_LOGIN))
                .requestProfile()
                .requestEmail()
                .build();


//        mGoogleApiClient = new GoogleApiClient.Builder(this)
//                .enableAutoManage(this /* FragmentActivity */, (GoogleApiClient.OnConnectionFailedListener) this /* OnConnectionFailedListener */)
//                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
//                .addApi(Plus.API)
//                .build();

        // Build a GoogleSignInClient with the options specified by gso.
        mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

        mGoogleSignInClient.revokeAccess()
                .addOnCompleteListener(this, new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        // ...

                    }
                });
        googleSignInButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GymMonEvents.googleLogin(AppDatabase.getInstance(getApplicationContext()).gymMonDao().getCurrentUser());
                signIn();
            }
        });


        facebookLogin();

    }

    private void facebookLogin() {
        loginButton.setReadPermissions(Arrays.asList("public_profile, email"));
        loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(final LoginResult loginResult) {
                Log.d("MainAcitvity", "facebook:onSuccess:" + loginResult);
                handleFacebookAccessToken(loginResult.getAccessToken());
                GymMonEvents.fbLogin(AppDatabase.getInstance(getApplicationContext()).gymMonDao().getCurrentUser());
                GraphRequest graphRequest = GraphRequest.newMeRequest(
                        loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(JSONObject object, GraphResponse response) {
                                progressDialog.show();
                                progressDialog.setMessage("Please wait...");

                                Log.d("Main", response.toString());
                                try {
                                    final String name = object.getString("name");
                                    Log.d("---->name", name);
                                    final String profilePicUrl = object.getJSONObject("picture").getJSONObject("data").getString("url");
                                    Log.d("---->profile", profilePicUrl);
                                    String id = object.getString("id");
                                    Users users = AppDatabase.getInstance(getApplicationContext()).gymMonDao().getCurrentUser();
                                    users.setUserName(name);
                                    AppDatabase.getInstance(getApplicationContext()).gymMonDao().updateUsername(name, users.getId());

                                    new Handler().postDelayed(new Runnable() {

                                        @Override
                                        public void run() {
                                            // This method will be executed once the timer is over
                                            if (FirebaseAuth.getInstance().getCurrentUser() != null) {
                                                databaseReference.child("Users").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("Name").setValue(name);
                                                databaseReference.child("Users").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("profileUrl").setValue(profilePicUrl);
                                                if (getOneTimeStatus()) {
                                                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                                    startActivity(intent);
                                                } else {
                                                    Intent intent = new Intent(LoginActivity.this, OneTimeInputProfile.class);
                                                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                                    setDayData();
                                                    startActivity(intent);
                                                }
                                            } else {
                                                Toast.makeText(LoginActivity.this, "Login failed", Toast.LENGTH_SHORT).show();
                                            }
                                            progressDialog.dismiss();
                                        }
                                    }, 5000);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }


                            }
                        });
                Bundle parameters = new Bundle();
                parameters.putString("fields", "id,name,picture.type(large)");
                graphRequest.setParameters(parameters);
                graphRequest.executeAsync();
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onError(FacebookException error) {

            }
        });

    }

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


    public void handleFacebookAccessToken(AccessToken token) {


        Log.d("MainActivity", "handleFacebookAccessToken:" + token);
        AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
        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("Login Activity", "signInWithCredential:success");
                            loginButton.setText("Continue with Facebook");
                            FirebaseUser user = mAuth.getCurrentUser();
                            if (user != null) {

                                loginButton.setText("Continue with Facebook");

                            } else {
                                Log.d("uidFacebook", "Not generated");
                            }


                        } else {
                            // If sign in fails, display a message to the user.
                            Log.w("Login Activity", "signInWithCredential:failure", task.getException());
                            Toast.makeText(LoginActivity.this, "You are already registered with same email via google",
                                    Toast.LENGTH_SHORT).show();
                        }

                        // ...
                    }
                });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        mCallbackManager.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);
                firebaseAuthWithGoogle(account);
            } catch (ApiException e) {
                // Google Sign In failed, update UI appropriately
                Log.w(TAG, "Google sign in failed", e);
                // ...
            }
        }

    }

在这里,我正在与谷歌进行身份验证。

     private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
        progressDialog.show();
        progressDialog.setMessage("Please wait...");

        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) {
                        if (task.isSuccessful()) {
                            // Sign in success, update UI with the signed-in user's information
                            Log.d(TAG, "signInWithCredential:success");
                            FirebaseUser user = mAuth.getCurrentUser();
                            if (user != null) {
                                if (getOneTimeStatus()) {
                                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                    startActivity(intent);
                                } else {
                                    Intent intent = new Intent(LoginActivity.this, OneTimeInputProfile.class);
                                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                    setDayData();
                                    startActivity(intent);
                                }

                                progressDialog.dismiss();
                            } else {
                                Log.d("uidGoogle", "Not generated");
                            }
                        } else {
                            // If sign in fails, display a message to the user.
                            Log.d(TAG, "signInWithCredential:failure", task.getException());
                        }

                        // ...
                    }
                });
    }

    @Override
    public void onStart() {
        super.onStart();
        // Check if user is signed in (non-null) and update UI accordingly.
        FirebaseUser currentUser = mAuth.getCurrentUser();
        if (currentUser != null) {
            if (getOneTimeStatus()) {
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
            } else {
                Intent intent = new Intent(LoginActivity.this, OneTimeInputProfile.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                setDayData();
                startActivity(intent);
            }
        }
    }

这样做之后,请帮助我解决它影响我的应用中的实时用户的问题。

答案

尝试在登录之前在代码中添加googleApiClient.signOut();,一旦问题得到解决,请从代码中删除它。

这是因为谷歌不知道登录用户现在已经消失了,也许它维护了这个应用程序中的一个用户已经登录并且它为它维护了一个令牌,所以这可能有所帮助。

以上是关于从firebase删除数据后无法使用google和facebook登录的主要内容,如果未能解决你的问题,请参考以下文章

如何从一个片段中删除数据,这些片段应该反映在google firebase中的其他片段中

Firebase我无法删除帐户[关闭]

使用firebase和flutter登录google后获取用户详细信息

谷歌服务插件和数据绑定问题

错误:Firebase Auth Google 删除 EmailPassword Auth

无法使用 SwiftUI 从 Firebase / Firestore 中删除文档