0

I am working on Facebook integration in my application:

But something going wrong and the email address of the user always returns null value:

Here is my code:

    @Override
public void onCreate(Bundle savedInstanceState) {
..
..
        //initiate session
        Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
        Session session = Session.getActiveSession();
        if (session == null) {
            if (savedInstanceState != null) {
                session = Session.restoreSession(this, null, statusCallback, savedInstanceState);
            }
            if (session == null) {
                session = new Session(this);
            }
            Session.setActiveSession(session);
            if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
                Session.OpenRequest op = new Session.OpenRequest(this);
            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.setActiveSession(session);
            session.openForPublish(op);
        }
    }
}

@Override
public void onStart() {
    Logger.d(TAG,"onStart");
    super.onStart();
    Session.getActiveSession().addCallback(statusCallback);
}

@Override
public void onStop() {
    Logger.d(TAG,"onStop");
    super.onStop();
    Session.getActiveSession().removeCallback(statusCallback);
}

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

@Override
protected void onSaveInstanceState(Bundle outState) {
    Logger.d(TAG,"onSaveInstanceState");
    super.onSaveInstanceState(outState);
    Session session = Session.getActiveSession();
    Session.saveSession(session, outState);
}


private void getUserDetails(Session session) {
    Logger.d(TAG,"getUserDetails");
    Request request = Request.newMeRequest(session, new Request.GraphUserCallback() {
        @Override
        public void onCompleted(GraphUser user, Response response) {
            Log.d(TAG,"newMeRequest:onCompleted");
            if (user != null) {
                fbCurrentUser = user;
                Log.d(TAG,"Already logged-in : "+ user.getId());
                Log.d(TAG,"User First Name: "+ user.getFirstName());
                Log.d(TAG, "User Email: " + user.getProperty("email"));
                Log.e(TAG, "response: " + response);
                Log.e(TAG, "user: " + user.toString());
                //check if new user [get his friends list]
                getFriendsList();
            } else {
                Log.e(TAG,"No User Found");
            }
        }

    });
    Request.executeBatchAsync(request);
}

Please Help! Thanks in advance!

AabidMulani
  • 2,325
  • 1
  • 28
  • 47

2 Answers2

0

Try this for getting email,

String email = user.asMap().get("email").toString();
Log.d(TAG, "User Email: " + email);
TextView mEmail = (TextView) findViewById(R.id.textView1);
mEmail.setText(email);

or else give a try to below code

Log.d(TAG, "User Email: " + user.getProperty("email").toString()); 

just add toString() at end of Log for more reference check this link

With this above piece of code i am successfully able to get the user's email.

Edit

Session.openActiveSession(this, true, new Session.StatusCallback() {

            @Override
            public void call(Session session, SessionState state,
                    Exception exception) {
                if (session.isOpened()) {
                    // make request to the /me API
                    Request.executeMeRequestAsync(session,
                            new Request.GraphUserCallback() {
                                @Override
                                public void onCompleted(GraphUser user,
                                        Response response) {
                                    if (user != null) {
                                        String email = user.asMap()
                                                .get("email").toString();
                                        mEmail.setText(email);
                                    }
                                }
                            });
                } else {
                    Toast.makeText(getApplicationContext(), "Error...",
                            Toast.LENGTH_LONG);
                }
            }
        });
Community
  • 1
  • 1
InnocentKiller
  • 5,234
  • 7
  • 36
  • 84
  • Not working, gives a null pointer! Please cross check if my posted code is correct.. – AabidMulani Apr 07 '14 at 08:35
  • String email = user.asMap().get("email").toString(); – AabidMulani Apr 07 '14 at 08:36
  • Which facebook SDK version are you using. – InnocentKiller Apr 07 '14 at 08:38
  • Okay ,try this, Log.d(TAG, `"User Email: " + user.getProperty("email").toString());` just add `toString()` after email and check and let me know. – InnocentKiller Apr 07 '14 at 08:41
  • Not working sir; it gives output as User Email: null – AabidMulani Apr 07 '14 at 08:45
  • I have printed the whole response that is coming from facebook server; it does not have any field named email – AabidMulani Apr 07 '14 at 08:46
  • well in my case it is working fine, i don't know why you are not getting, check one more time your full code. And also i suggest you to check the link which i have given in my answer. All the best. – InnocentKiller Apr 07 '14 at 08:48
  • I agree with your answer: but i dont find the key pair value for email in the response from facebook server. I doubt my posted code is going wrong some where. Can you please provide me with the code that you used. – AabidMulani Apr 07 '14 at 08:48
  • @username_AB, check my edited answer. use this method inside `onCreate`. – InnocentKiller Apr 07 '14 at 08:53
  • hey one strange observation: If the app uses a fresh account and then authenticate it [I GET THE EMAIL] but if the application is already authenticated in that scenario I get a null value...can u help to sort this out. – AabidMulani Apr 07 '14 at 10:24
  • If the application is used first time[i.e. U need to authenticate and grant access to the application to read fb data], in this scenario i get the users email id. But once i have authenticated the application and next time i login it do not pass email address. – AabidMulani Apr 07 '14 at 10:28
  • http://stackoverflow.com/questions/30319119/how-can-i-fetch-name-and-email-using-facebook-sdk-4/30319796#30319796 – Tufan May 19 '15 at 09:20
0

Check the response giving any error or not then

JSONObject json = com.facebook.android.Util.parseJson(response);
  if(json != null){
        final String id = json.getString("id");
    final String email = json.getString("email");
  }
Yuvaraja
  • 715
  • 6
  • 22