Support Topics Documentation Slack YouTube Blog

You are not logged in. Please log in and try again


(Software Heaven) #1

Hi Backendless Team,
Im tryin to integrate Facebook Login in my Android App, but every time i click the login button I get You are not logged in. Please log in and try again Error, It does not ask me to login,
I have added api.backendless.com to Facebook Login --> settings --> Valid OAuth redirect URIs in my facebook developer console, Im using Backendless version 4.0.0-beta3 and this is my LoginActivity code :
PS : The New documentation regarding the Facebook Login is not complete … check it at https://backendless.com/docs/android/users_facebook_login.html

public class LoginActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener,
        View.OnClickListener {

    @BindView(R.id.login_facebook)
    Button loginFacebookBtn;

    @BindView(R.id.privacy_policy)
    TextView privacyPolicy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

        Backendless.setUrl(AppConstant.SERVER_URL);
        Backendless.initApp(this, AppConstant.APPLICATION_ID, AppConstant.API_KEY);


        String userToken = UserTokenStorageFactory.instance().getStorage().get();

        if (userToken != null && !userToken.equals("")) {

            Backendless.UserService.isValidLogin(new DefaultCallback<Boolean>(this) {
                @Override
                public void handleResponse(Boolean isValidLogin) {
                    if (isValidLogin && Backendless.UserService.CurrentUser() == null) {
                        String currentUserId = Backendless.UserService.loggedInUser();

                        if (!currentUserId.equals("")) {
                            Backendless.UserService.findById(currentUserId, new DefaultCallback<BackendlessUser>(LoginActivity.this, "Logging in...") {
                                @Override
                                public void handleResponse(BackendlessUser currentUser) {
                                    super.handleResponse(currentUser);
                                    Backendless.UserService.setCurrentUser(currentUser);
                                    ActivityHelper.start(LoginActivity.this, MainActivity.class);
                                    finish();
                                }
                            });
                        }
                    }

                    super.handleResponse(isValidLogin);
                }
            });
        }

        loginWithFacebook();

        privacyPolicy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(LoginActivity.this, PrivacyPolicy.class));
            }
        });

        registerDevice();
    }

    private void registerDevice() {
        Backendless.Messaging.registerDevice(AppConstant.GCM_SENDER_ID, AppConstant.MESSAGING_CHANNEL, registerCallback());
    }

    private DefaultCallback<Void> registerCallback() {
        return new DefaultCallback<Void>(this) {
            @Override
            public void handleResponse(Void response) {
                super.handleResponse(response);
            }

            @Override
            public void handleFault(BackendlessFault fault) {
                super.handleFault(fault);
            }
        };
    }

    private void loginWithFacebook() {
        loginFacebookBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onLoginWithFacebookButtonClicked();
            }
        });
    }

    private void onLoginWithFacebookButtonClicked() {

        Map<String, String> facebookFieldsMapping = new HashMap<>();
        facebookFieldsMapping.put("name", "name");
        facebookFieldsMapping.put("email", "email");

        List<String> facebookPermissions = new ArrayList<>();
        facebookPermissions.add("email");

        Backendless.UserService.loginWithFacebook(LoginActivity.this, null, facebookFieldsMapping, facebookPermissions, new SocialCallback<BackendlessUser>(LoginActivity.this) {
            @Override
            public void handleResponse(BackendlessUser response) {
                ActivityHelper.start(LoginActivity.this, MainActivity.class);
                finish();
            }

            @Override
            public void handleFault(BackendlessFault fault) {
                super.handleFault(fault);

                Toasty.info(LoginActivity.this, "Try again, please ..", Toast.LENGTH_LONG).show();
            }
        }, true);

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
        System.exit(0);
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

    }
}

(Sergey Kukurudzyak) #2

@Software_Heaven use following doc https://developers.facebook.com/docs/facebook-login/android to get accessToken then use it with one of the overridden methods Backendless.UserService .loginWithFacebookSdk