Facebook Realtime Updates for Payments callback POST received but still having issues - facebook

I am using Facebook payments and Realtime Updates for Payments. Here is a typical payment callback from Facebook servers when someone buys/refunds something:
{
"object": "payments",
"entry": [
{
"id": "296989303750203",
"time": 1347996346,
"changed_fields": [
"actions"
]
}
]
}
I am using PHP to get this like this:
$updates = json_decode(file_get_contents("php://input"), true);
It works ok and I got the id stored in $myid. My problem is:
In the same PHP page I try to get details of this ID but it doesn't work:
$getDetails = json_decode(file_get_contents('https://graph.facebook.com/'.$myid.'?access_token='.$app_token));
The response is empty, nothing there. I even tried to get the user with this $user = $facebook->getUser(); in this PHP but it returned with zero. I suspect that since this PHP is run from the Facebook server (and not the client browser) Facebook does not know who the user is and that is why the Graph API does not work.
How can this PHP page knows who the user is? Cookies and sessions cannot be used since they will never communicate with the client browser. Help is needed. Thanks.

Related

Problems with facebook app's web-hook [duplicate]

I am trying to install a webhook for leadgen event for my page via a facebook app. So I have:
Facebook Page where leads come from
Facebook App
Webserver where I want to save leads
App and webserver are connected well I believe. Webhook is shown at app page etc. But when I am trying to create a test lead with this tool https://developers.facebook.com/tools/lead-ads-testing I am getting a POST request with no data in it.
I was suspecting permissions problems, but I am able to check a lead from page (via leadgen_id) directly with PHP SDK and the POST request is sent from Facebook just by URL, so they don't know about tokens yet.
UPD Plain POST request to the same url (curl -d "param=value" https://..url..) works as expected.
Facebook sends webhook data as Content-Type: application/json, not as …: application/x-www-form-urlencoded (as a normal form with method=post would.)
Therefor, PHP does not populate $_POST – you need to read the raw input stream instead. That can be done using file_get_contents('php://input') – and then just apply json_decode on that data, and you’ll have a proper data structure to work with.
Facebook sends the leads data in the request body. If you are using a framework, please check if you have access to the request body.
Try using a third party intermediate service like Runscope to see the full request, it is very usef
This code works for me...
if (!empty($_REQUEST['hub_mode']) && $_REQUEST['hub_mode'] == 'subscribe' && $_REQUEST['hub_verify_token'] == "<mytoken>") {
echo $_REQUEST['hub_challenge'];
} else {
$data = json_decode(file_get_contents("php://input"), true);
file_put_contents('logFB.txt', print_r($data, true));
}
First part is for verifying webhook, second for getting data from facebook webhook.
Hope this will help...
In case you are using the django framework, you should request.body, as post data will remain empty.
def webhook_response(request):
# it will print the contents from facebook webhook response
print(request.body)
# something like the following object will print up if you are using leadgen
{"object": "page", "entry": [{"id": "0", "time": 111111111, "changes": [{"field": "leadgen", "value": {"ad_id": "444444444", "form_id": "444444444444", "leadgen_id": "444444444444", "created_time": 11111111, "page_id": "444444444444", "adgroup_id": "44444444444"}}]}]}

Facebook real time update for user and page

I have setup realtime facebook update for both user and page. Whenever user updates his status, I get request on my server about the change. The data I get from facebook post call is like below.
{
"object":"user",
"entry":[
{
"uid":"10152689315982483",
"id":"10152689315982483",
"time":1427362347,
"changed_fields":[
"feed"
]
}
]
}
But I did not get any call from facebook when admin of a page update its status.
I followed the below steps to get the realtime facebook update.
Subscribe user/page to get updates with access token.
graph.facebook.com//subscriptions?object=user&fields=feed&verify_token=&method=post&callback_url=htps://serverurl/realtime.php
To get the list of subscription
graph.facebook.com//subscriptions
From this call I get both user and page data.
{
"data": [
{
"object": "user",
"callback_url": "https://serverurl/realtime.php",
"fields": [
"feed"
],
"active": true
},
{
"object": "page",
"callback_url": "https://serverurl/realtime.php",
"fields": [
"feed"
],
"active": true
}
]
}
I also added the app to page-tab but still not getting the updates for page. Could anyone tell me what I am missing?
Do an http GET on
https://graph.facebook.com/v2.3/{page-id}/subscribed_apps?access_token=PAGE_ACCESS_TOKEN
If your app is not listed, you need to "install it" (this is an alternative installing the app as a page tab app). Do this by issuing a post request to the same url. Eg with curl,
curl -X POST "https://graph.facebook.com/v2.3/{page-id}/subscribed_apps?access_token=PAGE_ACCESS_TOKEN"
You don't need to supply any parameters since the id of the app you wish to install is inferred from the access token.
If /subscribed_apps already includes your app id, you may be experiencing a bug and should report it at http://developers.facebook.com/bug

Facebook Real-time updates not working

I'm setting up Facebook Real-time updates for an app so that I can read user's status updates as they happen, but I'm not getting updates. As far as I can tell I've set everything up correctly. Here's my subscription:
{
"data": [
{
"object": "user",
"callback_url": "*****/facebook-rt-callback.aspx",
"fields": [
"checkins",
"feed"
],
"active": true
}
]
}
When I test my subscription, my callback gets the request and FB shows that it worked correctly.
When user's give access to my app, I request scope=user_status,user_checkins. I also have these permissions defined in my app on FB. My test user is able to login and give permission to my app, but when the test user posts status updates or checkins on his timeline, my callback never gets hit.
Any ideas? Thanks.

How to get Facebook test user email and password after create?

I am using Selenium to test a Facebook auth app. Currently I am using a script in the test suite to create Facebook test users by posting to the test user api found here:
https://developers.facebook.com/docs/test_users/
https://graph.facebook.com/APP_ID/accounts/test-users?
installed=true
&name=FULL_NAME
&locale=en_US
&permissions=read_stream
&method=post
&access_token=APP_ACCESS_TOKEN
This gives me back a response of:
{
"id": "1234...",
"access_token":"1234567..." ,
"login_url":"https://www.facebook.com/platform/test_account..."
"email": "example...#tfbnw.net",
"password": "1234..."
}
This gives me the username and password of the test. However If I want to reuse the user in a later test I don't know how to get this info. I might be over looking something. If I do a GET request to get a list of registered test users the response looks like this:
{
"data" [
{
"id": "1231....",
"access_token":"1223134..." ,
"login_url":"https://www.facebook.com/platform/test_account.."
}
{
"id": "1231....",
"access_token":"1223134..." ,
"login_url":"https://www.facebook.com/platform/test_account.."
}
]
}
This response dosen't give me any emails/passwords which I'd need to login the test users in a full test. I'd rather not log them in by using the access_token and login_url because this isn't true to the Facebook auth flow on the site.
See https://developers.facebook.com/docs/test_users/#changepw
Facebook does not store cleartext passwords and will only return the password on user creation per the documentation. You can change the user's password to anything at any time without knowing their password so you could set all your test users to have whatever password you want after creation and just rely on that when running your tests.

Interfacing with wall/feeds

This is the first time I'm trying to develop a facebook app so sorry in advance if my question is too naive.
What I need to do is making a chat-like facebook application where:
the user can write something on the wall
the app should be able to detect this event and send an HTTP request to an external web service of my own which will provide a response (text)
publish that text as a comment
the user should be able to continue the dialog by entering another comment(s) (in which case we go back to step #2)
Basically, this would be very similar to:
https://www.facebook.com/SkyscannerFlightSearch
I think one (ugly) way to do this would be making a script which searches for new wall entries/comments and posts replies in an infinite loop by using the Graph API but it's obviously sub-optimal and expensive.
Is there any way to have facebook call a certain url every time a wall post/comment is entered?
Or maybe something like Twitter's streaming API based on long-polling technique?
Am I in the right direction by assuming such kind of solutions or I'm totally missing the point?
Thanks in advance.
Giampaolo
I am working on something very similar myself.
So far i have the "loop" which can be set to any page, group or app on facebook.
SAMPLE: https://shawnsspace.com/plugins/wallfeed.php My page wall.
SAMPLE: https://shawnsspace.com/plugins/wallfeed.php?pageid=19292868552&ptype=feed&limit=40 Facebook Platform Wall.
With some perms, a form and user access_tokens i can make the wall postable. Asper UGLY - you do not need to run this in a loop, Facebook supports realtime updates and will send a response to your app when a user, or page has made a change.
MORE: http://developers.facebook.com/docs/api/realtime
Thanks for the input.
I tried to use the real-time API by using object=user and fields=feed.
If I understood the doc correctly this should result in my callback url being called (POST) every time a user writes something on my app's wall.
I received the initial GET request but never POST.
This is the current configuration:
{
"data": [
{
"object": "user",
"callback_url": "http://XXX.XXX.XXX.XX:8888/",
"fields": [
"feed"
],
"active": true
},
{
"object": "page",
"callback_url": "http://XXX.XXX.XXX.XX:8888/",
"fields": [
"picture"
],
"active": true
}
]
}
I've noticed various user comments reporting different concerns about the reliability of this API.
Also, here:
http://developers.facebook.com/docs/reference/api/page/
it says: "Note: Real-time updates are not yet supported for the total number of Page checkins."
...which I'm not sure what it means exactly.
For the record, my app's page I'm using for tests is:
http://www.facebook.com/pages/testgiamp/187148861354102?sk=wall