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.
Related
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.
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();
}
});
In Android I wonder how to get a Facebook user id number after he has signed in to my app. I tried this:
com.facebook.Profile.getCurrentProfile().getId()
But for some reason that give me a number but it´s not the user id.
When I go to sites like https://findmyfbid.com/ I get the correct id like "23982739223233". Even when I right click my Facebook profile picture in web browser and copy link the "referrer_profile_id=23982739223233" in the link is correct.
But when I sign in using that Facebook this Profile.getCurrentProfile().getId() gives me a completely different number
You can use GraphRequest
in code:
List<String> permissionNeeds = Arrays.asList(
"public_profile", "email", "user_birthday", "user_friends");
loginButton.setReadPermissions(permissionNeeds);
loginButton.registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
try {
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
// Application code
try {
Log.v("FACEBOOK LOGIN", response.toString());
String fb_id = object.getString("id"); //FaceBook User ID
String fb_name = object.getString("name");
String fb_email = object.getString("email");
String profilePicUrl = "https://graph.facebook.com/" + fb_id + "/picture?width=200&height=200";
} catch (Exception e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,picture.type(small)");
request.setParameters(parameters);
request.executeAsync();
} catch (Exception e) {
Log.d("ERROR", e.toString());
}
}
#Override
public void onCancel() {
Log.d("FACEBOOK ERRROR", "cancelled");
}
#Override
public void onError(FacebookException exception) {
Log.d("FACEBOOK ERRROR", exception.toString());
}
});
In Graph Request Response you can get user's Facebook Id, name , picture and other relevant information
ok I found the Answer. Looks like there is no common id for a Facebook user but my app get assigned a unique id as soon as user accept my app. Also here
This id is what i see with
com.facebook.Profile.getCurrentProfile().getId()
I'm trying to build a simple login to facebook app (with FB api v4.0)and retrieve some data about the user, but for some reason I success to login and get data only from my personal facebook profile, if tried to login with other users profile I'm getting null. (I change the setting in facebook developer to public app).
this is some of my loginButton_Fragment:
private CallbackManager callbackManager;
private FacebookCallback<LoginResult> facebookCallback = new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();
Log.d("koko",accessToken.toString() );
if (profile != null){
GraphRequest request = GraphRequest.newMeRequest(
accessToken,
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
try {
user_birthDay = object.getString("birthday");
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,birthday");
request.setParameters(parameters);
request.executeAsync();
user_name = profile.getName();
user_id = profile.getId();
Log.d("koko","first " +user_name + " " + user_id + " " + user_birthDay );
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
comm.respone(user_name, user_id, user_birthDay);
}
}, 500);
}
}
#Override
public void onCancel () {
}
#Override
public void onError (FacebookException e){
}
};
Each time I login, I'm getting inside the onSuccess, but if its not my personal profile so the profile its null.
set read permissions mButtonLogin.setReadPermissions(Arrays.asList("email,public_profile","user_birthday"));
try like this
mButtonLogin.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Log.d("status", "onSuccess");
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();
GraphRequest request = GraphRequest.newMeRequest(
accessToken,
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
// Application code
try {
String name = object.getString("name");
String email =object.getString("email");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "name,email,birthday");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
Log.d("status", "onCancel");
}
#Override
public void onError(FacebookException e) {
Log.d("status", "onError " + e);
}
}
);
You need to acquire app specific permission from Facebook for retrieve user personal information's such as birthday, user_friends etc by submitting it for a review.
Invest few moments here .
If your app requests such permissions Facebook will have to review how your app uses it.
You will get public_profile as it's default .
Check this & this .
My application allows the user to connect with Facebook. I want to get userprofile information after login with Facebook (using Facebook SDK 4.0.1). I think that I should use aynctask to get it but I don't know how.
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>()
{
#Override
public void onSuccess(LoginResult loginResult)
{
System.out.println("onSuccess");
msginfo.setText("You can now share image on facebook");
otherview.setVisibility(View.VISIBLE);
GraphRequest request = GraphRequest.newMeRequest
(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback()
{
#Override
public void onCompleted(JSONObject object, GraphResponse response)
{
// Application code
Log.v("LoginActivity", response.toString());
//System.out.println("Check: " + response.toString());
try
{
String id = object.getString("id");
idT.setText(object.getString("id"));
ppv.setProfileId(object.getString("id"));
nameT.setText(object.getString("name"));
emailT.setText(object.getString("email"));
locationT.setText(object.getString("address"));
String name = object.getString("name");
String email = object.getString("email");
String gender = object.getString("gender");
String birthday = object.getString("birthday");
// String location = object.getString("location");
// String location = object.getString("user_location");
// String location = object.getString("address");
System.out.println(id + ", " + name + ", " + email + ", " + gender + ", " + birthday);
// locationT.setText(location);
}
catch (JSONException e)
{
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender, birthday,link");
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());
}
});
And I can get the profile informations in the current activity but not in the next one.
I think you are missing permissions
parameters.putString("fields", "id,name,email,gender,picture,birthday");
And then you can easily get profile pic
object.getJSONObject("picture").getJSONObject("data").getString("url");
Hope it helps.