1
  1. code below shows:- fb login button click ,session created, retrieving details in onActivityResult().
  2. user.asMap().get("email") is always null but can fetch name user id

    loginButton = (LoginButton)findViewById(R.id.login_button);
    loginButton.setOnClickListener(new OnClickListener() {
    
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
         callback = new Session.StatusCallback() {
                @Override
                public void call(Session session, SessionState state,
                        Exception exception) {
                    onSessionStateChange(session, state, exception);
                    System.out.println("onclick");
                }
            };
        }
    });
    
    private void onSessionStateChange(Session session, SessionState state,
            Exception exception) {
    
        // When Session is successfully opened (User logged-in)
        session1=session;
        if (state.isOpened()) {
            Log.i(TAG, "Logged in...");
            // make request to the /me API to get Graph user
            String[] PERMISSION_ARRAY_READ = {"email","user_birthday"};
            email=PERMISSION_ARRAY_READ[0];
            List<String> PERMISSION_LIST=Arrays.asList(PERMISSION_ARRAY_READ);
            session.openForRead(new Session.OpenRequest(getParent()).setPermissions(PERMISSION_LIST).setCallback(callback));
            Request.newMeRequest(session, new Request.GraphUserCallback() {
    
                // callback after Graph API response with user
                // object
    
                @Override
                public void onCompleted(GraphUser user,
                        com.facebook.Response response) {
                    // TODO Auto-generated method stub
                    if(user!=null){
                    fbID=user.getId();
                    }
                }
    
            }).executeAsync();
        } else if (state.isClosed()) {
            Log.i(TAG, "Logged out...");
            //otherView.setVisibility(View.GONE);
        }
    }
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    
        uiHelper.onActivityResult(requestCode, resultCode, data);
        Log.i(TAG, "OnActivityResult...");
    
        Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
    
        if (Session.getActiveSession().isOpened())
        {
            // Request user data and show the results
              String[] PERMISSION_ARRAY_READ = {"email","user_birthday"};
                email=PERMISSION_ARRAY_READ[0];
                final List<String> PERMISSION_LIST=Arrays.asList(PERMISSION_ARRAY_READ);
                Arrays.asList("email", "user_birthday");
                //session1.openForRead(new Session.OpenRequest(getParent()).setPermissions(PERMISSION_LIST).setCallback(callback));
            Request.newMeRequest(Session.getActiveSession(), new GraphUserCallback()
            {
    
                @Override
                public void onCompleted(GraphUser user,
                        com.facebook.Response response) {
                    // TODO Auto-generated method stub
                    if (null != user)
                    {
                        // Display the parsed user info
    
                        System.out.println("UserID : " + user.getId()  +"   name  "+user.getName() );
                        //System.out.println(" email "+user.asMap().get("email").toString() );
    
                        System.out.println((String) response.getGraphObject().getProperty("email")+" EEEEEEEEEEE");
                        System.out.println("Birthday "+user.getBirthday() +" hhh "+user.getProperty("email"));
                        System.out.println(user.asMap().get("gender") +" KKKKKKKKK");
    
                        fbID=user.getId();
                    }
    
                }
            }).executeAsync();
        }
    }
    
Armali
  • 18,255
  • 14
  • 57
  • 171
Alan
  • 13
  • 3

1 Answers1

0

You should check the new SDK as it is easier to implement and use, but regarding your problem, Facebook won't send you the user email, unless you ask for it, something like this (I didn't test it as I don't have the old SDK anymore):

Request request = Request.newMeRequest(Session.getActiveSession(), new GraphUserCallback()
    {

        @Override
        public void onCompleted(GraphUser user,
                com.facebook.Response response) {
            // TODO Auto-generated method stub
            if (null != user)
            {
                // Display the parsed user info

                System.out.println("UserID : " + user.getId()  +"   name  "+user.getName() );
                //System.out.println(" email "+user.asMap().get("email").toString() );

                System.out.println((String) response.getGraphObject().getProperty("email")+" EEEEEEEEEEE");
                System.out.println("Birthday "+user.getBirthday() +" hhh "+user.getProperty("email"));
                System.out.println(user.asMap().get("gender") +" KKKKKKKKK");

                fbID=user.getId();
            }

        }
    });

Bundle bundle = new Bundle();
bundle.putString("fields", "id,email,<OTHER FIELDS YOU NEED>");
request.setParameters(bundle);
request.executeAsync();
Marco Batista
  • 1,410
  • 1
  • 20
  • 38
  • Great answer!..May I ask about a way to Log in/Log out from FB without using the custom button provided by FB sdk?.. – Alan Aug 17 '15 at 05:00
  • Using the new SDK, you just had to use the `LoginManager` provided by Facebook (example: http://stackoverflow.com/a/30230718/1333516). Using the old SDK (here is an example: http://stackoverflow.com/a/25371663/1333516). One suggetion that I would make, is to add the LoginButton to your interface and make it hidden, then when you want to login/logout just call `.preformClick()` on the LoginButton, that way the SDK will handle all the login and logout logic for you – Marco Batista Aug 17 '15 at 05:16
  • if user has hidden email from timeline ,I cant fetch it ;again email goes null. Is it a mistake ? – Alan Sep 08 '15 at 12:15
  • Facebook only gives you access to the information the user has made public. If the user hid the email, then you'll have to explicitly ask the user for it – Marco Batista Sep 08 '15 at 13:17
  • ok..but the problem is first time when a user logs in via FB - value of (String) response.getGraphObject().getProperty("email") is null..after attempting 3or 4 times it returns the value also..how this happens? – Alan Sep 09 '15 at 11:23
  • when you say "after attempting 3or 4 times", do you mean login attempts? Have you tried migrating to the newer SDK, there may be a bug on the API you are using – Marco Batista Sep 14 '15 at 17:18