I'm currently creating a test application to test using the latest facebook SDK to update our existing application problem is that I need to get the email address which I know depends if the user has provided one on his account. Now the account I'm using to test provides one for sure but for unknown reason the facebook SDK only provides the user_id and the fullname of the account and nothing else. I'm confused on this since the the SDK3 and above provides more information than the updated SDK4 and I'm lost on how to get the email as all the answers I've seen so far doesn't provide the email on my end. Here's my code so far:
Login Button
@OnClick(R.id.btn_login)
public void loginFacebook(){
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "email"));
}
LoginManager Callback:
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
requestUserProfile(loginResult);
}
@Override
public void onCancel() {
Toast.makeText(getBaseContext(),"Login Cancelled", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(FacebookException e) {
Toast.makeText(getBaseContext(),"Problem connecting to Facebook", Toast.LENGTH_SHORT).show();
}
});
And the request for user profile:
public void requestUserProfile(LoginResult loginResult){
GraphRequest.newMeRequest(
loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject me, GraphResponse response) {
if (response.getError() != null) {
// handle error
} else {
try {
String email = response.getJSONObject().get("email").toString();
Log.e("Result", email);
} catch (JSONException e) {
e.printStackTrace();
}
String id = me.optString("id");
// send email and id to your web server
Log.e("Result1", response.getRawResponse());
Log.e("Result", me.toString());
}
}
}).executeAsync();
}
The JSON response returns only the ID and full name of my account but doesn't include the email. Did I missed out something?
If you are giving any permission to get an email address. then, Facebook can not every time gives you email address or some other details.
when, Facebook user is not signup with his email-address.then Facebook can't provide you his email address.
if user is login with Facebook using email address then Facebook is provide email to you.
Facebook SDK provide only public details.
check a login with Facebook using email-address and also with phone number through.
You can also use GraphResponse object to get the values
Putting this code:
before
should solve the problem.
Hope it helps you
I guess getting email permissions can help..
You need to ask for parameters to facebook in order to get your data. Here I post my function where I get the facebook data. The key is in this line:
Hope it helps you.