Post to FB Page as User - php

So I have a Facebook Page (let's call it X), which has app Y on it. The user can ask a question via Y, and it gets posted to X as the user (not as the app).
My permissions for the app are currently set to publish_stream.
I can grab a token via
$token_url = "https://graph.facebook.com/oauth/access_token?" .
"client_id=" . $this -> data["environment"] -> fb_appid .
"&client_secret=" . $this -> data["environment"] -> fb_appsecret .
"&grant_type=client_credentials";
$app_token = file_get_contents($token_url);
which gives me a token just fine.
Now, if I try to POST via an APi call, I get two results:
When I do not pass the token and simply call
$post_id = $this ->Facebook->fb_api("/PAGE_ID/feed", "POST", array("message"=>"This is a post from PHP."));
I get a response back in the form of JSON
{
"id": "PAGEID_someOtherID"
}
but I do not see the post on the wall.
When I do pass the access token, ala
$post_id = $this ->Facebook->fb_api("/PAGE_ID/feed", "POST", array("access_token"=>$app_token,"message"=>"This is a post from PHP."));
my response comes back empty.
What am I doing wrong with such a simple concept?

Sample from Facebook docs
https://developers.facebook.com/docs/reference/php/facebook-api/
<?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_once('php-sdk/facebook.php');
$config = array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
?>
<html>
<head></head>
<body>
<?
if($user_id) {
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {
$ret_obj = $facebook->api('/'.$pageid.'/feed', 'POST',
array(
'link' => 'www.example.com',
'message' => 'Posting with the PHP SDK!'
));
echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';
} catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
$login_url = $facebook->getLoginUrl( array(
'scope' => 'publish_stream'
));
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
// Give the user a logout link
echo '<br />logout';
} else {
// No user, so print a link for the user to login
// To post to a user's wall, we need publish_stream permission
// We'll use the current URL as the redirect_uri, so we don't
// need to specify it here.
$login_url = $facebook->getLoginUrl( array( 'scope' => 'publish_stream' ) );
echo 'Please login.';
}
?>
</body>
</html>
Well you are not able to post something on a page wall without access_token Cause without access token facebook can not realize Who is The user who wants to post & about the time you are trying to post via access_token i think you are doing something wrong please check permissions you have (you need publish_stream not stream_publish) i will put a sample code for you soon

Related

Facebook PHP SDK: How can I post on page on behalf of page

I'm using facebook's php SDK and I'm trying to post on a page which is managed/administered by the logged in user. I've granted the publish_stream and manage_pages permission. And I want that the post should look like it is made by page rather than admin or logged in user. I've tried several helps but no one is working. Here is my part of my existing code:
require './php-fb-sdk/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook( array('appId' => 'xxxx', 'secret' => 'zzzz'));
// Get User ID
$user = $facebook -> getUser();
// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook -> api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
$params = array("scope" => array("manage_pages", "publish_stream"));
// Login or logout url will be needed depending on current user state.
if ($user) {
// Fetch the viewer's basic information
$basic = $facebook -> api('/me');
$permissions = $facebook -> api("/me/permissions");
if (array_key_exists('manage_pages', $permissions['data'][0]) && array_key_exists('publish_stream', $permissions['data'][0])) {
$admin_pages = $facebook -> api(array('method' => 'fql.query', 'query' => "SELECT page_id, type from page_admin WHERE uid=me() and type!='APPLICATION'"));
if (count($admin_pages) > 0) {
$post_info = $facebook -> api('/' . $admin_pages[0]["page_id"] . '/feed', 'post', array("caption" => "From web", "message" => "This is from my web at: " . time()));
echo '<hr>The post info is: ' . print_r($post_info, true) . '<hr>';
} else {
echo '<hr> You are not admin of any fb fan page<hr>';
}
//print_r($admin_pages);
} else {
// We don't have the permission
// Alert the user or ask for the permission!
header("Location: " . $facebook -> getLoginUrl(array("scope" => "manage_pages,publish_stream")));
}
$logoutUrl = $facebook -> getLogoutUrl();
} else {
//$statusUrl = $facebook->getLoginStatusUrl();
$loginUrl = $facebook -> getLoginUrl($params);
$statusUrl = $loginUrl;
}
Using the above code I can post on the page but it looks like it is made by user.
However if I use facebook JS SDK then I see the post looks like it is made by the page.
var data=
{
caption: 'My Caption',
message: 'My Message'
}
FB.api('/' + pageId + '/feed', 'POST', data, onPostToWallCompleted);
}
Any help or suggestions will be greatly appreciated.
To post on a page on behalf of the page, you need to use the page access token. Your current call:
$facebook -> api('/' . $admin_pages[0]["page_id"] . '/feed', 'post', array("caption" => "From web", "message" => "This is from my web at: " . time()));
is using the default (user) access token.
To get the page access token, make this call before publishing post:
\GET /{page-id}?fields=access_token
this will get you a page access token in the result, then simply use this to make your publishing feed call, just like this-
$facebook -> api(
'/' . $admin_pages[0]["page_id"] . '/feed',
'post',
array(
"caption" => "From web",
"message" => "This is from my web at: " . time(),
"access_token" => '{page_access_token}'
)
);
(If needed you can also get a never expiring token of your page, check here how to: What are the Steps to getting a Long Lasting Token For Posting To a Facebook Fan Page from a Server)

