I cannot understand what is the problem in my code. I have 'publish_stream' in my scope. The user has authenticated my app with all the permissions, but still this error shows up.
The folllowing is my code to post via the graph api :-
<?php
try{
$publishStream = $facebook->api("/$user/feed", 'post', array(
'message' => 'TESTING...',
'link' => 'http://example.com',
'picture' => 'http://example.com/image.png',
'name' => 'example name!',
'caption' => 'example caption',
'description' => 'PHP SDK ROCKS!',
));
echo "IN TRY BLOCK ";
}catch(FacebookApiException $e){
error_log($e);
echo "IN CATCH BLOCK ";
}
?>
The output for the given code is :- IN CATCH BLOCK. Please tell me where am I wrong...
Based on Facebook Graph API documentation: 1) Check $user variable so that surely contains the (clean) Profile_ID, 2) You need to have "publish permission" on the targeted profile, see: http://developers.facebook.com/docs/concepts/login/permissions-login-dialog/#publishing-permissions
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.
After I was searching how to post data to facebook page, now I can do it well.
<?php
// Remember to copy files from the SDK's src/ directory to a
// directory in your application on the server, such as php-sdk/
require '../src/facebook.php';
$facebook = new Facebook(array(
'appId' => '491652477512398',
'secret' => 'de3d98a619ade1afb4152d6be90acdb9',
));
$user_id = $facebook->getUser();
echo "User data ".$user_id." \n";
try {
if($user_id) {
$ret_obj = $facebook->api('/microdigit.it/feed/', 'POST',
array(
//'access_token' => 'AAACEdEose0cBAExkj6B4lnjpUtfSSVTOZA9CuvMA1SDjyCQWRnfZCbc1SimyRsZCTa6CUFZB2Q3ZBfVIv0qJmW13XkPZASpt9UDE4qQI488mDqlznDZA2ih',
'message' => '*** Microdigit Microdigit 000 ****',
'name' => 'name 777 after log out test',
'caption' => 'this is caption for action link',
'description' => 'here goes description and links http:anotherfeed.com | http://facebook.com/anotherfeed',
'picture' => 'http://www.google.com/tv/images/slivetv.png',
'link' => 'www.yahoo.com',
'properties' => array(
array('text' => 'test link 1', 'href' => 'http://anotherfeed.com/?ref=1'),
array('text' => 'test link 1', 'href' => 'http://anotherfeed.com/?ref=1'),
),
));
echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';
}else
{
$login_url = $facebook->getLoginUrl( array( 'scope' => 'offline_access,publish_stream' ) );
echo 'Please login.';
}
} catch(FacebookApiException $e) {
echo $e;
$login_url = $facebook->getLoginUrl( array(
'scope' => 'offline_access,publish_stream'
));
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
?>
But problem is I want to post
that information to page without needing any user interaction.
I mean whenever I run this code, If user is already log off,
my program need user interaction (such as login to facebook) before it post to facebook page.
So I will be appreciated every suggestion that can show me how to post data to facebook without needing any user interaction.
One way of doing it, depending on the trigger you want to use is to make use of a Cron Jobs . You can schedule to run php scripts.
I am trying to post dynamic data to facebook wall.
I downloaded facebook api for php.
First of all, as for testing, I use below code.
<?php
// Remember to copy files from the SDK's src/ directory to a
// directory in your application on the server, such as php-sdk/
require '../src/facebook.php';
$config = array(
'appId' => 'myAPPID',
'secret' => 'MySecretCode'
);
//'access_token' => $access_token = $facebook->getAccessToken(),
$facebook = new Facebook($config);
$attachment = array(
'access_token' => 'my_tokenkey',
'message' => 'Test Message offline test message',
'name' => 'Name Test offline test...',
'description' => 'here goes description and links http:anotherfeed.com | http://facebook.com/anotherfeed',
'caption' => 'this is caption for action link',
'picture' => 'http://www.google.com/tv/images/slivetv.png',
'link' => 'www.yahoo.com',
'properties' => array(
array('text' => 'test link 1', 'href' => 'http://anotherfeed.com/?ref=1'),
array('text' => 'test link 1', 'href' => 'http://anotherfeed.com/?ref=1'),
),
);
try {
$facebook->api('/me/feed', 'POST', $attachment);
} catch(FacebookApiException $e) {
echo $e;
# handling facebook api exception if something went wrong
}
?>
As for my program requirement, I want my program to post data to facebook's wall whether facebook is online or offline.
But when I try this code, I get error.
OAuthException: An active access token must be used to query information about the current user.
Could anyone please give me suggestions?
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 know this error has been bitten to death over here, and I read every single one to have better understanding of my problem.
But my issue is a bit different, and I wonder if anyone can give me good suggestion where to look.
I'm using wordpress + wordpress social login. This plugin authenticates user and stores name/age/email/fbID/fbProfilePic in the DB.
I've a little feature on my website where users registered through facebook can click and post message to the their wall.
My code looks like this:
<?php
//IF user is registered via facebook, when he clicks INTERESTED message will appear on his/her wall to spread the news
$user = get_user_meta($current_user->ID,'Facebook',true);
if ($user && $_GET['commentstab'] == 1 && !$_POST['delmycom']) {
require ('Facebook/facebook.php');
//Access details
$facebook = new Facebook(array(
'appId' => 'XXX',
'secret' => 'XXX'
//'cookie' => true
));
//try {
$params = array(
'message' => "Hurray! This works :)",
'name' => "This is my title",
'caption' => "My Caption",
'description' => "Some Description...",
'link' => "http://stackoverflow.com",
'picture' => "http://i.imgur.com/VUBz8.png",
);
$post = $facebook->api("/$user/feed","POST",$params);
echo "Your post was successfully posted to UID: $user";
//} //try
//catch (FacebookApiException $e) {
// $result = $e->getResult();
//} //catch
} //master if
?>
I read in various topics that I need publish_stream permission from user to perform this action. But since I store user info separately in my own DB, how do I get this publish stream permission? Do I need to modify wordpress plugin to store some kind of access token? and utilize this token to post to wall?
you generate loginurl with publish_stream permission
if user is not logged in with permission, you have to make sure he does
redirect user to proper page
you can do it like this,
<?php
include_once("facebook.php");
$facebook = new Facebook(array(
'appId' => APP_ID,
'secret' => APP_SECRET,
'cookie' => true,
));
$user = $facebook->getUser();
if ($user) {
try {
// Get the user profile data you have permission to view
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
$user = null;
}
} else {
$loginUrl = $facebook->getLoginUrl(array('scope' =>'publish_stream','redirect_uri'=>'example.com'));
die('<script> top.location.href="'.$loginUrl.'";</script>');
}
$params = array(
'message' => "Hurray! This works :)",
'name' => "This is my title",
'caption' => "My Caption",
'description' => "Some Description...",
'link' => "http://stackoverflow.com",
'picture' => "http://i.imgur.com/VUBz8.png",
);
$post = $facebook->api("/$user/feed","POST",$params);
?>