Not able to signIn with google and facebook after deleting data from firebase

南笙酒味 提交于 2020-01-06 05:25:19

问题


After deleting all the database and authenticate users from firebase database. When I am trying to signIn with it again it's showing below error.

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)

Below is the code for googleSignIn and facebook signin

    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);
                // ...
            }
        }

    }

Here I am authenticating with google.

     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);
            }
        }
    }

Please help me out it's affecting the live users in my app after doing this.


回答1:


Try adding googleApiClient.signOut(); in your code before you are Signing In and once issue gets resolved remove it from code.

This is because as google does not know that signed in user is now gone and maybe it maintains that from this app one user had logged in and it maintains a token for it so this may be of help.



来源:https://stackoverflow.com/questions/51764664/not-able-to-signin-with-google-and-facebook-after-deleting-data-from-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!