Facebooks documentation is inadecuate, confusing at best.
I have created an app, I have created a page for my business. I want to post updates on that page from my website as my App using the php-sdk.
I had this working perfectly with other pages/sites but nothing seems to be working any more.
I require offline_access, so I have followed the new endpoint as suggested in their docs.
Sequence of obtaining access_token with longer expiry:
https://www.facebook.com/dialog/oauth?client_id={my_app_id}&redirect_uri=http://my_url.redirect/fb/fb_token.php&scope=publish_stream,offline_access,user_status,read_stream,email,user_groups,manage_pages&response_type=token
returned [TOKEN_1]
https://graph.facebook.com/oauth/access_token?client_id={my_app_id}&client_secret={my_app_secret}&grant_type=fb_exchange_token&fb_exchange_token=[TOKEN_1]
returned [ACCESS_TOKEN]
https://graph.facebook.com/me/accounts?access_token=[ACCESS_TOKEN]
returns list of my pages and apps and their relevant ids and access codes. This is where I got my final [DEFINITIVE_ACCESS_TOKEN] && [PAGE_ID]
$facebook = new Facebook(array(
'appId' => {my_app_id},
'secret' => {my_app_secret},
'cookie' => false,
'oauth' => true
));
try {
$attachment = array(
'access_token' => [DEFINITIVE_ACCESS_TOKEN],
'message' => {my_message},
'name' => {my_title},
'link' => {my_url},
'description' => {my_description},
'picture' => {my_image_url},
);
$publishStream = $facebook->api('/[PAGE_ID]/feed', 'POST', $attachment);
} catch (FacebookApiException $e) {
die($e);
}
This has returned an error message "OAuthException: (#200) Posts where the actor is a page cannot also include a target_id"
Where am I going wrong? Is there a tutorial of getting this to work perfectly anywhere or are we supposed to all be mind readers and guess how it is supposed to be done?
Thanks in advance for any help.
I have noticed you are using offline access
Offline access will no longer be available as of May 1, 2012.
See this
https://developers.facebook.com/roadmap/offline-access-removal/
Make sure you have handling for this.
~K
Related
I have a script for posting to managed pages which works unless i try and schedule the post. It fails on
Array ( [error] => Array ( [message] => An unexpected error has occurred. Please retry your request later. [type] => OAuthException [code] => 2 ) )
I came across this Facebook Graph API error when posting scheduled post on Business page stating it could be a issue with the "link" but not specifying which link. But i tried all of them but still getting the same errors.
Also on a side question....do page access tokens expire? There seems to be some confusion over these.
foreach($_POST['fb'] as $key=>$code){
$facebookpage= $key;
$facebookaccesstoken= $code;
$newfacebook = new Facebook(array(
'appId' => $appId,
'secret' => $appSecret
));
$time= time();
$newtime= $time +13600;
$attachment=array(
'access_token'=> $facebookaccesstoken,
'published' => false,
'scheduled_publish_time'=> $newtime,
'message'=> "new sasa posting me!",//user message
'name'=> 'Anyone sasa interested in this? Check out my advert!',//title
'link'=> 'http://testserverdavideec.mx.nf/wp-content/uploads/2014/05/user53734efe383247.67413395boats.jpg', // link to advert
'description'=> 'myadvert asa brings the milkshakes',//inner ad
'picture'=>'http://testserverdavideec.mx.nf/wp-content/uploads/2014/05/user53734efe383247.67413395boats.jpg',//picture url if empty= site logo
'actions'=>array(array(
'name'=>'dshjs',
'link'=>'http://testserverdavideec.mx.nf/wp-content/uploads/2014/05/user53734efe383247.67413395boats.jpg'
))
);
$post_url='/'.$facebookpage.'/feed';
try{
$result=$newfacebook->api($post_url,'POST',$attachment);
echo '<h1> result=</h1>'; var_dump($result);
} catch (FacebookApiException $e) {
print_r($e);
}
}
According to the official documentation it's NOT SUPPORTED to use published with actions field. So you can use either scheduled_publish_time or actions but both won't go.
I hope it helps.
I'm trying to add an event to a page in the name of the page. I'm the admin of the page and I found a lot of stuff to the topic, but, no matter what I do, it ends in the PHP Exception:
Uncaught Exception: You must be an admin of the specified page to perform the requested action.
I can add events with my app in my own profile. So, I think the problem is the Access Token. I tried several ways, but to me the best way seems to be:
Open the Graph API Explorer
Select my Application
Request a new access code with permissions manage_pages, publish_actions, publish_stream, create_event
Request /me/accounts
Get the access_token from the page I want to post to and use it in my script
My test script, that works in my own profile:
require 'src/facebook.php';
$facebook = new Facebook(array(
'appId' => $APPLICATION_ID,
'secret' => $APPLICATION_SECRET,
'cookie' => false,
'fileUpload' => true
));
$event_id = 0;
$event_name = "New Event API Test Event";
$event_start = '2013-12-04';
$event_privacy = "OPEN";
$ret_obj = $facebook->api('/'.$page_id.'/events?access_token='.$access_token, 'POST',
array(
'name' => $event_name,
'start_time' => $event_start,
'privacy_type' => $event_privacy,
'location_id' => $location_id
));
if(isset($ret_obj['id'])) {
echo 'Event with added. ID: ' . $ret_obj['id'];
} else {
echo 'Couldn\'t create event';
}
What I did wrong?
You need a page access token.
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.
Doc: https://developers.facebook.com/docs/facebook-login/access-tokens/
Then use GET /me/accounts to get the access token associated to your page.
I found the solution in How to programmatically add an event to a page using Graph API?. I had to add the access_token direct into the event array and everything works as expected:
$ret_obj = $facebook->api('/'.$page_id.'/events', 'POST',
array(
'access_token' => $access_token,
'name' => $event_name,
'start_time' => $event_start,
'privacy_type' => $event_privacy,
'location_id' => $location_id
));
Thanks for the help!
I have the follow code to login with Facebook:
// Include Facebook SDK
$this->facebook = new Facebook(array(
'appId' => 'APP ID',
'secret' => 'APP SECRET',
'cookie' => true
));
$uid = $this->facebook->getUser();
if($uid) {
try {
echo 'Done!';
} catch (FacebookApiException $e) {
echo $e;
error_log($e);
}
} else {
$loginUrl = $this->facebook->getLoginUrl(array(
'redirect_uri' => 'http://ogabrielsantos.com.br/dev/index.php?route=account/connect/facebook',
'scope' => 'user_birthday'
));
echo('<script>top.location.href = "' . $loginUrl . '";</script>');
}
UPDATE 1
I can authorize the App, but, after authorization, I can't get the logged user, EVER redirecting to
http://ogabrielsantos.com.br/dev/index.php?route=account%2Fconnect%2Ffacebook&state=RANDOM STATE&code=RANDOM CODE#_=_
I have tried:
Put the APP in a server;
Verify APP settings. All is correct;
Verify if url is the same at settings and code. All is ok;
My url is properly written: http://ogabrielsantos.com.br/dev/index.php?route=account/connect/facebook;
Reset APP SECRET.
Nothing worked.
You can check what happens going to:
http://ogabrielsantos.com.br/dev/index.php?route=account/connect/facebook
Facebook ask you to authorize the app to get your website, which is a safe information.
UPDATE 2
The follow code:
$this->facebook = new Facebook(array(
'appId' => $this->connect->config->get('facebook_api_key'),
'secret' => $this->connect->config->get('facebook_app_secret'),
'cookie' => true
));
$uid = $this->facebook->getUser();
echo'<pre>';
print_r($this->facebook);
echo'</pre>';
exit;
returns:
Facebook Object
(
[sharedSessionID:protected] =>
[appId:protected] => APP ID
[appSecret:protected] => APP SECRET
[user:protected] => 0
[signedRequest:protected] =>
[state:protected] =>
[accessToken:protected] => APP ID|APP SECRET
[fileUploadSupport:protected] =>
[trustForwarded:protected] =>
)
I think the accessToken is wrong, returning my APP ID and SECRET with a pipeline separator.
UPDATE 3:
Now, I verify the code, but, always get Fatal error: Uncaught OAuthException: Error validating verification code
if(isset($_GET['code'])) {
$api = $this->facebook->api('/oauth/access_token', array(
'client_id' => APP ID,
'redirect_uri' => 'http://ogabrielsantos.com.br/dev/index.php?route=account/connect/facebook',
'client_secret' => APP SECRET,
'code' => $_GET['code']
));
echo'<pre>';
var_dump($api);
print_r($this->facebook->getUser());
echo'</pre>';
exit;
}
I have read some questions, which is similar but can't help-me.
Are you working on localhost? if yes it probably is the reason why.
If not, check your app settings in developers.facebook.com/apps/APP_ID
1. You have to compare the urls written in your code with the ones you have in your app settings.
2. Another reason could be the that the urls in you app settings are not properly written, I remember having to add index.php at the end of my urls in Page Tab settings. Worked for me
3. Another option is to get a new APP_SECRET and update your code
These options are from my personal experience, you issue could also be something else but you lose nothing trying
The Facebook Sharing and Facebook Sign In functions are completely separate and each require their own app.
Are you using facebook sharing in the page you want the login working on?
Ref: http://www.ning.com/help/?p=8523
I am trying to implement a functionality to publish post to Facebook Fan Page from Admin page (SonataAdminBundle) for Symfony 2 Framework. I've integrated Facebook PHP SDK and in Controller tried to use this:
$facebook = new \Facebook(array(
'appId' => $myAppId,
'secret' => $myAppSecret,
'cookie' => false,
));
//Get App Token
$token = $facebook->getAccessToken();
//Try to Publish on wall or catch the Facebook exception
try {
$args = array('access_token' => $token,
'from' => $myAppId,
'link' => 'http://mypage.pl',
'name' => 'My Page',
'caption' => $object->getTitle() ,
'description' => 'Description....',
'message' => $object->getText(),
);
$result = $facebook->api('/'.'$myAppId'.'/feed/', 'POST', $args);
}
//If the post is not published, print error details
catch (FacebookApiException $e) {
....
));
}
I am getting a response with id of app and a post id, I guess, like this:
{"status":"OK","content":{"id":"295131140562739_359030107506176"}}
But the post isn't shown on Facebook Fan Page Timeline. Any one implemented it with success?
$result = $facebook->api('/'.'$myAppId'.'/feed/', 'POST', $args);
The above doesn't produce the desired effect. As Darvex mentioned, you probably don't want your app id there, but the page id. Also, putting a variable between single quotes in PHP will make it a string with the value $myAppId, not the value of the variable. That line should become:
$result = $facebook->api('/'. $myPageId .'/feed/', 'POST', $args);
Just make sure you assign the correct id to $myPageId before the call :)
I'm just trying to make PHP Connector to Facebook for posting wall posts on a Page.
So i'm not trying to post wall post to profile wall or anything else.
I've read some tutorials and manuals and i decided to use Facebook PHP-SDK (from Naitik Shah)
https://github.com/facebook/php-sdk/
I've created Facebook App to post wallposts through it. I received appId and api secret. I've added application permissions to my Page and tried example code
$facebook = new Facebook(array(
'appId' => 'my app id',
'secret' => 'my api secret',
'cookie' => false,
'domain' => 'domain.com'
));
domain.com => domain from which i'm sending api requests
next ->
$facebook->getSession();
$token = $facebook->getAccessToken();
$facebook->api('/123456789/feed', array(
'access_token' => $token,
'link' => 'http://www.example.com'
));
So i'm trying to post link on wall of page with id 123456789
The request goes through without warnings/errors but nothing is posted in right place and nothing is returned.
Thanks for any idea about this problem.
Used tutorials:
How do you post to the wall on a facebook page (not profile)
http://blog.theunical.com/facebook-integration/5-steps-to-publish-on-a-facebook-wall-using-php/
http://www.moskjis.com/other-platforms/publish-facebook-page-wall-from-your-site
http://tips4php.net/2010/12/automatic-post-to-facebook-from-php-script/
$facebook->api('/123456789/feed', 'post', array(
'access_token' => $token,
'link' => 'http://www.example.com'
));
Note the 'post' part.
If you look at the source for the API via the link you provided, you'll see:
protected function _graph($path, $method='GET', $params=array()) {
if (is_array($method) && empty($params)) {
$params = $method;
$method = 'GET';
}
When you don't have 'post' as the second argument and your array as the third, it goes a get
If you are getting authorisation errors, make sure you have included the following permission:
publish_stream
https://developers.facebook.com/docs/reference/api/post/