Publishing Facebook (v2.3) comment on friends post from Android - android

After a few days of frustration, I would kindly ask for your help [also apologies as I am very new to FB]. I have an Android app in development, it has a Facebook app id, keys, hashes, etc. I created 4 test users for (obviously) test purposes and made all of these friends and users of the app. The app is set for v2.3 API. I see the increasing restrictions that Facebook is imposing, but I still think that I can do what I need to do ...
On login on the mobile, after the 'two-step' login with simple permission and then ask for more, I ultimately have these permissions:
user_friends
user_posts
manage_pages
publish_pages
publish_actions
public_profile
For a (test) user of the app, that has not yet logged into the app and has therefore not yet granted any permissions, when I query
/v2.3/{user-id}/feed or /v2.3/{user-id}/posts
I get a "200" (non-error) JSON response with no data, from memory its something like this:
{
"data" : [
]
}
which agrees with the documentation as given in FB docs
where it says:
If you attempt to read data from a feed that your app has not been authorized to access, the call will return an empty array. OK, so all that makes sense.
So then I get a list of messages from a friend (test user at this point) and store their message ids, it looks like they are in the form: userid_postid and I can scan the text, great. I use API v2.3 which looks like it will close in July 2017, but it works now.
OK, so now I want to post to the users wall or page or something that FB calls it - is it true that I can't do this anymore? I next tried to post to the comments of the stored user_postid but this fails with:
{HttpStatus: 403, errorCode: 200, errorType: OAuthException, errorMessage: (#200) Permissions error}
I do think I should have the permission to do this. I do notice that there is no comment widget on the test users posts - is that the problem? Do some posts not have comments at all, or is this a function of test users only? Here is what the posts look like, note there is no comment line shown:
I've come a long way, and can do quite a bit (get posts, user pics, friends that use the app - despite FB's documentation) but am now stuck. Any help or pointers greatly appreciated.
EDIT, forgot the code for the attempted publish to a comment:
params.putString("message", someText);
new GraphRequest(
// version 2.3 will work until July 8, 2017
AccessToken.getCurrentAccessToken(),
"/v2.3/" + mFacebookPostId + "/comments",
params,
HttpMethod.POST,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
mSends++;
FacebookRequestError err = response.getError();
if (err == null || err.getErrorCode() == 0) {
mSendGood++;
}
tv_status.setText("Sent " + mSends + " posts, " + mSendGood + " good");
}
}
).executeAsync();
Update: am I supposed to get a 'page access token', again the FB docs are not clear?

The answer seems to be a bug with the Facebook Test Users. I was never sure if I could do what I wanted to do as this was a first FB development for me, but started to get suspicious when I saw that Test User's posts do not have comments or like buttons.
But now I am not sure how to test this code, as real users can't use the app in development mode?

Related

Accessing Facebook Graph API without login

I've seen that with Javascript API and via GET requests, it is possible to get public posts from FanPages.
What i'm trying to acomplish is an Android Native App that doesn't ask user to get logged and with the FB App access token gets and shows the posts from the FB Page.
I'm wondering if this is possible via Android FB API as I can accomplish this via urls like
https://graph.facebook.com/{page_id}/feed?fields=message,picture&limit=10&access_token={your_acces_token}
When i'm trying this with Android Graph API, without user loging,
AccessToken.getCurrentAccessToken()
returns null
Thank you
I was struggling with the same for a while and after hours of research I came up with this (source):
#Override
protected void onCreate(Bundle savedInstanceState) {
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(getApplication());
AccessToken at = new AccessToken(getString(R.string.facebook_access_token), getString(R.string.facebook_app_id), getString(R.string.facebook_page_id), null, null, null, null, null);
Bundle parameters = new Bundle();
parameters.putString("fields", "message,created_time,link,full_picture");
new GraphRequest(at, "/" + at.getUserId() + "/feed", parameters, HttpMethod.GET, new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
Log.i(this.getClass().getName(), "onCompleted " + response.getRawResponse());
}
}).executeAsync();
}
I assume you already have a page access token. I created mine with the graph explorer from facebook. Here is a tutorial on how to create a never expiring one:
How to get a never expiring Facebook Page Access Token (look for the 2016 Method)
As far as I know, the extended token does not expire as long as it is used. But I am not sure about that. I think I red this somewhere in the facebook api docs but cannot find it anymore.
There are a lot other parameters you can set. See here Page and look for 'Fields'
Hope this clumsy solution is helpfull to someone.
There are two ways to do this and they are both documented here.
The simpler method is to simply concatenate your AppID and AppSecret (found in your Facebook developer dashboard) with a "|" character like so: 'App ID' + '|' + 'App Secret'
BE WARNED!
This will expose your App's Private Credentials in Client Side Code if someone decides to look carefully. Therefore it is much safer to setup a private backend server to act as a proxy for your mobile application's user-less access to the graph API.
You could set up an API in any language of your choice with a single public route that simply copies the request it receives, appends the credentials, sends the request to Facebook, and pipes Facebook's response back to the original requestor.
For Example:
Without Access Token:
Mobile App ---> http://www.YourServerDomain/{page_id}/feed?fields=message,picture&limit=10
With Access Token:
Your Server ---> http://graph.facebook.com/{page_id}/feed?fields=message,picture&limit=10&access_token=AppID|AppSecret
Response Pipeline
Facebook ---> API RESULT ---> Your Server ---> API RESULT ---> Mobile App

