Facebook Android SDK does not open Session after login

前端 未结 2 636
别那么骄傲
别那么骄傲 2021-01-01 07:13

1) This question appears many times and the awnser is always \"you have the wrong Key Hash\".

2) Facebook documentation tells we to use this code to get our Key Has

相关标签:
2条回答
  • 2021-01-01 07:23

    Are making call again and again (By clicking same login button). If yes then facebook dialog will not open after 1st time (on fist time click of login bttn it will open fbk dialog but not after that) Reason: Because session is created and it is not cleared `ie person is not logged out with the help of this session you can get profile info of fbk user.

    if you want to do that that can too be happen just write these lines of code:

     Session session = Session.getActiveSession();
                 if (session != null && session.isOpened()) {
    
                 Toast.makeText(getApplicationContext(), "session is opened",
                 Toast.LENGTH_LONG).show();
    
                 /* clear session and again click will show facebook login */
    
                 session = Session.getActiveSession();
                 if (!session.isClosed()) {
                 session.closeAndClearTokenInformation();
                 }
    
                 }
    

    after clearing token now make login call

    FacebookAuthentication();
    
    
        private void FacebookAuthentication() {
    
            // Toast.makeText(getApplicationContext(),
            // "new session was created and requires login", Toast.LENGTH_LONG)
            // .show();
    
            Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
    
            OpenRequest op = new Session.OpenRequest(SignUpActivity.this);
    
            op.setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO);
            op.setCallback(statusCallback);
    
            op.setPermissions(Arrays.asList("email", "public_profile"));
    
            Session session = new Session.Builder(getApplicationContext()).build();
    
            Session.setActiveSession(session);
    
            session.openForRead(op);
    
        }
    

    Declare globally

    private Session.StatusCallback statusCallback = new SessionStatusCallback();.
    

    //private emails are not accessed

    private class SessionStatusCallback implements Session.StatusCallback {
            @Override
            public void call(final Session session, SessionState state,
                    Exception exception) {
    
                if (session.isOpened()) {
    
                    makeFacebookMeRequest(session);
    
                }
    
            }
        }
    
    public void makeFacebookMeRequest(final Session session) {
    
            Request.newMeRequest(session, new Request.GraphUserCallback() {
    
                @Override
                public void onCompleted(GraphUser user, Response response) {
    
                    if (user != null) {
    
                        String access_token = session.getAccessToken();
    
                        Log.d("Response", "Response=" + response);
                        Log.d("AccessToken", access_token);
    
                        UserId_FromFacebook = user.getId();
                        UserName_FromFacebook = user.getUsername();
                        FirstName_FromFacebook = user.getFirstName();
                        LastName_FromFacebook = user.getLastName();
    
                        try {
    
                            Email_FromFacebook = user.asMap().get("email")
                                    .toString();
    
                            // Toast.makeText(getApplicationContext(),
                            // "" + Email_FromFacebook, Toast.LENGTH_LONG)
                            // .show();
    
                        } catch (Exception e) {
    
                            // e.printStackTrace();
    
                        }
    
                    }
    
                    //You can move to your activiy here and make sure user is signed up using your api
    
                }
    
            }).executeAsync();
        }
    

    //////////////////Apply this patch in your code :) Call below method for login and it will solve your problems (if on same button click again and again you want fbk dialog just uncomment my code in below method )//////////////////////

    private void LoginAndFetchFacebookProfileInfo() {
    
            Session currentSession = Session.getActiveSession();
    
            if (currentSession == null || currentSession.getState().isClosed()) {
    
    
    
                Session session = new Session(getApplicationContext());
                Session.setActiveSession(session);
    
                currentSession = session;
    
            }
    
            if (currentSession.isOpened()) {
    
    //you are logged in so do whatever you want
                // Toast.makeText(SignUpActivity.this, "session is  opened",
                // Toast.LENGTH_LONG).show();
    
                makeFacebookMeRequest(currentSession);
    
                // Session session = Session.getActiveSession();
                // if (session != null && session.isOpened()) {
                //
                // Toast.makeText(getApplicationContext(), "session is opened",
                // Toast.LENGTH_LONG).show();
                //
                // /* clear session and again click will show facebook login */
                //
                // session = Session.getActiveSession();
                // if (!session.isClosed()) {
                // session.closeAndClearTokenInformation();
                // }
                //
                // }
                //
                // FacebookAuthentication();
    
            } else if (!currentSession.isOpened()) {
                // Ask for username and password
                // Toast.makeText(SignUpActivity.this, "session is not opened",
                // Toast.LENGTH_LONG).show();
    
                FacebookAuthentication();
            }
    
        }
    
    0 讨论(0)
  • 2021-01-01 07:44

    I figured it out:

    The following method was missing. Just added it and everything worked.

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
    }
    
    0 讨论(0)
提交回复
热议问题