Facebook PHP SDK - How to get access token?

I'm trying to post on user's Facebook wall from my app. The user granted the permissions to the app to post on his wall, and I have userid in db. I need to send the post automatically, without the user logging in again.
My code is:
try{
require_once(dirname(__FILE__) . '\lib\facebook-php\src\facebook.php' );
}
catch(Exception $o){
print_r($o);
}
$config = array(
'appId' => '123456',
'secret' => '454544',
'allowSignedRequest' => false // optional but should be set to false for non-canvas apps
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$user_id = '123456';
if($user_id) {
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {
$ret_obj = $facebook->api('/me/feed', 'POST',
array(
'access_token' => $facebook->getAccessToken(),
'link' => 'www.example.com',
'message' => 'Posting with the PHP SDK!'
));
echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';
// Give the user a logout link
echo '<br />logout';
} catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
var_dump($e);
$login_url = $facebook->getLoginUrl( array(
'scope' => 'publish_stream'
));
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
// No user, so print a link for the user to login
// To post to a user's wall, we need publish_stream permission
// We'll use the current URL as the redirect_uri, so we don't
// need to specify it here.
$login_url = $facebook->getLoginUrl( array( 'scope' => 'publish_stream' ) );
echo 'Please login.';
}
This throws An active access token must be used to query information about the current user. error and asks for login. After I click login, the post is sucesfully posted.
How can I get the access token with userid without the user logging in?
This happens when the active access token is not available while calling the API end point /me
There are two solutions to get rig of this
use the userid instead of /me
set the access-token prior sending the api call
For the first you can make the call as
ret_obj = $facebook->api('/'.$user_id.'/feed', 'POST',
array(
'link' => 'www.example.com',
'message' => 'Posting with the PHP SDK!'
));
You dont need to send 'access_token' => $facebook->getAccessToken(),
The Second option is to do as
$access_token = $facebook->getAccessToken();
$facebook->setAccessToken($access_token);
ret_obj = $facebook->api('/me/feed', 'POST',
array(
'link' => 'www.example.com',
'message' => 'Posting with the PHP SDK!'
));
NOTE: publish_stream scope must be send while doing the login
Deprecation Notice :publish_stream was replaced by publish_actions, check the facebook doc for permission
you have to get permission .
Check it out here
https://developers.facebook.com/docs/reference/login/

Error trying to log in to facebook using php

I am trying to create simple php login for using facebook php-sdk. I am using example php code from facebook developers site:
<?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_once('php-sdk/facebook.php');
$config = array('appId' => 'YOUR_APP_ID','secret' => 'YOUR_APP_SECRET', 'allowSignedRequest' => false // optional but should be set to false for non-canvas apps);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
?>
<html>
<head></head>
<body>
<?php
if($user_id) {
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {
$ret_obj = $facebook->api('/me/feed', 'POST',
array(
'link' => 'www.example.com',
'message' => 'Posting with the PHP SDK!'
));
echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';
// Give the user a logout link
echo '<br />logout';
} catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
$login_url = $facebook->getLoginUrl( array(
'scope' => 'publish_stream'
));
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
// No user, so print a link for the user to login
// To post to a user's wall, we need publish_stream permission
// We'll use the current URL as the redirect_uri, so we don't
// need to specify it here.
$login_url = $facebook->getLoginUrl( array( 'scope' => 'publish_stream' ) );
echo 'Please login.';
}
?>
</body>
</html>
I have also tried several samples from internet and i always end up with same problem. After i grant permissions from Facebook, Facebook should return page to my page but instead it throws an error (this is translated error from my native language so it might be little bit different from actual English version): "ERROR This function is not available just now: Error processing this request. Try again later." And in URL there is "error_code=2&error_message=" if this helps anything? I have configured my app_id and app-secret in Facebook application page where also display name, email address, domain and site URL is set.
Also in that code i have set correct directory for SDK and app_id and secret_id.
Makes me wonder if i have faulty profile in Facebook apps? Every code i try gives same error. I have tried it with few different people. In users Facebook, applications section appears my application name and given permissions. Most likely this is some simple problem i just don't see, again...
And problem was that my application was not public. Why it takes several hours to find problem and when asked in here you immedialy invent solution by our self? :D

posting a php GD processed image or something else when user clicks a button in facebook application

how to post something on user wall ( may be a php processed image or something text ) when user clicks a button. As an example suppose a facebook user installs my app, then it shows some processed image then if the user wishes to publish he clicks the publish button and get published to his wall otherwise it will not. The difficulty I faced is to to go from one page to another ( as going from index.php to upload.php) ,transferring access_token,and how to make api call to upload the image from upload.php should i have to make upload.php as index.php so as it can re authenticate the user?
You sound really confused.
There should be no difference if you name your file index.phpor upload.php.
I don't see why you would need to manually take care of the access token. Use the PHP SDK, and it is done automatically for you. The PHP SDK uses the $_SESSION variable to store it. Otherwise, you could pass it in the URL as an argument like:
upload.php?access_token=SOMETHING_REALLY_SECRET
I'd not recommend that though, since it makes your site very vulnerable to attacks.
There is a complete example in the PHP SDK documentation:
<?
// Remember to copy files from the SDK's src/ directory to a
// directory in your application on the server, such as php-sdk/
require_once('php-sdk/facebook.php');
$config = array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
'fileUpload' => true,
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$photo = './mypic.png'; // Path to the photo on the local filesystem
$message = 'Photo upload via the PHP SDK!';
?>
<html>
<head></head>
<body>
<?
if($user_id) {
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {
// Upload to a user's profile. The photo will be in the
// first album in the profile. You can also upload to
// a specific album by using /ALBUM_ID as the path
$ret_obj = $facebook->api('/me/photos', 'POST', array(
'source' => '#' . $photo,
'message' => $message,
)
);
echo '<pre>Photo ID: ' . $ret_obj['id'] . '</pre>';
} catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
$login_url = $facebook->getLoginUrl( array(
'scope' => 'photo_upload'
));
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
echo '<br />logout';
} else {
// No user, print a link for the user to login
// To upload a photo to a user's wall, we need photo_upload permission
// We'll use the current URL as the redirect_uri, so we don't
// need to specify it here.
$login_url = $facebook->getLoginUrl( array( 'scope' => 'photo_upload') );
echo 'Please login.';
}
?>
</body>
</html>

post to the fan page's wall as page from another PHP site

I know that there are lot's of questons on this, but all seem to be needing a user to be logged in... I have been using code snippets from all possible tutorials, but none seem to work.
Here is the scenario:
I have a photo community running on PHP and I have a fan page on Facebook. When an image on the main site collects a certain amount of votes a function is triggered to post the image link to the Facebook wall. The link (post) is posted as a page and not as admin. Admin of course will not be online... Is this even possible to do these days? I have the latest PHP SDK and this is the function that I need to get working in stand alone mode before pluggin' into the main site.
OK. This code works perfectly if I am logged into the Facebook, but if I am not - it will not post... The App has all necessary and unnecessary :) permissions to interact with my page on my (admin) behalf. Any ideas will be appreciated.
<?php
//facebook application
$fbconfig['appid' ] = "1848740815xxxxx";
$fbconfig['secret'] = "a5aa62bb3a8ddcb98d5d9dbe4a3xxxxx";
$fbconfig['pageid'] = "121409594622865";
$user = null; //facebook user uid
try{
include_once "facebook.php";
}
catch(Exception $o){
error_log($o);
}
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret']
));
//Facebook Authentication part
$user = $facebook->getUser();
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'offline_access,publish_stream'
)
);
$logoutUrl = $facebook->getLogoutUrl();
$pageid = $fbconfig['pageid'];
if ($user) {
try {
$page_info = $facebook->api("/$pageid?fields=access_token");
if( !empty($page_info['access_token']) ) {
$args = array(
'access_token' => $page_info['access_token'],
'message' => 'This is a test feed message',
'link' => 'http://www.fotodvor.com',
'picture' => 'http://www.fotodvor.com/data/media/15/1319971991.jpg',
'name' => 'Test Picture',
'description'=> 'Description of the test picture!'
);
$post_id = $facebook->api("/$page_id/feed","post",$args);
}
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
?>
Thanks in advance
here is a modified script with explanations adjusted for the changes in the API
<?php
//facebook application
$fbconfig['userid'] = "5332534xx";
$fbconfig['appid' ] = "184874081548xxx";
$fbconfig['secret'] = "a5aa62bb3a8xxxb98d5d9dbe4a368xxx";
$fbconfig['pageid'] = "121409594622xxx";
$fbconfig['token1'] = "AAACoJFn1eLABAKpL9W0nZBrw0e3zzdSNVsTg6FWDMhSnOUeinjid6yAQ2z9JDxxxxxxc1hMHBC3GG18KZBwppGDehWMEwLe56wagZDZD"; // step 1 - returned by loggin in.
$fbconfig['token2'] = "AAACoJFn1eLABAC2Q8OLnqxjUSKzdn9CzaXhy8nsG61vzp2ufePr5iwHZA7TM7Ibxxxxxxyf868O04FeBxMrIo0RCumrNaB78hZAp2uRrbVlGVPXP"; // step 3 - this is a page access token as page
$fbconfig['my_ulr'] = 'http://'.$_SERVER['SERVER_NAME'];
include_once "facebook.php";
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
));
// 1. we need to get a user access_token first, (old approach with offline_access forces the tokens received not to expire (good examples here - http://developers.facebook.com/docs/authentication2/ and http://www.howtobe.pro/tag/graph-api)
// run the file and see step 1 instructions.
//offline_access has been deprecated in May 2012 and therefore excluded from the request below
$token_url1 = "https://www.facebook.com/dialog/oauth?"."client_id=".$fbconfig['appid']."&redirect_uri=".urlencode($fbconfig['my_ulr'])."&scope=manage_pages,publish_stream&response_type=token";
echo "<h2>This little working example should give you a working non expiring token to use in your PHP script to enable it to post to your Facebook page as a page and not as a user</h2><br>";
echo "1 - Click on the link below (this redirects to uri with token attached). Log in as admin of the page you are trying to post to. Then copy the token you will get in the address bar to be used in the script in step 2.<br>";
echo "<a href='".$token_url1."' target='_blank'>$token_url1</a>";
//2. then paste the token you received into "step 1" variable in the config section above. Run this script again when logged in to receive all info.
$token_url2 = "https://graph.facebook.com/me?access_token=".$fbconfig['token1'];
$me = json_decode(file_get_contents($token_url2), true);
//echo "<hr><br>this URL gives you all pages that you as admin have access to, but these are NOT what we need to post to the fan page as PAGE so this is just for the heck of it...<br>";
//echo "<a href='".$token_url2."' target='_blank'>$token_url2</a>";
//echo "<hr>this is a raw server reply<br>";
//echo d($me['id'])."<hr>";
//new changes to API - https://developers.facebook.com/roadmap/offline-access-removal/#extend_token
$new_token_url2 = "https://graph.facebook.com/oauth/access_token?client_id=".$fbconfig['appid']."&client_secret=".$fbconfig['secret']."&grant_type=fb_exchange_token&fb_exchange_token=".$fbconfig['token1'];
$new_token2 = file_get_contents($new_token_url2);
$vars = explode('&', $new_token2);
//d($vars);
echo "2. We now obtain a long lasting token (based on <a href='https://developers.facebook.com/roadmap/offline-access-removal/#extend_token' target='_blank'>this</a>)
<br><br>We send the request<br>'".$new_token_url2."'<br><br>and the reply is:<br>'".$new_token2."'<br><br>";
//http://developers.facebook.com/tools/explorer?method=GET&path=533253476&accounts&access_token=AAACoJFn1eLABAFZBftV0vtlBiHKA7ZAIrukrriyp2coWSavj3L4CbfJ9r3WY76IPi7pwUgt3wsubaI4iBbsr663PrbNyaLdZAhLxneOLAZDZD
echo"So now open this page <a href='http://developers.facebook.com/tools/explorer' target='_blank'>http://developers.facebook.com/tools/explorer</a>,
then put the token above to put into the 'Access Token: ' field and press enter... You will need to press 'accounts' link to the right from the response window.
<br>you will see another response and copy your page access token from there. Automating this task into one query did not work... I tried many times.. the token returned IS NOT THE SAME as you would get following the instructions step by step...
This approach does not work - <br>";
echo "http://developers.facebook.com/tools/explorer?method=GET&path=".$me['id']."%2Faccounts&".$vars[0]."<BR><BR><BR>";
echo "Please check it here <a href='http://developers.facebook.com/tools/debug' target='_blank'>https://developers.facebook.com/tools/debug</a> and make sure it never expires...";
$pageid = $fbconfig['pageid'];
try {
//Step 3. Run this script WHEN LOGGED IN to and paste the resulting token into step 3 variable above to check the functionality
/* $page_info = $facebook->api("/$pageid?fields=access_token&".$new_token2); //wrong approach to use this straight. THIS is the access token is that we need. BUT this will work only if user is logged in. so
echo "<hr>this is a page_info breakdown";
d($page_info);
echo "and the access token you needs to paste into fbconfig['token2'] variable is this:<br>";
echo $page_info['access_token']; */
$args = array(
'access_token' => $fbconfig['token2'], //do not attempt to plug the $page_info['access_token'] here... it will be empty once you log off Facebook
'message' => 'This is a test feed message',
'link' => 'http://www.test.com',
'picture' => 'https://www.google.com/intl/en_com/images/srpr/logo3w.png',
'name' => 'Test Picture',
'description'=> 'Description of the test picture!'
);
//uncomment this once you are ready to post and you can see all the access token in the last step. Then comment out all echo and d()'s to make the script silent...
//$post_id = $facebook->api("/$pageid/feed","post",$args);
echo "<hr>This will show once the message is posted - post_id is: <br>";
d($post_id);
} catch (FacebookApiException $e) {
error_log($e);
}
function d($d){
echo '<pre>';
print_r($d);
echo '</pre>';
}
?>
I think I have found the answer. After a long time of testing here is the solution:
Treat this as a little sample/guide for those who are new to this.
The code and output has all necessary info:
<?php
//facebook application
$fbconfig['appid' ] = "184874081XXXXXX";
$fbconfig['secret'] = "a5aa62bb3a8ddcb98d5d9dbe4aXXXXXX";
$fbconfig['pageid'] = "121409594XXXXXX";
$fbconfig['token1'] = "AAACoJFn1eLABAHn92JsWIHZCESQWXmkXZBCedXXXXXXcyUG5vrCYZBXcgsNHN0IUvBj0Sec9vOxVsUgtMHflXXF2cbOF1oZD"; // step 1 - returned by loggin in.
$fbconfig['token2'] = "AAACoJFn1eLABAAUAPthH5DaZCmasZCh5DGGSnZXXXXXXSDh8v1WYYUEWJYuFdua9E5EfJ63c03lfwXrVJbP4VQj35aVcztFgKRYZAheHPNfDeLfbkPys"; // step 3 - this is a page access token as page
$fbconfig['my_ulr'] = 'http://'.$_SERVER['SERVER_NAME'];
include_once "facebook.php";
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
));
// 1. we need to get a user access_token first, offline_access forces the tokens received not to expire (good examples here - http://developers.facebook.com/docs/authentication2/ and http://www.howtobe.pro/tag/graph-api)
//run the file and see step 1 instructions.
$token_url1 = "https://www.facebook.com/dialog/oauth?"."client_id=".$fbconfig['appid']."&redirect_uri=".urlencode($fbconfig['my_ulr'])."&scope=manage_pages,offline_access,publish_stream&response_type=token";
echo "1 - this redirects to uri with token attached. Copy and paste this line into your browser and log in as admin of the page you are trying to post to. Make sure you change redirect_uri to your own. Then copy the token you will get in the address bar to be used in the script in step 2.<br>";
echo $token_url1;
//2. then paste the token you received into "step 1" variable in the config section above. Run this script again when logged in to receive all info.
$token_url2 = "https://graph.facebook.com/me/accounts?access_token=".$fbconfig['token1'];
$app_token2 = file_get_contents($token_url2);
echo "<hr><br>2 - this URL gives you all pages that you as admin have access to, but these are NOT what we need to post to the fan page as PAGE<br>";
echo $token_url2;
echo "<hr>2 - this is a raw server reply<br>";
d($app_token2);
$pageid = $fbconfig['pageid'];
try {
//Step 3. Run this script WHEN LOGGED IN to and paste the resulting token into step 3 variable above
$page_info = $facebook->api("/$pageid?fields=access_token"); //wrong approach to use this straight. THIS is the access token is that we need. BUT this will work only if user is logged in. so
echo "this is a page_info breakdown";
d($page_info);
echo "and the access token you needs to paste into fbconfig['token2'] variable is this:<br>";
echo $page_info['access_token'];
$args = array(
'access_token' => $fbconfig['token2'], //do not attempt to plug the $page_info['access_token'] here... it will be empty once you log off Facebook
'message' => 'This is a test feed message',
'link' => 'http://www.test.com',
'picture' => 'https://www.google.com/intl/en_com/images/srpr/logo3w.png',
'name' => 'Test Picture',
'description'=> 'Description of the test picture!'
);
//uncomment this once you are ready to post and you can see all the access token in the last step. Then comment out all echo and d()'s to make the script silent...
//$post_id = $facebook->api("/$pageid/feed","post",$args);
echo "<hr>This will show once the message is posted - post_id is: <br>";
d($post_id);
} catch (FacebookApiException $e) {
error_log($e);
}
function d($d){
echo '<pre>';
print_r($d);
echo '</pre>';
}
?>
You should be using the page access token if i'm understanding what you're trying to do correctly - this is a token which allows your app to act as the page, not as one of the admins.
This is accessible at the /me/accounts endpoint when you have a user access token with manage_pages permission - if you use that access token to post to /{page id}/feed (or photos, etc) it'll appear as the page

Categories