Facebook Friends Request - Missing Friends

I am requesting to get user friends from an Android App that I am developing. As from Facebook Api V2.0 I know that I should get only user friends that have already logged in through my App. However, although I know certain friends of a user have logged In through my App they do not appear in Facebook Request Response when requesting friends of that user. For example I get back 40 friends rather than 50+.
Has anyone experienced this behavior before? I already deleted app from few users to re-authorized it through login but I haven't see any change in the behavior.
Here is the code I'm using:
new Request(ParseFacebookUtils.getSession(),"me/friends", null, null, new Request.Callback(){
#Override
public void onCompleted(Response response) {
if (response == null){
return;
}
else if (response.getError() != null){
response.getError().getException().printStackTrace();
return;
}
GraphMultiResult result = response.getGraphObjectAs(GraphMultiResult.class);
List<GraphObject> fbInList = result.getData();
if (fbInList != null && !fbInList.isEmpty()){
for (GraphObject user : fbInList) {
JSONObject jsonUser = user.getInnerJSONObject(); // The Facebook User
System.out.println("name: " + jsonUser.optString("name"));
}
}
}
}).executeAsync();
I've found what was the problem. When I updated to the latest Facebook SDK, Facebook was returning only 25 friends. I needed to use paging or add a limit to my Friend Request. On previous SDK I didn't need to.
Adding limit:
Bundle params = new Bundle();
params.putString("limit", "50"); // Up to 5000?
friendRequest.setParameters(params);
Relevant Stack Overflow Questions:
Facebook graph API 'friends' request now only returning 25 friends per page? What's going on?
newMyFriendsRequest Facebook returns only 25 friends
Useful Facebook link for paging:
https://developers.facebook.com/docs/graph-api/using-graph-api/v2.2#paging
The relevant portion of the doc for you is this
In v2.0 of the API I'm unable to get the full friend list of someone who has logged into my app - is there a way to get the full friend
list?
With Graph API v2.0 and above, calls to /me/friends return only the
friends who also use your app. These friends must have also granted
the user_friends permission. In the cases where you want to let people
tag their friends in stories published by your app, you can use the
Taggable Friends API. If you want to invite people to your app, we
have a number of solutions that depend on the type of app you've built
and the platforms you've built for. Please see our question about
Inviting Friends for more information.
So along with only friends using your app, they should also have authorized user_friends otherwise you wont get their details. Have you done this?

Unity facebook API FB.API() java.io.FileNotFoundException

