Meng
Meng

Reputation: 143

Android get facebook email address

i have done facebook login but i dont know how to get email address and display it in another form

So, this is the code

private void nextActivity(Profile profile){
    if(profile != null){
        Intent main = new Intent(LogInTo.this, SetUsername.class);
        main.putExtra("name", profile.getFirstName());
        main.putExtra("surname", profile.getLastName());
        //main.putExtra("email", profile.get());
        main.putExtra("imageUrl", 
        profile.getProfilePictureUri(200,200).toString());
        startActivity(main);
        finish();
    }
}

This is my callback

    LoginButton loginButton = (LoginButton)findViewById(R.id.loginButton);
    FacebookCallback<LoginResult> callback = new 
    FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            Profile profile = Profile.getCurrentProfile();
            nextActivity(profile);
            Toast.makeText(getApplicationContext(), "Logging in...", Toast.LENGTH_SHORT).show();

         }

        @Override
        public void onCancel() {
        }

        @Override
        public void onError(FacebookException e) {
        }
    };
    loginButton.setReadPermissions(Arrays.asList("user_friends"));
    loginButton.registerCallback(callbackManager, callback);

And this is the code in another form where the email address you get from facebook is shown

    Bundle inbundle = getIntent().getExtras();
    String name = inbundle.get("name").toString();
    String surname = inbundle.get("surname").toString();

    FbFullname = (EditText) findViewById(R.id.fbfullname);
    FbFullname.setText("" + name + " " + surname);

    FbUsername = (EditText) findViewById(R.id.fbusername);
    FbType = (EditText) findViewById(R.id.fbtype);
    FbEmail = (EditText) findViewById(R.id.fbemail);

Upvotes: 0

Views: 3605

Answers (1)

D.J
D.J

Reputation: 1559

loginButton.setReadPermissions("public_profile email");` 

add this line after create loginButton.

and for getting detail

GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
        @Override
        public void onCompleted(JSONObject object, GraphResponse response) {

            JSONObject json = response.getJSONObject();
            try {
                if (json != null) {

                    JSONObject data = json.getJSONObject("picture").getJSONObject("data");
                    String name=json.getString("name");
                    String email= json.getString("email");
                    String picUrl=data.getString("url");
                }

            } catch (JSONException e) {

            }
        }
    });

Upvotes: 2

Related Questions