I'm trying to get the realtime updates to my app from facebook using their realtime api. I have configured my subscription.
{
"data": [
{
"object": "user",
"callback_url": "https://myappname.herokuapp.com/realtime.php",
"fields": [
"activities",
"feed",
"is_app_user",
"likes",
"online_presence",
"photos",
"picture",
"status",
"wall_count"
],
"active": true
}
]
}
I have created an endpoint which simply logs the request when I get a post request. But I don't seem to be getting any calls from facebook. If I manually do a post to the end point, it logs the post body json. I have checked the heroku logs but there is no trace of post request form fb, only my manual request is present.
<?php
$method = $_SERVER['REQUEST_METHOD'];
if ($method == 'GET' && $_GET['hub_mode'] == 'subscribe') {
echo $_GET['hub_challenge'];
} else if ($method == 'POST') {
$data = file_get_contents("php://input");
$json = json_decode($data, true);
error_log("update recieved");
error_log('updates = ' . print_r(json, true));
pg_close($db);
}
?>
I have did a callback request test specified by
https://graph.facebook.com/yourappid/subscriptions?access_token=youraccesstoken&object=user&fields=feed&verify_token=yourownsecretstringsetinyourphpfile&method=post&callback_url=http://www.yourwebsite.com/facebook_subscribe.php
which returned me null indicating subscription is success.
I have made a few status updates and likes but I'm not getting any updates from fb. Am I missing something out here?
Thanks in advance.
It's a bug but FB has labelled it as invalid.
Upvote this: https://developers.facebook.com/bugs/394779173962716
Related
I keep getting this error message when am attaching images to page post
message: '(#10) Application does not have permission for this action'
but when am posting message only, it works fine
FB.api('PAGE_ID/feed', 'post', {
message: 'Message is here',
link: 'https://my_LINK.com',
"child_attachments": [
{
"link": "https://1.jpg",
"image_hash": "hash1",
"name": "Some Name",
"description": "Some description"
},
{
"link": "https://2.jpg",
"image_hash": "hash2",
"name": "Some Name",
"description": "Some description 2"
}]
}, function (res) {
if (!res || res.error) {
console.log(!res ? 'error occurred' : res.error);
return;
}
});
to add multi images to a post in facebook, it has to be done in 2 steps as per facebook
to upload image, refer to this and to make the post
function post_fb_ad(description, ad_images) {
attached_media = []
for (var adImg of ad_images) {
attached_media.push({ media_fbid: adImg.id })
}
FB.api('page_id/feed', 'post', {
message: description,
attached_media: attached_media,
access_token: EXD_ACCESS_TOKEN
}, function (res) {
if (!res || res.error) {
console.log(!res ? 'error occurred' : res.error);
return;
}
console.log('post id is ', res)
});
}
In order to publish to a page, you will need to use a page access token that has publish_pages permission which you may be missing.
You can quickly test the request using the Graph API Explorer tool.
You can also use the Access Token Debugger tool to test what permissions your access token has.
Also, note that publish_pages permission is reviewable and needs to be approved before you can use it with the general public. Prior to approval, you can only test it with an app where you are an admin and in a page where you have admin role.
I'm trying to integrate webhook into my project, I have verified the webhook successfully, but when I send sample data to the server, my server does not receive anything, my project developed on Codeigniter.
I tried using postman to post Json data to the webhook url that was authenticated, my server received
Postman: [POST] https://xxxxxx.xxx/api/webhook
[RAW]
{
"field": "conversations",
"value": {
"page_id": 4444444,
"thread_id": "t_mid.14833205540:9182a4e489"
}
}
Code:
public function webhook(){
if (isset($_GET['hub_mode']) && isset($_GET['hub_challenge']) && isset($_GET['hub_verify_token'])) {
if ($_GET['hub_verify_token'] == 'EcyUykjnmredclnuYFLShBKHfutRFfDRdfdfb'){
echo $_GET['hub_challenge'];
}
}
$data = file_get_contents("php://input",true);
$myfile = fopen("./my-assets/uploads/text.txt", "w");
fwrite($myfile, $data);
fclose($myfile);
http_response_code(200);
}
I need to post messages on a Facebook page. Specifically I want to post via cron.
Here's what the API docs say:
Page Access Token – These access tokens are similar to user access tokens, except that they provide permission to APIs that read, write or modify the data belonging to a Facebook Page. To obtain a page access token you need to start by obtaining a user access token and asking for the manage_pages permission. Once you have the user access token you then get the page access token via the Graph API.
How I can obtain a user access and page access token without a page callback? Is this possible?
What you need it an Extended Page Token, it is valid forever. You get one like this:
Authorize with the manage_pages permission (and publish_pages if you want to post as Page later), to get a User Token
Extend the User Token
Use /me/accounts?fields=access_token with the Extended User Token to get a list of all your Pages with Extended Page Tokens - or use /page-id?fields=access_token to get an Extended Page Token for a specific Page
Information about all Tokens and how to extend the User Token:
https://developers.facebook.com/docs/facebook-login/access-tokens#extending
http://www.devils-heaven.com/facebook-access-tokens/
PHP API V5
The below code worked for me after 24 hours of head scratching .... hope this helps by the way if you need this code to work you should have completed the first two steps
Should have login to facebook I used getRedirectLoginHelper
Set session variable with the received user access token on the call back file $_SESSION['fb_access_token'] = (string) $accessToken;
$fbApp = new Facebook\FacebookApp( 'xxx', 'xxx', 'v2.7' );
$fb = new Facebook\Facebook( array(
'app_id' => 'xxx',
'app_secret' => 'xxx',
'default_graph_version' => 'v2.7'
) );
$requestxx = new FacebookRequest(
$fbApp,
$_SESSION['fb_access_token'],//my user access token
'GET',
'/{page-id}?fields=access_token',
array( 'ADMINISTER' )
);
$responset = $fb->getClient()->sendRequest( $requestxx );
$json = json_decode( $responset->getBody() );
$page_access = $json->access_token;
//posting to page
$requesty = new FacebookRequest(
$fbApp,
$page_access ,
'POST',
'/{page-id}/feed?message=Hello fans YYYYYYYYYYYYYYY'
);
$response = $fb->getClient()->sendRequest( $requesty );
var_dump( $response );
You can get the page token this way:
$response = $fb->get('/'.$pageId.'?fields=access_token', (string)$accessToken);
$json = json_decode($response->getBody());
$page_token = $json->access_token;
$response = $fb->post('/'.$pageId.'/feed', $fbData, $page_token);
I've only JavaScript code, but once you have an access token, you may get the pages which can be adminstered by the given user. This will contain a page access token for each of them:
jQuery.ajax({type: "GET",
url: "https://graph.facebook.com/v2.2/me/accounts?access_token=" + userToken,
async: false,
data: jsonRequest,
dataType: "json",
cache: false,
success: function(data)
{
The data given back is like:
{
"data": [
{
"access_token": "CAACni8TcBB0B...cZBJfwZDZD",
"category": "Computers/Technology",
"name": "abc",
"id": "...",
"perms": [
"ADMINISTER",
"EDIT_PROFILE",
"CREATE_CONTENT",
"MODERATE_CONTENT",
"CREATE_ADS",
"BASIC_ADMIN"
]
},
{
"access_token": "CAA...ZDZD",
"category": "App Page",
"name": "xyz",
"id": "....",
"perms": [
"ADMINISTER",
"EDIT_PROFILE",
"CREATE_CONTENT",
"MODERATE_CONTENT",
"CREATE_ADS",
"BASIC_ADMIN"
]
}
],
access_token is your page token. You may transform the above request into PHP easily.
If I like more than 100 pages/things, FB.API('me/likes') returns 99 items and a link to the next paging.
Is it possible to get ALL without the pagination?
Thanks
Have you tried /me/likes?limit=999 ?
You may still need to paginate, but you should be able to get more than 99 items in a single call
Use FQL:
$fql = "SELECT page_id from page_fan where uid = me())";
$pages_i_liked = $facebook->api(array(
'method'=> 'fql.query',
'access_token' => $access_token,
'query'=> $fql,
));
print_r($pages_i_liked);
Get All facebook pages of a user using facebook api
required permissions: manage pages
type: GET
url: https://graph.facebook.com/me/accounts
param: access_token
responce of the above request like this
{
"data": [
{
"category": "Book",
"name": "Mind blowing books",
"access_token": "CAACEdEose0cBAFRU2j0rGgNxBcJvU0pkZCpDbI7rZCJNmO2cZAfZBXoejoZCdTVdKi4gNCyBuu9fPRnWRAwCKrmkPePzKHoE5e46Jz7gRDYe3PM5ECm0ZC5OZB2iWLeEh3OZBgTGfWDmQbbFivwlp5v2umc0CcC9JlTvHsWDnTZCkKIbZAJeD2nOus1ZCCXMqSXHOAZD",
"perms": [
"ADMINISTER",
"EDIT_PROFILE",
"CREATE_CONTENT",
"MODERATE_CONTENT",
"CREATE_ADS",
"BASIC_ADMIN"
],
"id": "618353601555775"
}
],
"paging": {
"next": "https://graph.facebook.com/100000328561058/accounts?access_token=CAACEdEose0cBADKMTNRBl5pjNhw8xsKnQf57XKShV7UlhGyJy67bBZCUKkepl9rELlxqq0I474f8LEPGnt51Mdgs0MMtgTycuUgkOyRnLgVypWVpBd7oKy3LXrrbsQWSdIUZBU4qKHLxSb14TP8ySOaZChLseseYMr1YMLG3qrJiWLuwWJeVz2PeE8TmkkZD&limit=5000&offset=5000&__after_id=618353601555775"
}
}
Post at specific facebook page of a user using facebook api
required permissions: piblish action
type: Post
url: https://graph.facebook.com/{PAGE_ID}/feed
param: access_token, message
this http request will write your message on fb page
PAGE_ID: its page id which is in responce of first request
The maximum results limit is 100
"I just noticed this while counting the results and also next page query, if I insert limit 999 for example, the exact results will be shown as 100 and the next link generated by facebook will contain the limit value also 100"
With this class from Github: FacebookLikedPagesAPI you can get all ID liked pages on Facebook without pagination
$access_token = '';// your access token here
$likes=new Likes();
$result=$likes->getAllLikedPages($access_token);
What i want to do is get the request ID and insert to my database. My request dialog is run well, but the problem is I cannot get 'TO' user id http://developers.facebook.com/docs/reference/dialogs/requests/ , but I still cannot get the request user ID.
here is my coding:
function newInvite(){
//var user_ids = document.getElementsByName("user_ids")[0].value;
FB.ui({
method : 'apprequests',
title: 'X-MATCH',
message: 'Come join US now, having fun here',
},
function getMultipleRequests(requestIds) {
FB.api('', {"ids": requestIds }, function(response) {
console.log(response);
});
}
);
}
any solution on this?
million thanks for help
Have you enable request 2.0 efficient?
If you have enabled, you can get the user id easily as the response like this
{
request: ‘request_id’
to:[array of user_ids]
}
In your callback function, you can use
response.request to get the request ID
response.to to get the array of user ids
And notice that if you use request 2.0, the request ID format will like this
<request_object_id>_<user_id>
If you doesn't enable it, then you can only get the array of request ids and you need to make another api call to get the user id
Edited:
FB.ui({
method : "apprequests",
title : "your title",
message : "your msg"
},
function(response) {
var receiverIDs;
if (response.request) {
var receiverIDs = response.to; // receiverIDs is an array holding all user ids
}
}
);
Then you can use the array "receiverIDs" for further process
For example I sent a request to user id with id "1234", "5678"
The response will like this:
{
request: ‘1234567890’ // example,
to:['1234', '5678']
}
In request 2.0, the full request id will look like this
1234567890_1234
1234567890_5678
Caution: FB doc tell you to manage and delete the request yourself, if you using request 2.0,
remember to delete the id like the above, if you directly delete the request '123456789', all the full request ID with this prefix will be deleted.
==================================================================
If you haven't enable request 2.0, follow the code on the doc page to see how to get the user id by making a api call
function getMultipleRequests(requestIds) {
FB.api('', {"ids": requestIds }, function(response) {
console.log(response);
});
}
The response format for these methods is as follows:
{
"id": "[request_id]",
"application": {
"name": "[Application Name]",
"id": "[Application ID]"
},
"to": {
"name": "[Recipient User Name]",
"id": "[Recipient User ID]"
},
"from": {
"name": "[Sender User ID]",
"id": "[Sender User Name]"
},
"message": "[Request Message]",
"created_time": "2011-09-12T23:08:47+0000"
}
you can implement the callback of api call and getting who is the receiver by response.to.id