Facebook auth with firebase get User data - android

I am trying to use Firebase authentication with Facebook.
I have successfully implemented the login part and stuck while getting the user details from facebook using accessToken.
Here is the code:
loginButton.setReadPermissions("email", "public_profile" );
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(final LoginResult loginResult) {
Log.d(TAG, "facebook:onSuccess:" + loginResult);
GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject me, GraphResponse response) {
Log.e("FBRESPONSE",me.toString());
if (response.getError() != null) {
// handle error
} else {
if (me.has("picture")) {
try {
f_photo = me.getJSONObject("picture").getJSONObject("data").getString("url");
} catch (JSONException e) {
e.printStackTrace();
}
}
f_email = me.optString("email");
String id = me.optString("id");
f_name=me.optString("first_name")+" "+me.optString("last_name");;
handleFacebookAccessToken(loginResult.getAccessToken());
}
}
}).executeAsync();
}
});
This is the JSON response i get when called this.
E/FBRESPONSE: {"name":"Vishnu Reddy","id":"855235691328214"}
How do i get email, profile picture URL?
Please help. Thanks in advance.

You need to use the Facebook Graph API. You need to have your user signed in and call the AccessToken.refreshCurrentAccessTokenAsync(); and initiate a request to the user data-fields you need.

Related

How to get signed in Facebook user id number in Android

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()

Profile.getCurrentProfile() return only my profile, all the rest are null

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 .

Cannot get the facebook's user's timeline with api 2.5

I'm trying to get the facebook's user's timeline in my Android app.
Here my code :
mLoginButton.setReadPermissions(Arrays.asList("user_about_me", "user_friends", "user_likes",
"user_photos", "user_relationships", "user_posts",
"user_status"));
// If using in a fragment
mLoginButton.setFragment(this);
// Other app specific specialization
// Callback registration
mLoginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
mAccessToken = loginResult.getAccessToken();
for (String permission : loginResult.getRecentlyGrantedPermissions()) {
Log.d(LOG_TAG, "Granted Permission:" + permission);
}
getUserFeed();
}
#Override
public void onCancel() {
// App code
}
#Override
public void onError(FacebookException exception) {
// App code
}
});
And after the login, I launch this :
private void getUserFeed() {
Bundle params = new Bundle();
params.putInt("limit", 25);
params.putString("fields", "id,name,link,full_picture,message,story,picture,type,place,from,to");
params.putBoolean("summary", true);
/* make the API call */
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/me/home",
params,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
try {
final JSONArray data = response.getJSONObject().getJSONArray("data");
//currentJson = response.getJSONObject();
} catch (JSONException e) {
Log.e("Error: ", e.toString());
}
}
}
).executeAsync();
}
I have this respond code from Facebook :
Requires extended permission: read_stream
I know this permission is depreceted, I'm using the latest API 2.5.
Do you know if we can continue to get the user's timeline now, if I replace the "/me/home" by "/me/feed" it's ok, but I just get my posts, not my entire timeline.
Thanks :)
Do you know if we can continue to get the user's timeline now,
No, you can’t.
if I replace the "/me/home" by "/me/feed" it's ok, but I just get my posts, not my entire timeline.
/me/home was deprecated together with the permission.
/me/feed is what you can get now, and that’s it.
Which posts you can expect to see is listed here: https://developers.facebook.com/docs/graph-api/reference/v2.5/user/feed#readperms

Android Facebook API - I receive emails only for some fb accounts

I have been using the Facebook SDK for Android a bit now. I have been having trouble where one of my testers could not create an account in my app using Facebook. After some debugging I figured out that for some reason when he tries to login / register the JSON object that contains the information does not come with his email. This only happens for HIS account!
Does anyone know why this could happen?
(I have included a simplified version of my login code so you can see if there is anything wrong with it that might cause it)
List<String> permissionNeeds = Arrays.asList("public_profile", "user_friends", "email");
loginButton.setReadPermissions(permissionNeeds);
loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(final LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
try {
Log.d(TAG, object.toString()); // Has no email sometimes!
} catch (JSONException e) {
e.printStackTrace();
Log.e(TAG, String.format("Error with facebook login: %s", e.getMessage()));
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,picture,email,gender");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException error) {
Log.d(TAG, String.format("Error with facebook login: %s", error.getMessage()));
}
});
loginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LoginManager.getInstance().logInWithReadPermissions(SplashActivity.this,
Arrays.asList("public_profile", "user_friends", "email"));
}
});
If any more information is needed, please tell me I'll be happy to add some.
Thanks!
So turns out that my tester had an inactive email account.
In general you should never be counting on every Facebook user having an email address.

Facebook user profile image + sdk 4.0

A bit puzzled with this...
I am using LoginButton class to login via facebook in my app.
I have registered a callback loginButton.registerCallback and I can get login successfully with it, this all is done in LoginActivity class of my app.
As shown below, in onSuccess method of callback, I call getFaceBookProfileDetails method, defined locally to call Graph API to fetch user details, viz name, profile picture etc, this too works well .
onSuccess(LoginResult loginResult) {
getFaceBookProfileDetails(loginResult.getAccessToken());
startActivity(intentHomeActivity)
}
Now the issue is I want to pass this facebook data to my HomeActivity where I will display this on UI.
As the graph API call runs Async on non-UI thread, I am getting Null pointer when I try to fetch this data in HomeActivity.
Looked at many work-arounds for this with no luck.
Any help would be appreciated.
facebookButton = (LoginButton) findViewById(R.id.login_button);
facebookButton.setReadPermissions(Arrays.asList("public_profile",
"email", "user_birthday"));
intentHomeActivity = new Intent(this, HomeActivity.class);
facebookButton.registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
getFaceBookProfileDetails(loginResult.getAccessToken());
startActivity(intentHomeActivity);
}
public void onCancel() {
}
#Override
public void onError(FacebookException e) {
}
});
private void getFaceBookProfileDetails(final AccessToken accessToken) {
GraphRequest request = GraphRequest.newMeRequest(
accessToken,
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(
final JSONObject object,
GraphResponse response) {
try {
Toast.makeText(getApplicationContext(), "User Name
is --"+object.get("name"), Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "User ID is
--"+object.get("id"), Toast.LENGTH_LONG).show();
ProfilePictureView profilePictureView = new
ProfilePictureView(getApplicationContext());
intentHomeActivity.putExtra(Constants.FACEBOOK_USER_OBJECT,
object.get("id").toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
});
request.executeAsync();
Move this line from the onSuccess() method:
startActivity(intentHomeActivity);
To the onCompleted() method. After:
intentHomeActivity.putExtra(Constants.FACEBOOK_USER_OBJECT, object.get("id").toString());
The onComplete() method would look like this:
#Override
public void onCompleted( final JSONObject object, GraphResponse response) {
try {
Toast.makeText(getApplicationContext(), "User Name is --" + object.get("name"), Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "User ID is --" + object.get("id"), Toast.LENGTH_LONG).show();
ProfilePictureView profilePictureView = new ProfilePictureView(getApplicationContext());
intentHomeActivity.putExtra(Constants.FACEBOOK_USER_OBJECT, object.get("id").toString());
startActivity(intentHomeActivity);
} catch (JSONException e) {
e.printStackTrace();
}
}

Categories

Resources