Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access user email using new Facebook Android SDK

Hi I'm playing with new Facebook Android SDK. One thing I could not figure out is how can I ask for email address. I tried following but it returned values for all fields except email address. I guess I probably have to ask permission separately for email address but not sure how I can do that if I use LoginFragment.

Request request = Request.newMeRequest(session, new GraphUserCallback() {

    @Override
    public void onCompleted(GraphUser user, Response response) {
        profilePictureView.setUserId(user.getId());
        userNameView.setText(user.getName());
    }
});

String NAME = "name";
String ID = "id";
String PICTURE = "picture";
String EMAIL = "email";
String FIELDS = "fields";
String REQUEST_FIELDS = TextUtils.join(",", new String[] {
    ID, NAME, PICTURE, EMAIL
});

Bundle parameters = new Bundle();
parameters.putString(FIELDS, REQUEST_FIELDS);
request.setParameters(parameters);
Request.executeBatchAsync(request);

Any help is appreciated. Thanks

like image 820
Mohammad Haque Avatar asked Nov 03 '12 02:11

Mohammad Haque


2 Answers

Do the following:

After setting the permissions as above access the email via: user.asMap().get("email"));

See the example below

@Override
protected void onSessionStateChange(SessionState state, Exception exception) {

    // user has either logged in or not ...
    if (state.isOpened()) {
        // make request to the /me API
        Request request = Request.newMeRequest(this.getSession(),
                new Request.GraphUserCallback() {
                    // callback after Graph API response with user object

                    @Override
                    public void onCompleted(GraphUser user,
                            Response response) {
                        // TODO Auto-generated method stub
                        if (user != null) {
                            TextView etName = (TextView) findViewById(R.id.etName);

                            etName.setText(user.getName() + ","
                                    + user.getUsername() + ","
                                    + user.getId() + "," + user.getLink()
                                    + "," + user.getFirstName()+ user.asMap().get("email"));

                        }
                    }
                });
        Request.executeBatchAsync(request);
    }
}
like image 155
Golan Shay Avatar answered Sep 19 '22 07:09

Golan Shay


When you open the Session, try including "email" in the list of permissions.

If you are using LoginButton, do something like:

loginButton.setReadPermissions(Arrays.asList("email"));

If you are opening the Session yourself, do something like:

session.openForRead(new Session.OpenRequest(this).setPermissions(Arrays.asList("email")));

You can experiment with requests/permissions using: https://developers.facebook.com/tools/explorer/

Once you have logged in successfully with these permissions, then try the request again including the email field.

like image 35
rightparen Avatar answered Sep 22 '22 07:09

rightparen