Question

I want to do native Facebook login with the following code. Can anybody help me about what is incorrect with the following code? I am using UiHelper class because I need this for posting custom stories with open graph.

I have used a button in this code and on clicking this button, I also want to post stories using Open Graph.

If anybody has a sample like same as I mentioned, please give me or refer to on that link. I also see the facebook document but did not get it, if any body has a sample for this, it will be very beneficial for me.
I have searched a lot but found nothing, so anybody help me to do this. I will be very thankful to you.

package com.blur.blureffect;

    import com.facebook.Request;
    import com.facebook.Response;
    import com.facebook.Session;
    import com.facebook.SessionState;
    import com.facebook.UiLifecycleHelper;
    import com.facebook.model.GraphUser;
    import com.facebook.widget.FacebookDialog;

    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.util.Log;
    import android.view.Menu;
    import android.widget.Button;

    public class MainActivity extends Activity {
        Button button1;
        private UiLifecycleHelper uiHelper;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            button1 = (Button) findViewById(R.id.button1);
            uiHelper = new UiLifecycleHelper(this, callback);
            uiHelper.onCreate(savedInstanceState);
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

        private Session.StatusCallback callback = new Session.StatusCallback() {
            @Override
            public void call(Session session, SessionState state, Exception exception) {
                onSessionStateChange(session, state, exception);
            }
        };

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            uiHelper.onActivityResult(requestCode, resultCode, data,
                    new FacebookDialog.Callback() {
                        @Override
                        public void onError(FacebookDialog.PendingCall pendingCall, 
                                Exception error, Bundle data) {
                                    Log.e("Activity",
                                    String.format("Error: %s", error.toString()));
                        }

                        @Override
                        public void onComplete(
                        FacebookDialog.PendingCall pendingCall, Bundle data) {
                            Log.i("Activity", "Success!");
                            button1.setEnabled(true);
                        }
            });
        }

        @SuppressWarnings("deprecation")
        private void onSessionStateChange(Session session, SessionState state,
                Exception exception) {
            if (session != null && session.isOpened()) {
                Log.d("DEBUG", "facebook session is open ");
                // make request to the /me API
                Request.executeMeRequestAsync(session,
                        new Request.GraphUserCallback() {
                            // callback after Graph API response with user object

                            @Override
                            public void onCompleted(GraphUser user,
                                    Response response) {
                                if (user != null) {
                                    Log.d("DEBUG",
                                            "email: "
                                                    + user.asMap().get("email")
                                                            .toString());
                                    button1.setEnabled(true);
                                }

                            }
                        });
            }
        }

        @Override
        protected void onResume() {
            super.onResume();
            uiHelper.onResume();
        }

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

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

        @Override
        public void onDestroy() {
            super.onDestroy();
            uiHelper.onDestroy();
        }
    }
Was it helpful?

Solution

Add the ButtonClick event

public void btnLoginWithFbClick(View v) {
        Session session = Session.getActiveSession();
        if (!session.isOpened() && !session.isClosed()) {
            session.openForRead(new Session.OpenRequest(this).setPermissions(
                    Arrays.asList("basic_info", "email"))
                    .setCallback(callback));
        } else {
            Session.openActiveSession(this, true, callback);
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top