Anupam Anupam - 1 year ago 49
Android Question

Set permission for getting User's email ID from Facebook Login

I'm using

Facebook 3.0 SDK
for android. I have to implement
Facebook
log in. I'm accessing user's basic info like name, userID. But I want to have access to email also of the user. I have gone through many blogs and forum but cannot figure out how to do that.

I'm using my own android button for log in not
com.facebook.widget.LoginButton
. If I use
Facebook
log in button it was easy just have to use these lines:

authButton.setReadPermissions(Arrays.asList("basic_info","email"));


But, I have my own requirement, will have to go with default android buttons. Here is what I have done so far:

Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(
this, Arrays.asList("email"));

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mLogin = (Button) findViewById(R.id.logIn);
mLogin.setOnClickListener(this);
}

@Override
public void onClick(View v) {
// start Facebook Login
Session.openActiveSession(this, true, new Session.StatusCallback() {

// callback when session changes state
@Override
public void call(final Session session, SessionState state,
Exception exception) {
if (session.isOpened()) {
session.requestNewReadPermissions(newPermissionsRequest);
// 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) {
System.out
.println("Facebook Response: "
+ response.toString());
access_token = session.getAccessToken();
firstName = user.getFirstName();
fb_user_id = user.getId();


System.out
.println("Facebook Access token: "
+ access_token);
System.out.println("First Name:"
+ firstName);
System.out.println("FB USER ID: "
+ fb_user_id);


}
}
});
}
}
});

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode,
resultCode, data);
}


I'm able to get all those things which I'm printing in log, but was unable to retrieve email from the user profile.

What can I do for this?

Any kind of help will be appreciated.

Answer Source

Solved it: Here is what I have done...

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mLogin = (Button) findViewById(R.id.logIn);
    mLogin.setOnClickListener(this);

}

@Override
public void onClick(View v) {

    Session currentSession = Session.getActiveSession();
    if (currentSession == null || currentSession.getState().isClosed()) {
        Session session = new Session.Builder(context).build();
        Session.setActiveSession(session);
        currentSession = session;
    }

    if (currentSession.isOpened()) {
        // Do whatever u want. User has logged in

    } else if (!currentSession.isOpened()) {
        // Ask for username and password
        OpenRequest op = new Session.OpenRequest((Activity) context);

        op.setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO);
        op.setCallback(null);

        List<String> permissions = new ArrayList<String>();
        permissions.add("publish_stream");
        permissions.add("user_likes");
        permissions.add("email");
        permissions.add("user_birthday");
        op.setPermissions(permissions);

        Session session = new Builder(MainActivity.this).build();
        Session.setActiveSession(session);
        session.openForPublish(op);
    }
}

public void call(Session session, SessionState state, Exception exception) {
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (Session.getActiveSession() != null)
        Session.getActiveSession().onActivityResult(this, requestCode,
                resultCode, data);

    Session currentSession = Session.getActiveSession();
    if (currentSession == null || currentSession.getState().isClosed()) {
        Session session = new Session.Builder(context).build();
        Session.setActiveSession(session);
        currentSession = session;
    }

    if (currentSession.isOpened()) {
        Session.openActiveSession(this, true, new Session.StatusCallback() {

            @Override
            public void call(final Session session, SessionState state,
                    Exception exception) {

                if (session.isOpened()) {

                    Request.executeMeRequestAsync(session,
                            new Request.GraphUserCallback() {

                                @Override
                                public void onCompleted(GraphUser user,
                                        Response response) {
                                    if (user != null) {

                                        TextView welcome = (TextView) findViewById(R.id.welcome);
                                        welcome.setText("Hello "
                                                + user.getName() + "!");

                                        access_token = session
                                                .getAccessToken();
                                        firstName = user.getFirstName();
                                        fb_user_id = user.getId();

                                        System.out
                                                .println("Facebook Access token: "
                                                        + access_token);
                                        System.out.println("First Name:"
                                                + firstName);
                                        System.out.println("FB USER ID: "
                                                + fb_user_id);

                                    }
                                }
                            });
                }
            }
        });
    }
}

Now I'm able to get email permission with rest of the publish permission.

Marking this as accepted answer so that it can help others.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download