Facebook android SDK 3 get email from user - android

Here is my code for facebook me request.i want to get user email and other basic info.this code is work without any issue in emulator.but in real device email gives null value. id,fristname... comes with real values. how can i get email on real device?
Request request = Request.newMeRequest(session,
new Request.GraphUserCallback() {
#Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
RequestParams params = new RequestParams();
params.put("email",(String) user.getProperty("email"));
params.put("password", "");
params.put("facebook_id", user.getId());
editor.putString("facebook_id", user.getId());
login(params,
(String) user.asMap().get("email"),
user.getId(), user.getFirstName(),
user.getLastName());
}
}
});
Bundle params = new Bundle();
params.putString("fields", "id,email,first_name,last_name");
request.setParameters(params);
request.executeAsync();

Instead of submitting a ME request, use Facebook's LoginButton, and add a hook to intercept the user info:
// Intercept the facebook user returned from login
facebookLoginButton.setUserInfoChangedCallback(new LoginButton.UserInfoChangedCallback() {
#Override
public void onUserInfoFetched(GraphUser user) {
mFacebookUser = user;
if (user != null) {
LogUtils.LOGFB(TAG, "Got a Facebook user: " + user.getFirstName() +
" " + user.getLastName() + ", email: " + user.getProperty("email"));
}
else {
LogUtils.LOGFB(TAG, "No Facebook user");
}
}
});
Then add an extra permission to get email:
facebookLoginButton.setReadPermissions(Arrays.asList(
"email", "user_birthday", ...));

Related

Does anyone know how to get firstname, lastname, email, id, birthday, gender, hometown, age and etc from facebook sdk in android platform?

