I am using php-sdk-4 for facebook wall posting using my app credential. Everything is working fine. Problem is only posting on user facebook wall or through user post on his facebook fan page wall working but when I used his facebook fan page secret code for publish a post on facebook fan wall then show me error :-
(#200) The user hasn't authorized the application to perform this action
My facebook app in testing mode
I am doing all these steps as admin.
In my project I want functionality user come login through facebook and select his facebook fan page and publish some text or image on his facebook fan page
Code :-
$facebook = new Facebook(array(
'app_id'=>'XXXXXXXXXX',
'app_secret'=>'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'default_graph_version' => 'v2.5'
));
$graph_url_pages = "https://graph.facebook.com/me/accounts?access_token=".$accessToken;
$pages = json_decode(file_get_contents($graph_url_pages)); // get all pages information from above url.
for($i=0;$i<count($pages->data);$i++)
{
$page_token = $pages->data[$i]->access_token;
$pageId = $pages->data[$i]->id;
$params = array(
'access_token' => $page_token,
'from' => 'XXXXXXXXXXX',
'to' => $pageId,
'message' => 'Google Inc',
'link' => 'http://www.google.com/',
);
try {
$response = $facebook->post('/'.$pageId.'/feed', $params);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$graphNode = $response->getGraphNode();
print_r($graphNode);die;
}
In code when I passed access_token then show me error but when I passed $accessToken (user access token) then user post going on facebook fan page wall but I want facebook fan page publish a post on timeline.
Related
As you know that Facebook introduced new Graph API v3.1 on July 26, 2018 that changed many things for developers. One of the questions I have now is how to share/post on Facebook profile/page timeline using Facebook Graph API v3.1 PHP SDK?
Reminder: Graph API v2.7 will be deprecated on Oct 05, 2018. Please use the API Upgrade Tool to understand how this might impact your app. For more details see the changelog
For this I created a new apps with some settings as shown in the below screenshots.
For this purpose, I used the below mentioned code along with facebook-php-graph-sdk-5.x.
fbConfig.php
<?php
if(!session_id()){
session_start();
}
// Include the autoloader provided in the SDK
require_once __DIR__ . '/facebook-php-graph-sdk-5.x/autoload.php';
// Include required libraries
use Facebook\Facebook;
use Facebook\Exceptions\FacebookResponseException;
use Facebook\Exceptions\FacebookSDKException;
/*
* Configuration and setup Facebook SDK
*/
$appId = 'APP_ID'; //Facebook App ID
$appSecret = 'APP_SECRET'; //Facebook App Secret
$redirectURL = 'MAIN_PAGE_URL_SAME_AS_IN_APPS_SETTING'; //Callback URL
$fbPermissions = array('publish_actions'); //Facebook permission
$fb = new Facebook(array(
'app_id' => $appId,
'app_secret' => $appSecret,
'default_graph_version' => 'v2.6',
));
// Get redirect login helper
$helper = $fb->getRedirectLoginHelper();
// Try to get access token
try {
if(isset($_SESSION['facebook_access_token'])){
$accessToken = $_SESSION['facebook_access_token'];
}else{
$accessToken = $helper->getAccessToken();
}
} catch(FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
?>
index.php
<?php
// Include FB configuration file
require_once 'fbConfig.php';
if(isset($accessToken)){
if(isset($_SESSION['facebook_access_token'])){
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
}else{
// Put short-lived access token in session
$_SESSION['facebook_access_token'] = (string) $accessToken;
// OAuth 2.0 client handler helps to manage access tokens
$oAuth2Client = $fb->getOAuth2Client();
// Exchanges a short-lived access token for a long-lived one
$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);
$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;
// Set default access token to be used in script
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
}
//FB post content
$message = 'Test message from stackoverflow.com website';
$title = 'Post From Website';
$link = 'http://www.stackoverflow.com/';
$description = 'stackoverflow is simply awesome.';
$picture = 'https://i.stack.imgur.com/MybMA.png';
$attachment = array(
'message' => $message,
'name' => $title,
'link' => $link,
'description' => $description,
'picture'=>$picture,
);
try{
// Post to Facebook
$fb->post('/me/feed', $attachment, $accessToken);
// Display post submission status
echo 'The post was published successfully to the Facebook timeline.';
}catch(FacebookResponseException $e){
echo 'Graph returned an error: ' . $e->getMessage();
exit;
}catch(FacebookSDKException $e){
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
}else{
// Get Facebook login URL
$fbLoginURL = $helper->getLoginUrl($redirectURL, $fbPermissions);
// Redirect to Facebook login page
echo '<img src="https://www.freeiconspng.com/uploads/facebook-login-button-png-11.png" />';
}
?>
Where as my files level are as shown in the below screenshot.
Finally after doing all of the above setting and codes, when I try to run my page then I got LOGIN FACEBOOK BUTTON and after clicking there, I got the below screenshot error.
What I want is simple post my desired content without showing any POPUP or dialog so that I can easily use it via my PHP Codes.
I tried to find any working solution over internet but all are now old, nothing is now working with new Facebook Graph API v3.1.
What I want is simple post my desired content without showing any POPUP or dialog
That is not possible anymore for user profiles. You have to use Sharing Dialogs instead. publish_actions got removed and there is no replacement or workaround.
For Pages, you can use manage_pages and publish_pages to post to a Page with a Page Token.
Yes, you should post on page feed instead of the personal feed.
As luschn said, you need manage_pages and publish_pages permission to obtain the correct page's access token.
First, you have to develop your app in dev mode, then you need to submit for App Review with those permissions.
This process requires you to upload screencasts with a demo and put the app in a public place so that the Facebook Review team can reach it and test it.
It's a long way to go..
I've created a Facebook App using the latest version of the PHP SDK (version 5) which publishes posts to my Facebook page when ran.
This is working...however the posts are going onto the page as 'private', so only I can see the posts when logged in to my personal Facebook page. the public cannot see them.
Does anyone know why this may be? Am I right in assuming I need to 'submit the App to Facebook for review' before it can send public posts to my page?
Here's my code below:
define('FACEBOOK_SDK_V4_SRC_DIR', __DIR__.'/src/facebook-sdk-v5/');
require_once(__DIR__.'/src/facebook-sdk-v5/autoload.php');
//Set your Facebook API settings
$fb = new Facebook\Facebook([
'app_id' => 'apptoken',
'app_secret' => 'appsecrettoken',
'default_graph_version' => 'v2.2',
]);
//Post property to Facebook
$linkData = [
'link' => 'http://www.google.co.uk',
'message' => 'Test message'
];
try {
$response = $fb->post('/me/feed', $linkData, 'thepageaccesstokenhere');
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: '.$e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: '.$e->getMessage();
exit;
}
$graphNode = $response->getGraphNode();
One solution is to make the app public. By default, I believe it's in development mode.
To do this, please follow below steps:
1. Go to https://developers.facebook.com
2. From the left sidebar, select "App Review"
3. Set "Make your app public?" to "Yes"
Page post made by your app should be available even if you're not logged in.
You are Facebook app & page admin. That's why you are able to post as page, but it's for test purposes only so hidden from other users. You have to apply for permission (post_as_page) to achieve this.
I'm trying to share a link on Facebook with the php-sdk. Using Graph API Explorer in https://developers.facebook.com/tools/explorer and choosing the app that I create and the admin user for the page, it give me the access token and can share the link perfectly, but from the code with the same token I always get the error
Graph returned an error: Invalid OAuth access token.
I understand that is an authentication error but, avoid the authentication is not the purpose of use the token?, if not, how can be logged the admin user for do the task and why is not mentioned in https://developers.facebook.com/docs/php/howto/example_post_links/5.0.0 .The idea is to share the link with the user page administrator, not with the user logged on my site. This is the code.
public function shareOnFacebook($link)
{
$pageId = "XXXXXXXXXXX";
$accessToken = "YYYYYYYYYYYYYY";
$appId = "ZZZZZZZZZZZ";
$appSecret = "SSSSSSSSSSSSS";
$fb = new Facebook([
'app_id' => '{'.$appId.'}',
'app_secret' => '{'.$appSecret.'}',
'default_graph_version' => 'v2.2',
]);
$linkData = [
'link' => 'http://www.example.com',
'message' => 'User provided message',
];
try {
$response = $fb->post('/'.$pageId.'/feed', $linkData, '{'.$accessToken.'}');
} catch(FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
}
You should replace the whole field with your access token:
$response = $fb->post('/'.$pageId.'/feed', $linkData, '{'.$accessToken.'}');
Should be
$response = $fb->post('/'.$pageId.'/feed', $linkData, $accessToken);
I suppose adding the access token to the $linkData should work as well.
I'm trying to publish a feed to my users' Facebook wall. I use Facebook Connect for my site. I have generated the link with getLoginUrl() with email, offline_access and publish_stream permissions.
My users' click on this link, authenticate with facebook and comes back to my site. When they do, i'm getting their's ID with getUser().
Then i'm trying to publish a post to their walls. I use this:
$user = $facebook->getUser();
try {
$publishStream = $facebook->api("/$user/feed", 'post', array(
'message' => "Mesaj icerigi",
'link' => 'http://ithinkdiff.net',
'picture' => 'http://thinkdiff.net/ithinkdiff.png',
'name' => 'iOS Apps & Games',
'description'=> 'Checkout iOS apps and games from iThinkdiff.net. I found some of them are just awesome!'
)
);
} catch (FacebookApiException $e) {
print_r($e);
}
But everytime i try to that, i got an error. Api exception or something. What i'm doing wrong?
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Facebook Graph Api - Posting to Fan Page as an Admin
I am trying to have a form on my site where I can submit a blog post and then send that same blog post to my page on facebook.
My page is: https://www.facebook.com/foreveraloneminecraft
I want it to post it as if I was logged in and manually posted (like all the other posts on there).
I have been searching all over the place and everything I find either never actually posts something or throws an error.
This is what I got so far after searching all over:
<?php
include('core/init.inc.php');
require_once('libs/parser.php'); // path to Recruiting Parsers' file
$parser = new parser; // start up Recruiting Parsers
if(isset($_POST['submit'], $_POST['user'], $_POST['title'], $_POST['body'])
{
//--- Facebook Posting ------
require_once('libs/facebook/facebook.php');
$appid = '286964398044671';
$appsecret = 'myappsecret';
$pageid = '215852885143861';
$token = 'mytoken';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => $appid,
'secret' => $appsecret,
));
// Get the current access token
//$token = $facebook->getAccessToken();
//Information that makes up the facebook page post
$attachment = array(
'access_token' => $token,
'message' => $_POST['body'],
'link' => 'http://hgs1957.hostedd.com/news.php',
'picture' => 'http://hgs1957.hostedd.com/images/foreverAloneMCguy.png',
'name' => $_POST['title'],
'description'=> 'Latest news for foreveralonemc.com.'
);
//Try to post to the facebook page
try{
$res = $facebook->api('/'.$pageid.'/feed','POST',$attachment);
} catch (Exception $e){
echo $e->getMessage();
}
//Add the post to the sql database of posts
//add_post($_POST['user'], $_POST['title'], $_POST['body']);
//Redirect to news feed
header('Location: news.php');
die();
}
?>
I used the appsecret from the token is that I got when using https://developers.facebook.com/tools/explorer/ with the permissions: publish_stream, status_update, manage_pages, publish_actions, offline_access
After trying that it seems to post to the page wall but as me not as the page or admin.
Anyone know if this is just a bug or what I am doing wrong?
You should re-login to your app again to renew and store a new access token after it is expired.
I Hope This will help you.