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
Related
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.
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()
Above is my code but i cant find out together usage, always gives error
Cannot pass a publish or manage permission (publish_actions) to a request for read authorization
This is my Permission list
private Collection<String> permissions = new ArrayList<>();
permissions.add("public_profile");
permissions.add("email");
permissions.add("user_birthday");
permissions.add("publish_actions");
And this is login request
ParseFacebookUtils.logInWithReadPermissionsInBackground(activity, permissions, new LogInCallback() {
#Override
public void done(ParseUser parseUser, ParseException parseException) {
if (parseUser == null) {
} else {
}
}
});
How can i use this together?
After long hours, this is solution. You must behave twice login to facebook. Once is publish and other one is read permissions. If you need public profile data , just publish permission is enough but in my case i need birthday, email, etc.. So code is below;
These are my permissions lists;
Collection<String> readPermissions = new ArrayList<>();
readPermissions.add("public_profile");
readPermissions.add("email");
readPermissions.add("user_birthday");
Collection<String> publishPermissions = new ArrayList<>();
publishPermissions.add("publish_actions");
Firstly, I should login with readpermission
ParseFacebookUtils.logInWithReadPermissionsInBackground(activity, readPermissions, new LogInCallback() {
#Override
public void done(ParseUser parseUser, ParseException parseException) {
if (parseUser == null) {
listener.onFailure(new UserCancelledFacebookLogin());
} else {
getPublishPermissions(parseUser);
}
}
});
After this, here my "getPublishPermissions" method; FacebookRequestListener is my own listener , don't care/mind delete it.
public void getPublishPermissions(final ParseUser parseUser) {
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
// User succesfully login with all permissions
// After this with these json and ParseUser , you can save your user to Parse
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,first_name,last_name,name,email,gender,birthday");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException facebookException) {
}
});
LoginManager.getInstance().logInWithPublishPermissions(activity, publishPermissions);
}
that's all folks =)
happy coding to everyone
The error message means you should not request read and write permissions at the same time. Login with read permissions when the User enters your App, request write permission (publish_actions) only right before you post.
Use ParseFacebookUtils.logInWithPublishPermissionsInBackground for that.
That error message is already well known, take a look at some other thread about it:
How to set permission "publish_actions" in LoginButton using facebook sdk?
Facebook, setReadPermissions and setPublishPermissions
facebook login Cannot pass a publish or manage permission (email) to a request for read authorization
You need to do a POST request instead of a GET one. See:
https://developers.facebook.com/docs/graph-api/reference/v2.2/user/scores/#publish
Sample:
Bundle param = new Bundle();
param.putString("message", "picture caption");
param.putByteArray("picture", ImageBytes);
mAsyncRunner.request("me/photos", param, "POST", new SampleUploadListener());
this is your answer check link
I used test app id and log on by test user create at dash_board app on facebook develop site, require pulish_actions permission when login using login button widget of facebook sdk but result get postid always = null.
Here is my code:
....
shareDialog = new ShareDialog(MainActivity.this);
shareDialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
#Override
public void onSuccess(Sharer.Result result) {
if (result.getPostId() != null)
Log.e(TAG, result.getPostId());
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException e) {
}
});
pulishButton.setOnClickListener(this);
try{
loginButton.setPublishPermissions(new String[]{"publish_actions","publish_stream"});
}catch (FacebookException e){
e.printStackTrace();
}
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Log.e(TAG, "success");
loginButton.setEnabled(false);
pulishButton.setEnabled(true);
GraphRequest.newMeRequest(
loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject json, GraphResponse response) {
if (response.getError() != null) {
// handle error
System.out.println("ERROR");
} else {
System.out.println("Success");
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}).executeAsync();
}
#Override
public void onCancel() {
Log.e(TAG, "On cancel");
}
#Override
public void onError(FacebookException e) {
Log.d(TAG, e.toString());
}
});
The solution is to force ShareDialog to use Feed mode and avoid using FB's App for sharing:
shareDialog.show(linkContent, ShareDialog.Mode.FEED);
I believe this is a bug by FB. They don't send postId to onSuccess callback when using FB App for sharing (postId = null), but they do if you use Feed.
I've heard you can avoid the "postId=null" problem by using Facebook Login and demanding "publish_actions" permission. But I don't think this is the correct way to deal with this problem. Regarding the mentioned permission Facebook states:
Publishing via dialogs or social plugins does not require this permission. Do not request review of this permission if you're only using Share dialog, Feed Dialog, Message Dialog etc, or Social Plugins (e.g. the Like Button.)
Facebook's docs on Share Dialog:
This does not require Facebook Login or any extended permissions, so it is the easiest way to enable sharing on the web.
Facebook confirmed this issue in their latest update:
https://developers.facebook.com/support/bugs/478167629274681/
https://developers.facebook.com/support/bugs/647119912303459/
You can't login directly with "publish_action", you have to first login with any read permission then ask for for publish permissions
https://developers.facebook.com/docs/facebook-login/permissions/v2.4#publishing
I have show facebook feeds(post) list in may application. I am using facebook 4.0 I am able to get profile but unable to fetch all my feeds(post). If anybody have any reference then please tell me. Thanks.
Right-now I am using following code for get Feeds but getting blank response like {"data":[]}
FacebookCallback<LoginResult> facebookCallback = new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
new GraphRequest(
AccessToken.getCurrentAccessToken(),
loginResult.getAccessToken().getUserId()+"/feed/",
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
Log.i("fb", "Feeds :" +response.getJSONObject());
Toast.makeText(
getApplicationContext(),
response.getJSONObject()
+ "",
Toast.LENGTH_SHORT).show();
}
}
).executeAsync();
}
You need to make a graph api call from within your app https://developers.facebook.com/docs/graph-api/reference/v2.3/user/feed
Here,
You are not passing parameters like these:
Bundle params = new Bundle();
params.putString("fields", "message,created_time,id,full_picture,status_type,source,comments.summary(true),likes.summary(true)");
params.putString("limit", "10");
/* make the API call */
new GraphRequest(AccessToken.getCurrentAccessToken(), "/userId/posts", params, HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
/* handle the result */
System.out.println("Festival Page response::" + String.valueOf(response.getJSONObject()));
try {
JSONObject jObjResponse = new JSONObject(String.valueOf(response.getJSONObject()));
}
catch (Exception e) {
e.printStackTrace();
}
}
}
).executeAsync();
Hope to solving these issue i am solving into facebook SDK 4.6.0
You should add the user_posts permission to your login request:
LoginManager.getInstance().logInWithReadPermissions(this,
Arrays.asList("public_profile ", "user_status","user_posts"));