I am new for facebook sdk.
Does any know how to get firstname, lastname, email, id, birthday, gender, hometown, age and etc from facebook sdk in android platform?
Please show me the code?
below is what I have tried
callbackManager = CallbackManager.Factory.create();
LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions( "public_profile", "email", "user_birthday", "user_friends");
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = AccessToken.getCurrentAccessToken();
boolean isLoggedIn = accessToken != null && !accessToken.isExpired();
GraphRequest request = GraphRequest.newMeRequest(
accessToken,
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
// Insert your code here
try {
if (object != null) {
JSONObject obj = response.getJSONObject();
id = obj.getString("id");
name = obj.getString("name");
birthday = obj.getString("birthday");
email = obj.getString("email");
String gender = obj.getString("gender");
showUserInfoToast();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,email,first_name,last_name,gender");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
// App code
}
#Override
public void onError(FacebookException exception) {
// App code
}
});
I tried but it does not work....
please help
Please try this code .Hope it will fetch all the data you needed.Thanx
public void onSuccess(final LoginResult loginResult) {
Log.e("bug", "facebook sign in success");
if (loginResult != null) {
Profile profile = Profile.getCurrentProfile();
if (profile != null) {
firstName = profile.getFirstName();
lastName = profile.getLastName();
social_id = profile.getId();
profileURL = String.valueOf(profile.getProfilePictureUri(200, 200));
Log.e(TAG, "social Id: " + profileURL);
}
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.e("bug", "facebook social_id=>" + social_id + " toeken" + loginResult.getAccessToken().getToken());
Log.e("bug", "graph response=>" + object.toString());
try {
if (object.has("name")) {
String[] name = object.getString("name").split(" ");
firstName = name[0];
lastName = name[1];
}
email = object.has("email") ? object.getString("email") : "";
social_id = object.has("id") ? object.getString("id") : "";
socialSignUp("facebook");
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,first_name,name,last_name,email,gender,birthday");
request.setParameters(parameters);
request.executeAsync();
}
The data you access depends on the permissions someone grants your app and data they chose to share with apps (Link)
These fields can be null
To get any user-specific data other than userId, nameand email you have to submit your app for review to facebook console so that facebook will have clarity that what you want to do with user personal data.
If it is some sort of useful thing your app is doing with this data then facebook will allow you to access that data like birthday date, user_friends and user gender.
To do that go to facebook developer console and follow there procedure.
Hope this will help you to achieve what you want to achieve.

Facebook Android SDK 4.14.0 get birthday

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 user birthday. 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 birthday as all the answers I've seen so far doesn't provide the birthday on my end. Here's my code so far:
LoginButton CallBacks:
fbLogin = (LoginButton) findViewById(R.id.login_button);
fbLogin.setReadPermissions(Arrays.asList("email", "user_birthday"));
fbLogin.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 object, GraphResponse response) {
try {
if (object.has("id"))
id = object.getString("id");
if (object.has("name"))
userName = object.getString("name");
if (object.has("email"))
userEmail = object.getString("email");
if (object.has("gender"))
gender = object.getString("gender");
if (object.has("birthday"))
birthday = object.getString("birthday");
String profile_URL = "https://graph.facebook.com/" + id+ "/picture?type=large";
LogCat.show(id + "\n" + userName + "\n" + userEmail + "\n" + birthday + "\n" + profile_URL + "\n" + gender);
} catch (Exception e) {
e.printStackTrace();
LogCat.show("Error:" + e.getMessage());
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id, name, email,gender, birthday");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
// App code
Toast.makeText(SplashLogin.this, "CANCEL", Toast.LENGTH_SHORT).show();
}
#Override
public void onError(FacebookException exception) {
// App code
Toast.makeText(SplashLogin.this, "" + exception.toString(), Toast.LENGTH_SHORT).show();
}
});
The JSON response returns only the ID and name, email and gender of my account but doesn't include the birthday. Did I missed out something?
According to the latest sdk of facebook, to get birthday you need to first submit your application for review. On test applications you can just get the public profile which includes the following things
id
cover
name
first_name
last_name
age_range
link
gender
locale
picture
timezone
updated_time
verified
For more information you can read the following documentation from this link
If your app requests this permission Facebook will have to review how
your app uses it. link
Facebook don't give user birthday by default.You have to use extended permissions.
I solve this problem by enable permission of Birthday in my app from the Facebook Developer Console.
fbSignUp.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(final LoginResult loginResult) {
// App code
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
try {
Data.fbAccessToken = loginResult.getAccessToken().getToken();
LogCat.show(loginResult.getAccessToken().getToken());
if (object.has("id")) {
id = object.getString("id");
Data.fbId = object.getString("id");
}
if (object.has("name")) {
userName = object.getString("name");
;
Data.fbUserName = object.getString("name");
}
if (object.has("email")) {
userEmail = object.getString("email");
Data.fbUserEmail = object.getString("email");
}
if (object.has("birthday")) {
userBirthDay = object.getString("birthday");
}
else {
if (userEmail == null || "".equalsIgnoreCase(userEmail)) {
if (userName != null && !"".equalsIgnoreCase(userName)) {
userEmail = userName + "#facebook.com";
Data.fbUserEmail = Data.fbUserName + "#facebook.com";
} else {
userEmail = id + "#facebook.com";
Data.fbUserEmail = Data.fbId + "#facebook.com";
}
}
}
} catch (Exception e) {
e.printStackTrace();
LogCat.show("Error:" + e.getMessage());
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id, name, email,birthday,gender");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
// App code
Toast.makeText(RegisterScreen.this, "CANCEL", Toast.LENGTH_SHORT).show();
}
#Override
public void onError(FacebookException exception) {
// App code
Toast.makeText(RegisterScreen.this, "" + exception.toString(), Toast.LENGTH_SHORT).show();
}
});

Android Facebook GraphRequest Not Returning Email

I have looked at several posts regarding this issue and have not found a solution.
facebookLoginButton = (LoginButton) findViewById(R.id.facebook_login_button);
facebookLoginButton.setReadPermissions(Arrays.asList("read_stream", "user_photos", "email", "user_location"));
// create callback manager for facebook
callbackManager = CallbackManager.Factory.create();
facebookLoginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Log.d(TAG, "LOGGED IN");
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.d(TAG, "Facebook graph response: " + response.toString());
try {
// get only the part before the # symbol
String email_username = object.getString("email").substring(0, object.getString("email").indexOf("#"));
editor = preferences.edit();
editor.putString("username", email_username);
editor.commit();
Log.d(TAG, "logging in user " + preferences.getString("username", "") + " with userID: " + preferences.getString("userID", ""));
SpinLoginTask spinLoginTask = new SpinLoginTask(LoginActivity.this);
spinLoginTask.execute("facebook");
} catch (Exception e) {
e.printStackTrace();
}
}
});
request.executeAsync();
}
I am getting the following response:
Facebook graph response: {Response: responseCode: 200, graphObject: {"name":"Spin Tester","id":"xxxxx"}, error: null}
org.json.JSONException: No value for email
Since I set read permissions on the button to provide all information, why is none of it appearing in the Graph request response?
Try this code I have always used: EDIT
Actually, you need to change a small part of your code:
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.d(TAG, "Facebook graph response: " + response.toString());
try {
// get only the part before the # symbol
String email_username = object.getString("email").substring(0, object.getString("email").indexOf("#"));
editor = preferences.edit();
editor.putString("username", email_username);
editor.commit();
Log.d(TAG, "logging in user " + preferences.getString("username", "") + " with userID: " + preferences.getString("userID", ""));
SpinLoginTask spinLoginTask = new SpinLoginTask(LoginActivity.this);
spinLoginTask.execute("facebook");
} catch (Exception e) {
e.printStackTrace();
}
}
});
//ADD THIS LINES PLEASE
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email");
request.setParameters(parameters);
request.executeAsync();
}
Users have the option to deny access to their email address on first login via the Facebook provided UI. I would do a check to ensure that the email is in the JSON structure returned. If you require the email address, bring an AlertDialog or some other UI to ask them for their email. Make sure you have logic in place in case they refuse to provide email, since that is always a possibility too.

'user_birthday' not found in GraphUser - android facebook sdk

Permission was set in Facebook application dashboard to access user_birthday. In application user_birthday permission is listed in permission request.
request.setPermissions(Arrays.asList("public_profile", "email", "user_birthday", "user_work_history"));
In the JSON response after connecting with facebook, the tag firstname, middlename, lastname, email,and id was output but the json has no birthday tag. What have i missed
Request request = Request.newMeRequest(session, new Request.GraphUserCallback() {
#Override
public void onCompleted(GraphUser graphUser, Response response) {
Log.i(TAG, "LoggedInUser:" + graphUser);
Log.i(TAG, "LoggedInUser_Id:" + graphUser.getId());
Log.i(TAG, "LoggedInUser_First:" + graphUser.getFirstName());
Log.i(TAG, "LoggedInUser_Middle:" + graphUser.getMiddleName());
Log.i(TAG, "LoggedInUser_Last:" + graphUser.getLastName());
Log.i(TAG, "LoggedInUser_Email:" + graphUser.asMap().get("email"));
Log.e(TAG, "LoggedInUser_BIRTHDAY:" + graphUser.asMap().get("user_birthday"));
String email = (String) graphUser.asMap().get("email");
if(email==null || email.isEmpty())
Toast.makeText(LogIn.this, "Failed to get facebook credentials",
Toast.LENGTH_LONG).show();
else
editEmail.setText(email);
firstName = graphUser.getFirstName();
lastName = graphUser.getLastName();
}
});
use this code
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object,
GraphResponse response) {
String email = object
.optString("email");
String dob = object
.optString("birthday");
// user_id = object.optString("id");
String name = object.optString("name");
Log.d("fb detail", "" + email + " "
+ name + " " + dob);
new LoginAsynTask(context).execute(
"social", name, email, dob,
object.optString("first_name"),
object.optString("last_name"));
Toast.makeText(context, "" + email,
Toast.LENGTH_LONG).show();
}
});
Bundle parameters = new Bundle();
parameters
.putString("fields",
"id,name, email, birthday,gender,first_name,last_name");
request.setParameters(parameters);
request.executeAsync();

Why I get null for user friend's birthday in android app that use facebook api?

I write this code in my android app i can get the user friends name but i cannot get their birthday what should I do?I think that I have problem with permissions but I don;t know where.
Session s = new Session(this);
Session.setActiveSession(s);
Session.OpenRequest request = new Session.OpenRequest(this);
request.setPermissions(Arrays.asList("friends_birthday","user_friends","public_profile"));
request.setCallback( new Session.StatusCallback() {
#Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
Request myFriendsRequest = Request.newMyFriendsRequest(session, new Request.GraphUserListCallback() {
#Override
public void onCompleted(List<GraphUser> users, Response response) {
if (users != null) {
TextView welcome = (TextView) findViewById(R.id.welcome);
for (GraphUser user : users) {
// Here I can get the friend name
welcome.setText(welcome.getText() + "Hello " + user.getName() + "!");
// But here I cannot get birthday and it returns null
Toast.makeText(MyContext, user.getBirthday(), Toast.LENGTH_SHORT).show();
}
}
}
});
Bundle requestParams = myFriendsRequest.getParameters();
requestParams.putString("fields", "name,birthday");
myFriendsRequest.setParameters(requestParams);
myFriendsRequest.executeAsync();
}
}
});
s.openForRead(request);
That's because all friends_* permissions have been removed with Graph AP v2.0. You can't access their data anymore, except name and profile picture.
See
https://developers.facebook.com/docs/apps/changelog#v2_0_permissions

Categories

Resources