Get data of a post using ID provided by facebook realtime-api - facebook

I am using Facebook realtime api, as I get only the updates and have to fetch the whole data by hitting the server again.
I have a page, my app added to that hence I am getting page feed(like, comment, post, all).
When any user posts on the page, we get the update from Facebook realtime update api. But when I try to fetch post data using the Koala gem it gives me error, note that error is not in case of Update from Page itself(page admin) but when some other user posts on it.
Following is the code for help :-
Trying to fetch using long lived page-token, and without that too, failing both ways
##graph = Koala::Facebook::API.new ACCESS_TOKENS["facebook"]["page_token"]
##public_graph = Koala::Facebook::API.new
JSON response from facebook :-
{"object"=>"page",
"entry"=>
[{"id"=>"123412341234234",
"time"=>1412341234,
"changes"=>
[{"field"=>"feed",
"value"=>
{"item"=>"post",
"verb"=>"add",
"post_id"=>123412341234123,
"sender_id"=>1234123412}}]}]}}
##public_graph.get_object("123412341234123")
*** Koala::Facebook::ClientError Exception: type: GraphMethodException, code: 100, message: Unsupported get request. [HTTP 400]
##graph.get_object("123412341234123")
*** Koala::Facebook::ClientError Exception: type: GraphMethodException, code: 100, message: Unsupported get request. [HTTP 400]
Please help me out to understand how to fetch the public post data using the post_id provided by the realtime-updates api of facebook.

Q: how to fetch the public post data using the post_id provided by the realtime-updates api of facebook.
For fetching public data of the post from the page, you will need to specify both the IDs (page id as well as post id) you are getting in the RT hit form fb.
You will need to pass id as <page_id>_<post_id>. In your case, it will be:
rt_hit = {"object"=>"page",
"entry"=>
[{"id"=>"123412341234234",
"time"=>1412341234,
"changes"=>
[{"field"=>"feed",
"value"=>
{"item"=>"post",
"verb"=>"add",
"post_id"=>123412341234123,
"sender_id"=>1234123412}}]}]}}
entry = rt_hit["entry"].first // you may want to have loop instead of `first`
public_id = "#{entry['id']}_#{entry['changes'].first['value']['post_id']}"
##public_graph.get_object(public_id) // fetch object

Related

How to access Facebook Analytics data using the marketing API?

I'm creating an app that needs to display Facebook marketing data such as insights, cpm, etc on various pages and posts associated with an account. I haven't been able to figure it out so far. I've created an app, and have tried making calls from the sandbox account with the appropriate access token. When running the demo code to read the ad account's name and age I am getting the following error. This has been really frustrating and my employer is getting mad. Any help is appreciated.
Code (node.js):
const adsSdk = require('facebook-nodejs-ads-sdk');
const accessToken = 'Sandbox acct token';
const api = adsSdk.FacebookAdsApi.init(accessToken);
const AdAccount = adsSdk.AdAccount;
const account = new AdAccount(XX Sandbox ID XX);
console.log(account.id)
account
.read([AdAccount.Fields.name, AdAccount.Fields.age])
.then((account) => {
logPassedTest(test1 + ':Pass', account);
})
.catch((error) => {
console.log(error);
});
Error:
message: 'Unsupported get request. Object with ID \'XXX\' does not exist, cannot be loaded due to missing permissions, or does not support this operation. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api',
{ error:
{ message: 'Unsupported get request. Object with ID \'XXX\' does not exist, cannot be loaded due to missing permissions, or does not support this operation. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api',
type: 'GraphMethodException',
code: 100,
error_subcode: 33,
fbtrace_id: 'ACp+q8Bas3Z' } },
method: 'GET',
url: 'https://graph.facebook.com/v2.11/X?fields=name%2Cage&access_token=XX',
data: {} }
The AdAccount ID is prefixed with act_
i.e. act_<ACCOUNT_ID>.
There is really no way of debugging this. The error code likely means you are providing the wrong object to the call you are making.
Go to: https://developers.facebook.com/tools/explorer/
Plug in your access token and the exact call you are making. If the call fails, remove pieces of it until it succeeds.

Getting post lists from facebook pages

