简体   繁体   中英

Problems with facebook login for android application

We are using for our android application Facebook SDK version 3.0.1 For Facebook login I used this tutorial : https://developers.facebook.com/docs/howtos/androidsdk/3.0/login-with-facebook/

Many of my users have problems to get in to our application using Facebook login. Users keep sending me emails about this problems and also I can see in their logs, that they cannot get in. After clicking on Facebook LoginButton, it goes to Facebook LoginActivity, displays the progress bar and stays there for ever. I don't get any answer from Facebook and don't get any exceptions, exept when the user clicks on back button.

I've printed some logs from Facebook's SDK, so maybe someone will understand why this problems happens.

记录事件

here is an example of logs flow that works OK

在此处输入图片说明

here is my code :

My Activity :


/**My Activity **/
//BackClickListenerActivity extends FragmentActivity
public class RegisterActivity extends BackClickListenerActivity implements RegistrationManager{

        private RegistrationFragment mainFragment;

        @Override
        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);    
            setContentView(R.layout.register);

            if (savedInstanceState == null) {
                // Add the fragment on initial activity setup
                mainFragment = new RegistrationFragment();
                getSupportFragmentManager()
                .beginTransaction()
                .add(android.R.id.content, mainFragment)
                .commit();
            } else {
                // Or set the fragment from restored state info
                mainFragment = (RegistrationFragment) getSupportFragmentManager()
                        .findFragmentById(android.R.id.content);
            }
            ...
        }

        public void onStart() {     
            mainFragment.initialize(this,R.layout.register,false);
            super.onStart();
        }

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

            case REQUEST_FACEBOOK_LOGIN:
                if (resultCode == RESULT_OK) {
                    ...
                } 
                else {
                    ...
                }
                ...
            }
        }

}

My Fragment :

   public class RegistrationFragment extends Fragment{

        public void initialize(RegistrationManager registrationManager, int layoutRes, boolean hideButton){

            this.registrationManager = registrationManager;
            sentRequest = false;
                    // I use this fragment from 2 different Activities, and 2 layouts
            this.layoutRes = layoutRes;

        }


    @Override
    public View onCreateView(LayoutInflater inflater, 
            ViewGroup container, 
            Bundle savedInstanceState) {
        Log.d(TAG,"onCreateView RegistrationManager");
        final View view = inflater.inflate(layoutRes, container, false);

        LoginButton authButton = (LoginButton) view.findViewById(R.id.facebook_login);

        authButton.setFragment(this);
        authButton.setReadPermissions(Arrays.asList(FacebookConfig.getInstance().getPermissions()));
        ...
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d(TAG,"onCreate RegistrationManager");
        super.onCreate(savedInstanceState);
        uiHelper = new UiLifecycleHelper(getActivity(), callback);
        uiHelper.onCreate(savedInstanceState);
    }
    private void onSessionStateChange(Session session, SessionState state, Exception exception) {

        if(state == SessionState.OPENING){
            ...
        }
        else if (state.isOpened()) {
            ...

        } else if (state.isClosed()) {
            ...

        }
    }
    private Session.StatusCallback callback = new Session.StatusCallback() {
        @Override
        public void call(Session session, SessionState state, Exception exception) {
            onSessionStateChange(session, state, exception);
        }
    };
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        uiHelper.onActivityResult(requestCode, resultCode, data);
    }
    @Override
    public void onResume() {
        super.onResume();
        uiHelper.onResume();
    }
    @Override
    public void onPause() {
        super.onPause();
        uiHelper.onPause();
    }

    @Override
    public void onStop(){
        super.onStop();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        uiHelper.onDestroy();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        uiHelper.onSaveInstanceState(outState);
    }

}

At the SDK, I did some changes that are dealing with the LoginButton - I've changed the image, the text and put under comment at onClick listener the case when session is not null (to prevent logout). Hope my question is clear enough, and someone could help us. Thank you, Alex

I have the same problem. In devices 2.3.4 or lower default login don't work as expected. For all those devices I have to implement Web Dialog. Getting access token from the dialog after completion and setting up session from that access token. It seems to be working as of now.

Bundle bundle = new Bundle();
bundle.putString("message", "message");
WebDialog localWebDialog = new WebDialog.Builder(this, "app_id", "oauth", bundle).build();
localWebDialog.setOnCompleteListener(new WebDialog.OnCompleteListener()
{
    public void onComplete(Bundle bundle, FacebookException facebookException)
    {

        Session.getActiveSession();
        AccessToken localAccessToken = AccessToken.createFromExistingAccessToken(bundle.getString("access_token"), null, null, AccessTokenSource.WEB_VIEW, null);
        Session.openActiveSessionWithAccessToken(MainActivity.this.getApplicationContext(), localAccessToken, MainActivity.this.callback);
    }
});
localWebDialog.show();

private class SessionStatusCallback implements Session.StatusCallback {
    @Override
    public void call(Session session, SessionState state, Exception exception) {
        if(session.isOpened()
            //do something
    }

I cant add a comment, so please dont Downvote this :).

In order to Facebook you dont get an exceptions but often a warning/logoutputs, in there you can see what happens during the login process. If you would have a keyhash problem, then you would definetely get an warning exception.

Please check your logcat for hints.

A very good sample is this Hackbook

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM