Skip to content Skip to sidebar Skip to footer

How To Get Email Id From Facebook Sdk In Android Applications?

I integrated Facebook login in my android application. I want to get email id of login user. How will I get it? private void loginToFacebook() { Session.openActiveSession(this,

Solution 1:

Before calling Session.openActiveSession do this to get permissions add this:

List<String> permissions = newArrayList<String>();
permissions.add("email");

The last parameter in Session.openActiveSession() should be permissions. Now you can access user.getProperty("email").toString().

EDIT:

This is the way I am doing facebook authorization:

List<String> permissions = newArrayList<String>();
permissions.add("email");

loginProgress.setVisibility(View.VISIBLE);

//start Facebook sessionopenActiveSession(this, true, newSession.StatusCallback() {
    @Overridepublicvoidcall(Session session, SessionState state, Exception exception) {
        if (session.isOpened()) {
            //make request to the /me APILog.e("sessionopened", "true");
            Request.executeMeRequestAsync(session, newRequest.GraphUserCallback() {
                @OverridepublicvoidonCompleted(GraphUser user, Response response) {
                    if (user != null) {
                        String firstName = user.getFirstName();
                        String lastName = user.getLastName();
                        String id = user.getId();
                        String email = user.getProperty("email").toString();

                        Log.e("facebookid", id);
                        Log.e("firstName", firstName);
                        Log.e("lastName", lastName);
                        Log.e("email", email);
                    }
                }
            });
         }
     }
 }, permissions);

Add this method to your activity:

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

Solution 2:

Following the suggestion of Egor N, I change my old code

Session.openActiveSession(this, true, statusCallback);

whith this new one:

List<String> permissions = newArrayList<String>();
permissions.add("email");
Session.openActiveSession(this, true, permissions, statusCallback);

Now FB ask the user about the permission of email and I can read it in the response.

Solution 3:

Try this article. Hope your issues will be solved Click Here

Btw, You need to use user.asMap().get("email").toString()); for receving the User Email ID. Also, you need to assign that into some of label like lblEmail.setText where lblEmail is a Textview.

Post a Comment for "How To Get Email Id From Facebook Sdk In Android Applications?"