简体   繁体   中英

In Android, Facebook API V2.4 is not returning email id whereas V2.3 is returning. How to get email id in V2.4?

When I wrote the following code for API V2.3 this was giving me all details including email id. And now the same code is not giving me email id. What can I can do to get email id?

    oncreate(..)
    {
    .
    .    
    EMAIL_PERMISSION = new ArrayList<String>();
    EMAIL_PERMISSION.add("email");
    uiLifecycleHelper = new UiLifecycleHelper(this, statusCallback);

    uiLifecycleHelper.onCreate(savedInstanceState);

   Session.openActiveSession(this, true, EMAIL_PERMISSION,   
                                          statusCallback);
    // callback when session changes state
Session.StatusCallback statusCallback = new StatusCallback()
{
    @Override
    public void call(Session session, SessionState state, Exception  
                                                         exception)
    {

        // Checking whether the session is opened or not
        if (state.isOpened())
        {
        } else
        {
            if (state.isClosed())
            {
            }
            Log.d(TAG, state.toString());
        }
    }
};
 // Method to get user facebook profile
void getUserFacebookProfile(Session session, final boolean finish)
{
    // Checking whether the session is opened or not
    if (session.isOpened())
    {
        // Sending request to the facebook to get user facebook profile
        Request.newMeRequest(session, new GraphUserCallback()
        {

            @Override
            public void onCompleted(GraphUser user, Response response)
            {

                if (user != null)
                {
                    // To get network user id
                    String networkUserid = user.getId();
                    // To get user first name
                    String fname = user.getFirstName();
                    // To get user last name
                    String lname = user.getLastName();
                    // To get user middle name
                    String mname = user.getMiddleName();
                // String email = user.getProperty("email").toString();
    String email = response.getGraphObject().getProperty("email")
                     .toString();
                }

Now the above code gave me all details including email id for V2.3, now i'm not able to get email id. Please let me know solution. Thanks.

public class LoginFacebook {
    CallbackManager callbackManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        FacebookSdk.sdkInitialize(getApplicationContext());
        callbackManager = CallbackManager.Factory.create();


    public void openFB() {
            LoginManager.getInstance().logInWithReadPermissions(activity,
                    Arrays.asList("read_stream", "user_photos", "email", "user_location"));

            // Login Callback registration
            LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {

                @Override
                public void onSuccess(final LoginResult loginResult) {
                    new GraphRequest(AccessToken.getCurrentAccessToken(),
                            "/me", null, HttpMethod.GET,
                            new GraphRequest.Callback() {
                                public void onCompleted(
                                        GraphResponse response) {
                                    /* handle the result */

                                    try {
                                        //GET USER INFORMATION
                                        JSONObject json = response.getJSONObject();
                                        String email = json.getString("email");
                                        String fullName = json.getString("name");
                                        String accessToken = loginResult.getAccessToken().getToken();
                                        int type = 1;
                                        String lastUpdate = json.getString("updated_time");
                                        String user_id = json.getString("id");

                                    } catch (JSONException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                }
                            }).executeAsync();

                    GetSnsPost getSnsPost = GetSnsPost.getInstance(activity);
                    getSnsPost.getFacebookPosts();

                }

                @Override
                public void onCancel() {

                }

                @Override
                public void onError(FacebookException exception) {

                }
            });

        }

     public void loginFacebook(View v){
           openFB();
     }
     protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
        callbackManager.onActivityResult(requestCode, resultCode, data);

    }
    }

Since session have been deprecated long time ago, I don't use it anymore. I get user information this way. Hope this code will solve your problem ;)

Bundle params = new Bundle(); params.putString("fields", "id,name,email,birthday,first_name,last_name");

new GraphRequest( AccessToken.getCurrentAccessToken(), AccessToken.getCurrentAccessToken().getUserId(), params, HttpMethod.GET, new GraphRequest.Callback() {

                                @Override
                                public void onCompleted(
                                        GraphResponse response) {

                                    System.out.println("\n J S O N :"
                                            + response.toString());


                                    } catch (JSONException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                }
                            }).executeAsync();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM