1

I am having trouble getting email from Facebook in my app. I have googled a lot and came across many answers but nothing is working in my case. I followed the same method described here, but the value returning is null. Below given is my code. And also I have added User & Friend permissions as email in the facebook app. It would be of great help if someone could point out the mistake I might be doing.

    private void makeMeRequest(final Session session) {
    Request request = Request.newMeRequest(session,
            new Request.GraphUserCallback() {
                @Override
                public void onCompleted(GraphUser user, Response response) {
                    if (session == Session.getActiveSession()) {
                        if (user != null) {
                            profilePictureView.setProfileId(user.getId());
                            facebook_id = String.valueOf(user.getId());
                            fullName.setText(user.getName());
                            if (user.asMap().get("email") != null)
                                email.setText(user.asMap().get("email")
                                        .toString());
                        }
                    }
                    if (response.getError() != null) {
                    }
                }
            });
    Bundle params = request.getParameters();
    params.putString("fields", "email,name");
    request.setParameters(params);
    request.executeAsync();
}
Community
  • 1
  • 1
Nidhin Rejoice
  • 1,367
  • 1
  • 16
  • 26

2 Answers2

1

The request looks OK. The reason for null value of email property is usually lack of email permission.

You wrote:

And also I have added User & Friend permissions as email in the facebook app

What do you mean by facebook app? You need to ask for permissions in your app.


Take a look at this solution. I think it can help you: https://stackoverflow.com/a/18147719/334522

Community
  • 1
  • 1
sromku
  • 4,663
  • 1
  • 36
  • 37
  • then where should I provide the email permission. Isn't this line of code enough --> params.putString("fields", "email,name"); – Nidhin Rejoice Aug 20 '13 at 11:43
  • 1
    No it isn't. setting `params.putString("fields", "email,name")` is what you want to get from response to your request. You need to add this line: `openRequest.setPermissions(Arrays.asList("email"));` Check out this solution: http://stackoverflow.com/a/13926065/334522. Or, you can use this open source android-simple-facebook library where you don't need to write so much code. Follow these few steps and you will have the answer: https://github.com/sromku/android-simple-facebook#usage – sromku Aug 20 '13 at 12:48
  • I used your library and it has been really useful. But now i am stuck somewhere. I logged in successfully but how can I retrieve the session in a different activity? (without initializing a new instance by setting permissions and configurations). Is there any method for that? I used SimpleFacebook.getInstance(getApplicationContext()); but it is throwing "user not logged in" error – Nidhin Rejoice Aug 28 '13 at 09:42
  • 1
    You are right, I will reveal this option in case of logging in one activity, but working in another. For now, you can call in your other activity for `Session.getActiveSession()`. By the way, I updated the library few days ago and it has better session managment – sromku Aug 28 '13 at 15:09
  • But how could i use the session I get using `Session.getActivieSession()` for some tasks like publishing on facebook using the method `mSimpleFacebook.publish(feed,onPublishListener)` ? – Nidhin Rejoice Sep 03 '13 at 07:39
  • 1
    @NidhinRejoice now, i understand what u r trying to achieve. u don't need to have session from this library. do the next steps: (1) in your first activity do the login stuff. (2) in other activities in onResume method create the instance of SimpleFacebook without setting again anything like permissions. just: `mSimpleFacebook = SimpleFacebook.getInstance(Activity);` (3) now, you can use this library in your second activity. you can call to `mSimpleFacebook.publish(feed,onPublishListener)` and it will work for you. ---- I have updated the library recently and sample app with similar example – sromku Sep 03 '13 at 08:15
  • good luck and you are welcome to upvote :). and if you have any other questions, i am here :) – sromku Sep 03 '13 at 09:25
  • one more question. Its working in normal activities but now I want to `publishFeed()` from a ViewPager activity where I did this `mSimpleFacebook = SimpleFacebook.getInstance(Activity)` in onResume() method and in the fragment I tried to use mSimpleFacebook.publish and its throwing "Cannot request publish or manage authorization with no permissions." Its working perfectly in normal activities but when it comes to fragments this problem..any help? – Nidhin Rejoice Sep 03 '13 at 12:47
  • just to double check: have you added `onActivityResult` like these two lines to the activity that holds fragments: https://github.com/sromku/android-simple-facebook#3-override-onactivityresult-method-and-add-this-line ? – sromku Sep 03 '13 at 13:00
  • It seems that in your current flow the SimpleFacebook.setConfiguration() wasn't called and thus permissions weren't set before asking to `publish()`. please check that you reach the publish method after setConfiguration was called. As i said before, setting configuration can be done once in any place, but it must happen at least one time before running any action. let me know if this was a problem. – sromku Sep 03 '13 at 14:03
  • Yes, I have set the permissions in the LoginActivity. But when I want to publish in another activity, I cant reuse the session without setting the permissions again. – Nidhin Rejoice Sep 11 '13 at 07:24
1

Depending on your needs this might be suitable.

I'm using https://github.com/sromku/android-simple-facebook instead of the bloated Facebook SDK. And with that it's pretty easy to get e-mail.

mSimpleFacebook.getProfile(new OnProfileRequestAdapter()
{
    @Override
    public void onComplete(Profile profile)
    {
        String id = profile.getId();
        String firstName = profile.getFirstName();
        String birthday = profile.getBirthday();
        String email = profile.getEmail();
        String bio = profile.getBio();
        // ... and many more properties of profile ...
    }
});
David Anderson
  • 623
  • 5
  • 22