I ran into a problem. I am using Unity facebook SDK and FB.API to post on user feed.
The Code I am using is:
if (FB.IsLoggedIn) {
var data = new Dictionary<string, string>() {{"message", "I just scored "+score+" in GameName. Download it now!"}};
FB.API ("/me/feed", HttpMethod.POST,LogCallback, data);
}
void LogCallback (FBResult result) {
if (result.Error != null) {
print ("score submission failed with error= "+result.Error.ToString());
}
else {
print ("score submitted with result= "+result.Text.ToString());
}
}
and the result I am recieving is:
score submission failed with error= java.io.FileNotFoundException: https://graph.facebook.com/me/feed
I searched on the net and found that perhaps it has something to do with
publish_actions
permission. But I am using this permission.
I then went to the see the app on Facebook Developers and went to Status and Review.
There Approved permissions are:
email, public_profile and user_friends
I then tried to add
publish_actions
permission but it showed the error that I am missing icon, long description and privacy policy URL.
It wants to submit app for review. Am I doing everything correct? Do I need to upload android apk for them to review?
Can someone guide me? Thank you
You need to ask for publish_permissions from FB.Login first in order to use the /me/feed. (you'll see a message "this app wants to post on your behalf...")
That said:
Using the graph API to post a message to a user's feed if the user hasn't explicitly clicked a button indicating they want it to happen is known as 'implicit shares' and is discouraged.
Using a message you create in code instead of letting the user type in their own message is known as "pre-filling" and is not allowed by Facebook policies.
You don't need publish_actions if you use the Facebook dialogs instead of the graph API.

Post to facebook - user hasn't authorized the application to perform this request

I am trying to setup my app, so the user can login to facebook and i can then upload a post for them, simple.
I have this code
// let the user login
FB.login(function(response) {
if (response.authResponse) {
alert(JSON.stringify(response.authResponse));
// post to facebook
// create our facebook data object
var data = {
access_token: response.authResponse.accessToken,
message: "Upload this post to facebook"
};
// success, now upload the rest of the post, text etc
FB.api('/me/feed', 'post', data, function(response) {
if (!response || response.error) {
alert("There was an error uploading your post to facebook: " + response
.error.message);
} else {
alert("error: " + JSON.stringify(response));
}
});
} else {
alert(
'There was an error logging you in to facebook, or you did not authorize the app to post on your wall'
);
}
}, {
scope: 'publish_stream'
});
Since this is a cordova app, i have this in my device ready event
// initialize facebook
FB.init({
appId: "123456789", // facebook appId
nativeInterface: CDV.FB,
useCachedDialogs: false
});
Now as you can see, i am asking for the permission "publish_stream" which i believe is correct, but i keep getting a message when attempting to post, saying
There was an error uploading your post to facebook (#200). The user hasn't authorizard the application to perform this request
Well i quite clearly have.
I have this on another application that i created nearly a year ago and it all seems to work fine.
One thing i have read about is that "publish_stream" or "publish_actions" is now not available when you login, unless you submit your app for review? is this only for new apps created, as i havent done this with my other app, but it seems to work. This app also shows a message after you login, shown here. I am sure my other app doesnt show this
EDIT:
I have checked my other app and it doesnt show the above message. here is a screenshot
Any help would be appreicated.
publish_stream is deprecated, use publish_actions
before you can use publish_actions for other users, you have to go through a login review process - or the permission will only for for users with a role in your app (Admin/Developer/Tester)
Btw, you are not allowed to prefill the message parameter, it always has to be 100% user generated. And don´t "autopost" right after login (or ever). That´s against the rules too. Check out the platform policy for more information: https://developers.facebook.com/policy/

Facebook Android Unity scores api

When i try submit scores from Android Unity game i receive next error:
You are trying to load data from a www stream which had the following error when downloading.
ava.io.FileNotFoundException: https://graph.facebook.com/me/scores
FB.Login("publish_actions", OnLoginComplete);
var query = new Dictionary<string, string>();
query["score"] = "110";
FB.API("/me/scores", Facebook.HttpMethod.POST, delegate(FBResult r) { Debug.Log("!!!! Score submit result: " + r.Text); }, query);
Facebook sdk version 5.1.0
Unity version 4.5.2f1
For posterity, the most common cause of this error is not having your app configured as a "Game" in the app settings on Facebook. OP and I talked elsewhere, earlier, and ruled that out in this case, but for anyone else finding this post via search, make sure you check that.
To clarify, the code starting with "var query" - is it possible that it is running before OnLoginComplete is called? If you've ruled that out, could you verify whether the problem might be with the grant of permissions? An easy way to do that would be to log the value of the access token right when you make the call to FB.API, then copy-paste that into the access token debugging tool at https://developers.facebook.com/tools/debug and verify that it actually has publish_actions. You could also just take that token and use it in curl in verbose mode (curl -kv "https://graph.facebook.com/me/scores?score=110&access_token=TOKEN"), and see if the headers or response body gives any additional clues.

Categories

Resources