Post to facebook page wall as a page - php

I want to post to facebook page wall as a page using PHP. I've got access_token by below links.
https://graph.facebook.com/oauth/authorize?type=user_agent&client_id=123456789&redirect_uri=http%3A%2F%2Fmysite.net&scope=publish_stream,manage_pages,offline_access
https://graph.facebook.com/me/accounts?access_token=...
I'm using this simple code:
$appid = "";
$secret = "";
$pageid = "";
$access_token = "";
require_once("facebook-php-sdk/src/facebook.php");
$facebook = new Facebook(array(
'appId' => $appid,
'secret' => $secret
));
try {
$args = array(
'access_token' => $access_token,
'message' => 'Test',
'link' => 'http://www.test.com',
'description' => 'Test'
);
$post_id = $facebook->api("/$pageid/feed","post",$args);
} catch (FacebookApiException $e) {
error_log($e);
}
And that's the error I'm getting:
OAuthException: (#200) Posts where the actor is a page cannot also include a target_id
But posting /me/feed won't work neither. All solutions I've googled don't work anymore, official documentation didn't help. I got it working when posting as a user (not a page) and with javascript api (required 'share' click action by a user).
Anyone knows the solution for automatic post to a fb page as a page? Spent couple of frustrating days trying to figure it out -_-
Thanks,
A.

I've had the same problem. The reason was that I was an admin of more than one page, and I tried to post with the token of another page.

Pages cannot post to user's walls (that's why you're getting the OAuthException that says "as a page, you cannot set the target_id"). You'll have to first create an application and get the user's permission (publish_stream) to post to their feed via said app.

I assume you have some knowledge of app and how posting work.
1.You have to get Page access token and use this on array.
2.you use this
"/$pageid/feed"
try use this
$facebook->api($pageID . '/feed','POST'
Thanks...

Related

Create a Facebook App that can post to a page I am an admin of

As part of the website I am working on, I need to be able to post directly to the wall of my clients Facebook Page.
I have created an App and am successfully posting to my own dummy profile by simply using:
$request = new FacebookRequest(
$session, 'POST', '/me/feed', array(
'link' => 'my_url',
'message' => 'my_message'
);
Now I need to modify this to post to my clients page, im assuming I cant just change 'me' to by their page ID because that would be a massive security flaw so I need to get permission somehow to post to their page. I am an admin for the page, how can I do this? Or do I actually need to be logged in with the user account that created the page?
I have looked all around in the developer section at creating an app but there doesn't seem to be any way of creating an app for a different account.
Any advice would be greatly appreciated on this.
Many thanks
In order to post to a Facebook page, you need to go through a two steps process -
login as the user using the extended user token,
get the user's token for the page
post to the page using the page's token.
The following code should be able to post a message to your page. I HAVEN'T DEBUGGED IT since it is simplified from my own web site :-) Don't forget to include the Facebook SDK files:
function extract_access_token_of_facebook_page_by_id($array_of_all_user_pages,$page_id){
$num_of_pages=count($array_of_all_user_pages['data']);
$the_page_token="";
for ($i=0; $i<$num_of_pages; $i++){
if ($array_of_all_user_pages['data'][$i]['id']==$page_id){
$the_page_token=$array_of_all_user_pages['data'][$i]['access_token'];
}
}
return $the_page_token;
}
$facebook = new Facebook(array(
'appId' => $YOUR-APP-ID,
'secret' => $YOUR-APP-SECRET,
));
try
{
$facebook->setAccessToken($THE-USER-TOKEN);
$page_id = YOUR-PAGE-ID;
$fanpage_access_token=extract_access_token_of_facebook_page_by_id($facebook->api("/me/accounts"),$page_id);
$args = array();
$args['access_token'] = $fanpage_access_token;
$args['message'] = "THE TEXT YOU WANT TO POST";
$api_url="/".$page_id."/feed";
$api_response = $facebook->api($api_url, 'post', $args);
}
catch (FacebookApiException $e)
{
echo 'Error facebookservice'.$e;
}
Please look into Application developer section. It will help you. Create on developer account and you will be able to access page.
you code is for old sdk version i think, it not work for me : Fatal error: Class 'Facebook' not found

Facebook ignoring og:image for shared website link

I have recently noticed that facebook has started ignoring my og:image tag on my site. The image used is always larger than 200x200.
Here is an example page:
http://bit.ly/15CrOhS
http://bit.ly/1b8Mgbe
Seems to be alot of questions, but no answers. I've added all the og information and checked with the linter and it all goes through fine. So why does it choose to pick a random image?
I use the facebook api for PHP to send:
<?php
$link = 'http://www.mylinkaddress.com';
$msg = 'Check out my new photo. '.$link;
$get_oauth = "SELECT * FROM users_oauth_cred WHERE userid = ".$_SESSION['userid']." AND share = 1 AND oauth_access_token != ''";
$get_oauth = mysql_query_run($get_oauth);
$oauth = mysql_fetch_array($get_oauth);
# FACEBOOK
$facebook = new Facebook(array(
'appId' => FACEBOOKAPPID,
'secret' => FACEBOOKSECRET,
'cookie' => false,
));
$token = $oauth['oauth_access_token'];
try {
$result = $facebook->api(
'/me/feed/',
'post',
array('access_token' => $token, 'message' => str_replace($link,'',$msg), 'link' => $link)
);
}
catch(FacebookApiException $e) {}
?>
I had the same issue.
Go to Facebook Object Debugger, verify every issue pointed there, fix them all. This solved my problem.
http://goo.gl/ASBsAa
Testing your query with the facebook Graph API Explorer worked fine for me.
You could try to add the additional parameter 'picture' to your post and place the link to your image in it.
So facebook is forced to use this as image in the post.
$result = $facebook->api(
'/me/feed/',
'post',
array('access_token' => $token, 'message' => str_replace($link,'',$msg), 'link' => $link, 'picture' => 'http://images.ephotozine.com/gallery/2011/49/normal/52194_1323334048.jpg')
);
Reading through all the comments here, I think what's happened is the following.
Let me know how close I got. This is a shot in the dark. ;)
The first time you shared a URL to Facebook, the og:image wasn't set up perfectly, and Facebook selected a random image on the page, and cached it.
You then fixed the og:image tag, and tried to check the same URL.. FB then regurgitated the cached image.
I tried with both your links just now, and everything seems fine.
:{D
Just be patient. Facebook needs some time to crawl this image. It will not immediately show up in Facebook. If your image is not showing up in some days, just drop a note here.
Much testing and trying other bulk uploaders etc. Finally just decided to email facebook with details and screnshot. next day the issue has been resolved. So, make sure to try that from the help email through your facebook account. worked for me.

Facebook - Retrieving recent posts from FB API in PHP

I am working on a script to pull recent Facebook posts/status updates for a specific using the Facebook API. I downloaded the FB SDK and searched all over the FB developers site, SO, and other places and it seems like the code below should accomplish what I need. However, although I am not getting an error, I am getting a NULL result.
require 'facebook-php-sdk/src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'XXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXX',
));
$fbApiGetPosts = $facebook->api('***My Name***/feed?limit=5');
var_dump($fbApiGetPosts["data"]);
I have tried using my two FB accounts,(my personal acct and a test account that I created just for this project) as well as one for my company and each time the response was NULL. When I misspelled the account name or used a name that did not exist I got an error, which seems to suggest the request is going somewhere.
In the code above, I also tried var_dump-ing $fbApiGetPosts and that result was also null
Can anybody see what the missing piece of the puzzle is?
I've not used the Facebook SDK, but here is how I retrieve Facebook posts:
$appId = 'XXXXXXXXXXXX';
$token = 'XXXXXXXXXXXXXXXXXXXXXXXX';
$posts = json_decode(
file_get_contents('https://graph.facebook.com/' . $appId . '/feed?
access_token=' . $token
)
);
var_dump($posts->data);
Hopefully this helps!
EDIT:
The token is just called an "Access Token". Here are the instructions for obtaining one. You might find the Access Token Tool helpful, but it's more for debugging existing tokens then creating new ones.
Just use the PHP SDK :
download link : https://developers.facebook.com/docs/php/gettingstarted/
Log your app :
$facebook = new Facebook(array('appId' => 'XXXXX','secret' => 'XXXXX'));
$facebook->setAccessToken('stored access token');
then :
$fbApiGetPosts = $facebook->api('/me/feed');
print_r($fbApiGetPosts);
Here is the Graph doc : https://developers.facebook.com/docs/graph-api/reference/
and the /feed ref : https://developers.facebook.com/docs/graph-api/reference/user/feed/

Post Photos as Fan Page not as User (Post as User Working 100%)

I´m trying to post Photos on my Fan Page using Facebook API but the Posts appears as my User and not as Fan Page it self.
I know i should use the Page Access Token instead of the App User Access Token but when i put the Page Access Token i get:
Fatal error: Uncaught OAuthException: A user access token is required to request this resource.
What should i do to post the Image as Fan Page itself not as the app Owner ?
Here´s the Working Code:
require_once('src/facebook.php');
$pageid = "xxxxxxxxxxxxx";
$accessToken = "xxxxxxxxxxxxxxxx";
$facebook = new Facebook(array(
"appId" => "xxxxxxxxxxxxxx",
"secret" => "xxxxxxxxxxxxxxxxxxxx",
"cookie" => true,
));
$args = array(
'access_token' => $accessToken,
"url" => "http://example.com/image.jpg",
"message" => "Picture Caption",
);
$facebook->api("/$pageid/photos?access_token='.$accessToken","post",$args);
I just figure it out what was wrong. I was using wrong Page Access Token, the right way to get the right Page Access Token is through the following link:
https://graph.facebook.com/me/accounts?access_token=USER_ACCESS_TOKEN
You can even try the new Access Token on Debug Tools:
https://developers.facebook.com/tools/debug/
If you do that you´ll see a field named "Profile ID", this field has to be the same as your Fan Page ID, and probably will.
Now all my post appear as my Fan Page.
Hope someone else find this usefull.

offline fb posting not working

I have to post on a users wall when he is offline. I went through number of posting and checked the code written below seems ok and also not throwing error and complete successful yet I am unable to see posting on users wall. Pl help.
$facebook = new Facebook(
array(
'appId' => "446262598768110",
'secret' => "<REMOVED>",
)
);
$facebook->setAccessToken("<REMOVED>");
//create message with token gained before
echo " lets try posting";
$access_token = $facebook->getAccessToken();
$user_id = $facebook->getUser();
echo "\ndone3..... $access_token.-----.$user_id.----new\n";
$post = array(
'message' => 'This message is posted with access token - ' . date('Y-m-d H:i:s')
);
//and make the request
$res = $facebook->api("/me/feed", 'GET', $post); //have tried both Post and Get method but result remain same
offline access was removed quite a few months ago. also, you do have to "POST" to facebook graph, everytime you want to publish something.
without seeing the result of the facebook graph, it's kind of impossible to help you at all.
nevertheless: double check, that you're using the correct access token. you have to extend the short lived access tokens by your own (https://developers.facebook.com/docs/howtos/login/extending-tokens/), so that the access token itself is still valid when you want to post by cron-job.
if you're trying to post by using the app access token, you can't use "/me/feed" at all.

Categories