Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I need user's email address after successful facebook login in android using SDK 4.0

I have integrated latest Facebook android sdk 4.0. In SDK 3.0+ user's email address is retreived using user.getProperty("email") after successful login. I am looking for corresponding command in Facebook Android sdk 4.0 Reference Links:

https://developers.facebook.com/docs/facebook-login/android/v2.3#overview https://developers.facebook.com/docs/android/upgrading-4.x

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
    callbackManager = CallbackManager.Factory.create();
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    loginButton = (LoginButton) view.findViewById(R.id.login_button);
    loginButton.setReadPermissions("email", "user_likes", "user_friends");
    loginButton.setFragment(this);
    setFacebookLoginText(loginButton);
    // Other app specific specialization

    // Callback registration
    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            AccessToken accessToken = loginResult.getAccessToken();
            GraphRequest request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(JSONObject user, GraphResponse graphResponse) {

                  //Need User email address after login success.

                }
            }).executeAsync();
        }

        @Override
        public void onCancel() {
            Toast.makeText(getActivity(), "fail", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(FacebookException exception) {
            Toast.makeText(getActivity(), "error", Toast.LENGTH_SHORT).show();
        }
    });

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_facebook_login, container, false);
    return view;
}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);
}
like image 996
Devanshu Dwivedi Avatar asked Apr 01 '15 16:04

Devanshu Dwivedi


3 Answers

Yeah, it worked. The only thing required was to change GraphRequest to GraphRequestAsyncTask in onSuccess method of FacebookCallBack, and then user details could easily be fetched from the JSONObject.

        @Override
        public void onSuccess(LoginResult loginResult) {
            final AccessToken accessToken = loginResult.getAccessToken();
            final FBUser fbUser = new FBUser();
            GraphRequestAsyncTask request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(JSONObject user, GraphResponse graphResponse) {
                    fbUser.setEmail(user.optString("email"));
                    fbUser.setName(user.optString("name"));
                    fbUser.setId(user.optString("id"));
                }
            }).executeAsync();
        }

FBUser Model Class

public class FBUser {
private String displayName;
private String email;

public FBUser(String displayName, String email) {
    this.displayName= displayName;
    this.email = email;
}

public FBUser() {

}

public String getName() {
    return displayName;
}

public void setName(String displayName) {
    this.displayName = displayName;
}


public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

}
like image 111
Devanshu Dwivedi Avatar answered Nov 14 '22 12:11

Devanshu Dwivedi


In the new facebook graph you need to ask permissions for access that information. for example on the activity you have put the LoginButton you add this line in the OnCreate method

loginButtonFacebook.setReadPermissions(Arrays.asList("public_profile", "email", "user_birthday"));

Then you get that information

loginButtonFacebook.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
    GraphRequest.newMeRequest(
        loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
            @Override
            public void onCompleted(JSONObject me, GraphResponse response) {
                if (response.getError() != null) {
                    // handle error
                } else {
                    String email = me.optString("email");
                }
            }
        }).executeAsync();
}
like image 39
agusgambina Avatar answered Nov 14 '22 11:11

agusgambina


This will give you all the information from facebook sdk 4.0.0

fbloginButton.setReadPermissions("email");

    fbloginButton.registerCallback(callbackmanager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            AccessToken accessToken = loginResult.getAccessToken();
           GraphRequest graphRequest=GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
               @Override
               public void onCompleted(JSONObject object, GraphResponse response) {

                   if (response.getError()!=null)
                   {
                       Log.e(TAG,"Error in Response "+ response);
                   }
                   else
                   {
                       email=object.optString("email");
                       Log.e(TAG,"Json Object Data "+object+" Email id "+ email);
                   }


               }
           });

            Bundle bundle=new Bundle();
            bundle.putString("fields","id,email,name");
            graphRequest.setParameters(bundle);
            graphRequest.executeAsync();

        }
like image 1
Sagar Gangawane Avatar answered Nov 14 '22 12:11

Sagar Gangawane