I'm trying fetch list of post from given set of facebook pages using restfb(java)
List fbPages = Arrays.asList("178697151159/posts", "538560813021153/posts");
JsonObject fetchObjectsSubResults = client.fetchObjects(fbPages, JsonObject.class, Parameter.with("fields","shares,created_time"),Parameter.with("limit", 5));
But I'm getting a runtime error from facebook api as following.. IS there any body that can help me out to resolve this or is this impossible (
Exception in thread "main" com.restfb.exception.FacebookOAuthException: Received Facebook error response of type OAuthException: (#803) Some of the aliases you requested do not exist: 178697151159/posts?,538560813021153/posts? (code 803, subcode null)
at com.restfb.DefaultFacebookClient$DefaultGraphFacebookExceptionMapper.exceptionForTypeAndMessage(DefaultFacebookClient.java:1278)
at com.restfb.DefaultFacebookClient.throwFacebookResponseStatusExceptionIfNecessary(DefaultFacebookClient.java:1195)
at com.restfb.DefaultFacebookClient.makeRequestAndProcessResponse(DefaultFacebookClient.java:1136)
at com.restfb.DefaultFacebookClient.makeRequest(DefaultFacebookClient.java:1058)
at com.restfb.DefaultFacebookClient.makeRequest(DefaultFacebookClient.java:1020)
at com.restfb.DefaultFacebookClient.fetchObjects(DefaultFacebookClient.java:476)
at main.main(main.java:75)
I suggest using 1 call per page. SO it should look like:
Connection<Post> postConnection = client.fetchConnection(pageId+ "/posts", Post.class, Parameter.with("fields","shares,created_time"), Parameter.with("limit","5"));
Depending on your use case, you can run over the postConnection with a for loop to get all posts or take the data from the first page of the result set with postConnection.getData().
In both cases you can eventually work with a Post type and access the fields your are interested in. The other fields are left null.
BTW working with multiple ids in a single call or request these ids one after another makes no difference to the Facebook calls. The amount that is internally calculated is the same; in your case 2.

Unable to get past login page using jsoup

I am unable to get the desired response using jsoup to login and scrape the referred page.
I am trying to access a page called https://localhost/private/frontpage.do , and I can see that the POST method is POST security_check which I gleaned via firebug with the parameters password=guest&username=guest&submit=.
When I use the following code I still am unable to get to the desired page.
Response res = Jsoup
.connect("https://localhost/private/security_check")
.data("j_username", "guest")
.data("j_password", "guest")
.referrer("https://localhost/private/frontpage.do")
.data("submit", "")
.method(Method.POST)
.execute();
Document doc = res.parse();
String sessionId = res.cookie("SESSIONID");
Document doc2 = Jsoup.connect("https://localhost/private/frontpage.do")
.cookie("SESSIONID", sessionId)
.get();
I received the following POST location and parameters from firebug as follows https://localhost/private/j_security_check?password=guest&username=guest&submit=
Any idea why I cant see the new page, but get a HTTP error instead in Java Exception in thread "main" org.jsoup.HttpStatusException: HTTP error fetching URL. Status=408

Facebook Private Messaging

It is said, that it is not possible to initiate new conversation through the API alone, except using Facebook's own Form integrated in the app. Is this correct, or is there some new API, which enables me to initiate a new conversation?
To reply to an existing conversation, I retrieved the conversations id using the following FQL Query "SELECT thread_id, . WHERE viewer_id={0} AND folder_id=0". Afterwards I retrieved the PageAccessToken for my app page using my user Access token, and tried to use this call:
*You can reply to a user's message by issuing an HTTP POST to /CONVERSATION_ID/messages with the following parameters [conversation id, message]. A conversation ID look like t_id.216477638451347.*
My POST Call looked like this (this is not a valid thread id): /t_id.2319203912/messages with message parameter filled. But it always said "Unknown method". Can you help me out with this one? Is there a parameter missing? I passed in the page's Access Token to call this one.
Is there some API out (except Facebook's Chat API), that I am missing, which can send private messages to users?
Edit:
What I wonder about is, that the code below only returns a single page, the application's page. Is this correct, or is there another page token required? This is what bugged me the most about the returned page.
The FacebookClient uses my UserToken to perform the next following task.
This is the code to retrieve my Page Access Token:
dynamic pageService = FacebookContext.FacebookClient.GetTaskAsync("/"+UserId+"/accounts").Result;
dynamic pageResult = pageService.data[0];
_pageId = pageResult["id"].ToString();
return pageResult["access_token"].ToString();
Now the code to retrieve my ConversationÍd:
dynamic parameters = new ExpandoObject();
parameters.q = string.Format("SELECT thread_id, folder_id, subject, recipients, updated_time, parent_message_id, parent_thread_id, message_count, snippet, snippet_author, object_id, unread, viewer_id FROM thread WHERE viewer_id={0} AND folder_id=0", FacebookContext.UserId);
dynamic conversations = FacebookContext.FacebookClient.GetTaskAsync("/fql",parameters).Result;
The following code is executed using the access token retrieved from the code above (page access token request).
Now the Code used to send the reply:
dynamic parameters = new ExpandoObject();
parameters.message = CurrentAnswer;
string taskString = "/t_id." + _conversationId + "/messages";
dynamic result = FacebookContext.FacebookClient.PostTaskAsync(taskString,parameters).Result;
return true;
I also tried it with facebook's graph API Debugger using the token, which is returned by my first part of code. But with the same error message.

Facebook Request Dialog with data

I read this article.
So, I tried it and I put a number in the data property.
FB.ui({
method: 'apprequests',
message: 'Come join me and play at MyWebSite!',
data: '12345',
redirect_uri: 'myWebSite'
});
I get the request_ids, but how do I get the data part (the 12345 number)?.
on server side, you can do something like:(using php here)
$request_ids = $_GET['request_ids'];
$request_ids = explode(",", $request_ids);
foreach($request_ids as $request_id)
{
$request_object = $facebook->api($request_id);
if(isset($request_object['data'])) $req_data = $request_object['data']; //$req_data will be '12345' as per your request data set.
// after getting the data, you may like to delete the request.
$full_request_id = $request_id."_".$fbid; //$fbid is current user facebook id
$facebook->api("$full_request_id","DELETE");
}
Did you try Facebook's documentation too?
https://developers.facebook.com/docs/requests/ has more documentation; if a data parameter was added in the call to the requests dialog, the same value should also be there when requesting the Request details via the API (i.e. a call to /REQUEST_ID)
See the facebook developer site documentation for more details
http://developers.facebook.com/docs/reference/dialogs/requests/
Note:
data:Optional, additional data you may pass for tracking. This will be stored as part of the request objects created. The maximum length is 255 characters.