How to Get Email Id from Facebook Sdk in Android Applications

how to get email id from facebook sdk in android applications?

Before calling Session.openActiveSession do this to get permissions add this:

List<String> permissions = new ArrayList<String>();
permissions.add("email");

The last parameter in Session.openActiveSession() should be permissions.
Now you can access user.getProperty("email").toString().

EDIT:

This is the way I am doing facebook authorization:

List<String> permissions = new ArrayList<String>();
permissions.add("email");

loginProgress.setVisibility(View.VISIBLE);

//start Facebook session
openActiveSession(this, true, new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
//make request to the /me API
Log.e("sessionopened", "true");
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
String firstName = user.getFirstName();
String lastName = user.getLastName();
String id = user.getId();
String email = user.getProperty("email").toString();

Log.e("facebookid", id);
Log.e("firstName", firstName);
Log.e("lastName", lastName);
Log.e("email", email);
}
}
});
}
}
}, permissions);

Add this method to your activity:

private static Session openActiveSession(Activity activity, boolean allowLoginUI, Session.StatusCallback callback, List<String> permissions) {
Session.OpenRequest openRequest = new Session.OpenRequest(activity).setPermissions(permissions).setCallback(callback);
Session session = new Session.Builder(activity).build();
if (SessionState.CREATED_TOKEN_LOADED.equals(session.getState()) || allowLoginUI) {
Session.setActiveSession(session);
session.openForRead(openRequest);
return session;
}
return null;
}

Facebook Android SDK 4.5.0 get email address

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:

parameters.putString("fields", "id, first_name, last_name, email,gender, birthday, location"); // Parámetros que pedimos a facebook

btnLoginFb.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {

@Override
public void onSuccess(LoginResult loginResult) {

System.out.println("onSuccess");
progressDialog = new ProgressDialog(LoginActivity.this);
progressDialog.setMessage("Procesando datos...");
progressDialog.show();
String accessToken = loginResult.getAccessToken().getToken();
Log.i("accessToken", accessToken);

GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {

@Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.i("LoginActivity", response.toString());
// Get facebook data from login
Bundle bFacebookData = getFacebookData(object);
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id, first_name, last_name, email,gender, birthday, location"); // Parámetros que pedimos a facebook
request.setParameters(parameters);
request.executeAsync();
}

@Override
public void onCancel() {
System.out.println("onCancel");
}

@Override
public void onError(FacebookException exception) {
System.out.println("onError");
Log.v("LoginActivity", exception.getCause().toString());
}
});

private Bundle getFacebookData(JSONObject object) {

try {
Bundle bundle = new Bundle();
String id = object.getString("id");

try {
URL profile_pic = new URL("https://graph.facebook.com/" + id + "/picture?width=200&height=150");
Log.i("profile_pic", profile_pic + "");
bundle.putString("profile_pic", profile_pic.toString());

} catch (MalformedURLException e) {
e.printStackTrace();
return null;
}

bundle.putString("idFacebook", id);
if (object.has("first_name"))
bundle.putString("first_name", object.getString("first_name"));
if (object.has("last_name"))
bundle.putString("last_name", object.getString("last_name"));
if (object.has("email"))
bundle.putString("email", object.getString("email"));
if (object.has("gender"))
bundle.putString("gender", object.getString("gender"));
if (object.has("birthday"))
bundle.putString("birthday", object.getString("birthday"));
if (object.has("location"))
bundle.putString("location", object.getJSONObject("location").getString("name"));

return bundle;
}
catch(JSONException e) {
Log.d(TAG,"Error parsing JSON");
}
return null;
}

How to get email id From Android Facebook SDK 4.6.0?

LoginManager.getInstance().logInWithReadPermissions(WelcomeActivity1.this, (Arrays.asList("public_profile", "user_friends","user_birthday","user_about_me","email")));

String email;

LoginManager.getInstance().registerCallback(callbackManager,new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.d("tag","FF fb onSuccess");
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(),new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object,GraphResponse response) {
try {
String[] splited ;
JSONObject obj = object.getJSONObject("picture").getJSONObject("data");

if (object.has("email"))
{
email = object.getString("email");
}
else
{
email = "";
}

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

}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link,birthday,picture,email,gender");
request.setParameters(parameters);
request.executeAsync();

}

@Override
public void onCancel() {
Log.d("tag","fb onCancel");
// App code
}

@Override
public void onError(FacebookException exception) {
Log.d("tag","fb onError");
// App code
}
});

how to get email from Facebook sdk 4.0

Try This It's Work For me

 login.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {

if (AccessToken.getCurrentAccessToken() != null) {
RequestData();
}
}

@Override
public void onCancel() {

}

@Override
public void onError(FacebookException exception) {
}
});

private void RequestData() {

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

final JSONObject json = response.getJSONObject();

try {
if(json != null){
text = "<b>Name :</b> "+json.getString("name")+"<br><br><b>Email :</b> "+json.getString("email")+"<br><br><b>Profile link :</b> "+json.getString("link");
/*details_txt.setText(Html.fromHtml(text));
profile.setProfileId(json.getString("id"));*/

Log.e(TAG, json.getString("name"));
Log.e(TAG, json.getString("email"));
Log.e(TAG, json.getString("id"));
//web.loadData(text, "text/html", "UTF-8");

}

} catch (JSONException e) {
e.printStackTrace();
}
}
});

Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link,email,picture");
request.setParameters(parameters);
request.executeAsync();
}

//Get Profile Picture from id

 public static Bitmap getFacebookProfilePicture(String userID){
try {
URL imageURL = new URL("https://graph.facebook.com/" + userID + "/picture?type=large");
Log.e(TAG,imageURL.toString());
try {
bitmap = BitmapFactory.decodeStream(imageURL.openConnection().getInputStream());
} catch (IOException e) {
e.printStackTrace();
}

} catch (MalformedURLException e) {
e.printStackTrace();
}

return bitmap;
}

Android - how to get user email with facebook sdk 4+

Try This

protected void connectToFacebook() {
ArrayList<String> list = new ArrayList<String>();
list.add("email");
// LoginManager.getInstance().logInWithReadPermissions(this, list);
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("email", "user_photos", "public_profile"));

LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
// App code
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject json, GraphResponse response) {
// Application code
if (response.getError() != null) {
System.out.println("ERROR");
} else {
System.out.println("Success");
String jsonresult = String.valueOf(json);
System.out.println("JSON Result" + jsonresult);

String fbUserId = json.optString("id");
String fbUserFirstName = json.optString("name");
String fbUserEmail = json.optString("email");
String fbUserProfilePics = "http://graph.facebook.com/" + fbUserId + "/picture?type=large";
callApiForCheckSocialLogin(fbUserId, fbUserFirstName, fbUserEmail, fbUserProfilePics, "fb");
}
Log.v("FaceBook Response :", response.toString());
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender, birthday");
request.setParameters(parameters);
request.executeAsync();

}

@Override
public void onCancel() {
// App code
Log.v("LoginActivity", "cancel");
}

@Override
public void onError(FacebookException exception) {
// App code
// Log.v("LoginActivity", "" + exception);
Utilities.showToast(mActivity, "" + exception);
}
});
}

Android Facebook SDK 4.X , how to get Email address and Facebook Access Token to pass it to Web Service

fbLoginBtn.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");
String id = me.optString("id");
// send email and id to your web server
}
}
}).executeAsync();
}

@Override
public void onCancel() {
// App code
}

@Override
public void onError(FacebookException exception) {
// App code
}
});


Related Topics



Leave a reply



Submit