Reputation: 510
I am trying to figure out how I can get a page access token
using php. everything I do will not return the access token.
the first code bellow, works and it post the message as the user. but I need to post as a page and according to documentation, i will need a page access token to be able to post as a page/app.
so I've edited my working code (first code) to the second code (returns a blank page).
could someone please let me know whats missing or what I am doing wrong?
working code that posts as user:
<?php
include_once 'inc/facebook.php';
$appId = '00000000000000000';
$secret = '00000000000000000000000000000';
$returnurl = 'https://mydomain.com';
$permissions = 'manage_pages, publish_actions';
$fb = new Facebook(array('appId'=>$appId, 'secret'=>$secret));
$fbuser = $fb->getUser();
if($fbuser){
if(isset($_POST['msg']) and $_POST['msg']!=''){
try{
$message = array(
'message' => $_POST['msg']
);
$posturl = '/'.$_POST['pageid'].'/feed';
$result = $fb->api($posturl,'POST',$message);
if($result){
echo 'Successfully posted to Facebook Wall...';
}
}catch(FacebookApiException $e){
echo $e->getMessage();
}
}
try{
$qry = 'select page_id, name from page where page_id in (select page_id from page_admin where uid ='.$fbuser.')';
$pages = $fb->api(array('method' => 'fql.query','query' => $qry));
if(empty($pages)){
echo 'The user does not have any pages.';
}else{
echo '<form action="" method="post">';
echo 'Select Page: <select name="pageid">';
foreach($pages as $page){
echo '<option value="'.$page['page_id'].'">'.$page['name'].'</option>';
}
echo '</select>';
echo '<br />Message: <textarea name="msg"></textarea>';
echo '<br /><input type="submit" value="Post to wall" />';
echo '</form>';
}
}catch(FacebookApiException $e){
echo $e->getMessage();
}
}else{
$fbloginurl = $fb->getLoginUrl(array('redirect-uri'=>$returnurl, 'scope'=>$permissions));
echo '<a href="'.$fbloginurl.'">Login with Facebook</a>';
}
echo $page_info['access_token']
?>
second/edited code which returns a blank page when viewed from a browser:
<?php
include_once 'inc/facebook.php';
$appId = '0000000000000';
$secret = '000000000000000000000000000';
$returnurl = 'https://mydomain.com';
$permissions = 'manage_pages, publish_actions';
$fb = new Facebook(array('appId'=>$appId, 'secret'=>$secret));
$fbuser = $fb->getUser();
if($fbuser){
if(isset($_POST['msg']) and $_POST['msg']!=''){
try{
$page_info = $facebook->api("/$pageId?fields=access_token");
if (!empty($page_info['access_token'])) {
$message = array(
'access_token' => $page_info['access_token'],
'message' => $_POST['msg']
);
$posturl = '/'.$_POST['pageid'].'/feed';
$result = $fb->api($posturl,'POST',$message);
if($result){
echo 'Successfully posted to Facebook Wall...';
}
}
}catch(FacebookApiException $e){
echo $e->getMessage();
}
}
try{
$qry = 'select page_id, name from page where page_id in (select page_id from page_admin where uid ='.$fbuser.')';
$pages = $fb->api(array('method' => 'fql.query','query' => $qry));
if(empty($pages)){
echo 'The user does not have any pages.';
}else{
echo '<form action="" method="post">';
echo 'Select Page: <select name="pageid">';
foreach($pages as $page){
echo '<option value="'.$page['page_id'].'">'.$page['name'].'</option>';
}
echo '</select>';
echo '<br />Message: <textarea name="msg"></textarea>';
echo '<br /><input type="submit" value="Post to wall" />';
echo '</form>';
}
}catch(FacebookApiException $e){
echo $e->getMessage();
}
}else{
$fbloginurl = $fb->getLoginUrl(array('redirect-uri'=>$returnurl, 'scope'=>$permissions));
echo '<a href="'.$fbloginurl.'">Login with Facebook</a>';
}
?>
EDIT, This will generate an access token but I am not sure if it is a page access token or it is a user access token because the access token is the same for all the pages:
include_once 'inc/facebook.php';
$appId = '000000000000000000';
$secret = '000000000000000000000000000000';
$returnurl = 'https://mydomain.com';
$permissions = 'manage_pages, publish_actions';
$fb = new Facebook(array('appId'=>$appId, 'secret'=>$secret));
$fbuser = $fb->getUser();
if($fbuser){
if(isset($_POST['msg']) and $_POST['msg']!=''){
try{
$message = array(
'message' => $_POST['msg']
);
$posturl = '/'.$_POST['pageid'].'/feed';
//$posturl2 = '/'.$_POST['pageid'].'/tabs';
//$result2 = $fb->api($posturl2,"post", array("access_token" => $pageAccessToken, "app_id" => $appId));
$result = $fb->api($posturl,'POST',$message);
if($result){
echo 'Successfully posted to Facebook Wall...';
}
}catch(FacebookApiException $e){
echo $e->getMessage();
}
}
try{
$qry = 'select page_id, name from page where page_id in (select page_id from page_admin where uid ='.$fbuser.')';
$pages = $fb->api(array('method' => 'fql.query','query' => $qry));
$pageIds=$fb->api('/me/accounts');
$pageAccessToken=$pageIds["data"][1]["access_token"];
if(empty($pages)){
echo 'The user does not have any pages.';
}else{
echo '<form action="" method="post">';
echo 'Select Page: <select name="pageid">';
foreach($pages as $page){
echo '<option value="'.$page['page_id'].'">'.$page['name'].'</option>';
}
echo '</select>';
echo 'Select Page: <select name="pageid">';
foreach($pageIds as $pageId){
echo '<option value="'.$page['page_id'].'">'.$pageAccessToken.'</option>';
}
echo '</select>';
echo '<br />Message: <textarea name="msg"></textarea>';
echo '<br /><input type="submit" value="Post to wall" />';
echo '</form>';
}
}catch(FacebookApiException $e){
echo $e->getMessage();
}
}else{
$fbloginurl = $fb->getLoginUrl(array('redirect-uri'=>$returnurl, 'scope'=>$permissions));
echo '<a href="'.$fbloginurl.'">Login with Facebook</a>';
}
echo $pageAccessToken;
Upvotes: 0
Views: 4353
Reputation: 20753
It's not really possible for me to go through your bunch of code . But what you want is quite simple. Just follow the following steps-
(I'll start after the user successfully grants manage_pages
and publish_stream
permissions)
Get the page access token-
\GET /{page-id}?fields=access_token
Use the page access token to post a feed on the page-
\POST /{page-id}/feed?fields=page_access_token //send any parameters with this
That's it!
Upvotes: 1
Reputation: 319
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. Once this permission has been granted, you can retrieve the page access token using the following Graph API request:
GET /{user-id}/accounts
Read this Page [Page Access Tokens][1]
[1] https://developers.facebook.com/docs/facebook-login/access-tokens
Upvotes: 0