I am using the following code. I want the user's Date Of Birth, Email and Gender. Please help. How to retrieve those data?
This is my onViewCreated()
inside the Fragment.
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// Setup TextView.
mTextDetails = (TextView) view.findViewById(R.id.text_details);
// Set up Login Button.
LoginButton mButtonLogin = (LoginButton) view.findViewById(R.id.login_button);
// setFragment only if you are using it inside a Fragment.
mButtonLogin.setFragment(this);
mButtonLogin.setReadPermissions("user_friends");
mButtonLogin.setReadPermissions("public_profile");
mButtonLogin.setReadPermissions("email");
mButtonLogin.setReadPermissions("user_birthday");
// Register a callback method when Login Button is Clicked.
mButtonLogin.registerCallback(mCallbackManager, mFacebookCallback);
}
This is my Callback Method.
private FacebookCallback<LoginResult> mFacebookCallback = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.d("Shreks Fragment", "onSuccess");
Profile profile = Profile.getCurrentProfile();
Log.d("Shreks Fragment onSuccess", "" +profile);
// Get User Name
mTextDetails.setText(profile.getName() + "");
}
@Override
public void onCancel() {
Log.d("Shreks Fragmnt", "onCancel");
}
@Override
public void onError(FacebookException e) {
Log.d("Shreks Fragment", "onError " + e);
}
};
Add this line on Click on button
loginButton.setReadPermissions(Arrays.asList( "public_profile", "email", "user_birthday", "user_friends"));
Following is the code to find email id, name and profile url etc
This is worked for me, Hope to help someone (Using my own button not FB login button )
You won't get Profile in
onSuccess()
you need to implementProfileTracker
along with registering callbackAlso don't forget to handle the start and stop of profile tracker
Now you will have a profile to get AccessToken from (solved the issue of null profile). You just have to use "https://developers.facebook.com/docs/android/graph#userdata" to get any data.
You can use the GraphRequest class to issue calls to the Facebook Graph API to get user information. See https://developers.facebook.com/docs/android/graph for more info.
That's not the right way to set the permissions as you are overwriting them with each method call.
Replace this:
With the following, as the method
setReadPermissions()
accepts an ArrayList:Also here is how to query extra data GraphRequest:
EDIT:
One possible problem is that Facebook assumes that your email is invalid. To test it, use the Graph API Explorer and try to get it. If even there you can't get your email, change it in your profile settings and try again. This approach resolved this issue for some developers commenting my answer.