Why is Facebook session state always close?

I want to add the Facebook login feature to my application. Note:

  • I do not want to use the Facebook login button widget.
  • I copied my code that worked correctly last year (from my other project) into my new project.
  • I checked Session and I think I have no problem.

The only difference: Request.executeMeRequestAsync()hss has been changed to Request.newMeRequest(). I made this change, but it seems that my session is always closed, although I could see that the method Session.setActiveSession(session);starts by debugging the project. So, I do not know why sessionit is always close in the method call().

Any idea would be appreciated. Thank.

My code is:

public class FacebookLogin extends FragmentActivity
{
    private static final String TAG = "FacebookLogin";

    private static final List<String> READ_PERMISSIONS =
            Arrays.asList("email", "user_about_me", "user_photos");
    //    private static final List<String> WRITE_PERMISSIONS = Arrays.asList("");


    private final Session.StatusCallback statusCallback = new Session.StatusCallback()
    {
        @Override
        public void call(final Session session, SessionState state, Exception exception)
        {
            if (session.isOpened())
            {
                // make request to the /me API
                Request request = Request.newMeRequest(session, new Request.GraphUserCallback()
                {
                    // callback after Graph API response with user object
                    @Override
                    public void onCompleted(GraphUser user, Response response)
                    {
                        if (user != null)
                        {
                            MyLog.d(TAG, "User name: " + user.getName() + "!, Login successfully :)");
                            MyLog.d(TAG, "User id: " + user.getId());
                            MyLog.d(TAG, "Access token is: " + session.getAccessToken());
                            MyLog.d(TAG, "Application id: " + session.getApplicationId());
                            MyLog.d(TAG, "JSON Object: " + user.getInnerJSONObject());

                            SpStorage.setKeyFacebook(FacebookLogin.this, session.getAccessToken());
                            SpStorage.setFacebookUserId(FacebookLogin.this, user.getId());

                            // erson person = parseJSON(user.getInnerJSONObject().toString());
                            // registerUser();

                            // Close activity
                            FacebookLogin.this.finish();
                        }
                    }
                });
                request.executeAsync();
            }
            else if (state.isClosed()) {
                MyLog.d(TAG, "Facebook session closed");
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        // Find device hash key (should not be used in production)
//        printHashKey();

        // start Facebook Login
        openActiveSession(this, true, statusCallback, READ_PERMISSIONS);
    }

    private static Session openActiveSession(Activity activity, boolean allowLoginUI,
            Session.StatusCallback callback, List<String> permissions)
    {
        Session.OpenRequest openRequest = new Session
                .OpenRequest(activity)
                .setPermissions(permissions)
                .setCallback(callback);
        Session session = new Session.Builder(activity).build();
        if (SessionState.CREATED_TOKEN_LOADED.equals(session.getState()) || allowLoginUI)
        {
            Session.setActiveSession(session);
            session.openForRead(openRequest);
            return session;
        }

        return null;
    }
}

What I get in logcat: Facebook session closed

+4
3

onActivityResult Fragment , .

, UiLifecycleHelper .

+2

onActivityResult SDK facebook. Facebook SDK:

, , : onCreate(), onResume(), onPause(), onDestroy(), onActivityResult() onSaveInstanceState() UiLifecycleHelper. , onCreate() UiLifecycleHelper Facebook , .

+1

Facebook API V2.2 . . 1 . .

public void getProfileData(View button){
         Session activeSession = Session.getActiveSession();
         new Request(
                    activeSession,
                    "me",
                    null,
                    HttpMethod.GET,
                    new Request.Callback() {
                        public void onCompleted(Response response) {
                            /* handle the result */
                                                 try
                                                    {   GraphObject go  = response.getGraphObject();
                                                        JSONObject  jso = go.getInnerJSONObject();
                                                        String name23   = jso.getString("name");
                                                        Log.e("Name  response",""+name23);
                                                        name = name23;
                                                        Toast.makeText(getApplicationContext(), "Name: " + name , Toast.LENGTH_LONG).show();
                                                   }
                                                    catch ( Throwable t )
                                                    {
                                                        t.printStackTrace();
                                                    }
                             String name = response.toString();
                             Log.e("Name  request",""+name);
                        }
                    }
                ).executeAsync();
    }
+1

All Articles