php - Cannot obtain page access token through an app that is granted the manage_pages permission -
i'm trying develop php application let me make new posts on pages automatically, through cronjob, while offline , not logged facebook @ all. i'm aware offline_access permission has long been gone, removal of offline_access permission article states following:
scenario 5: page access tokens
when user grants app manage_pages permission, app able obtain page access tokens pages user administers querying [user id]/accounts graph api endpoint. migration enabled, when using short-lived user access token query endpoint, page access tokens obtained short-lived well.
exchange short-lived user access token long-lived access token using endpoint , steps explained earlier. using long-lived user access token, querying [user id]/accounts endpoint provide page access tokens not expire pages user manages. apply when querying non-expiring user access token obtained through deprecated offline_access permission.
... made me think automated php solution had in mind indeed possible engineer. went ahead , created new application , granted manage_pages permission, publish_actions permission.
however code:
<?php require_once("sdk/facebook.php"); $config = array(); $config['appid'] = my_app_id; $config['secret'] = my_app_secret; $config['fileupload'] = false; // optional $facebook = new facebook($config); $accountid = my_account_id; $pages = $facebook->api("/$accountid/accounts/"); var_dump($pages); ?>
...returns following error:
fatal error: uncaught oauthexception: user access token required request resource.
what doing wrong?
even more confusing - @ same time, piece of code here, posts status update wall without problem (and i'm still not logged facebook or anything)
<?php require_once("sdk/facebook.php"); $config = array(); $config['appid'] = my_app_id; $config['secret'] = my_app_secret; $config['fileupload'] = false; // optional $token_url = "https://graph.facebook.com/oauth/access_token?" . "client_id=" . $config['appid'] . "&client_secret=" . $config['secret'] . "&grant_type=client_credentials"; $app_token = str_replace('access_token=','',file_get_contents($token_url)); $facebook = new facebook($config); $args = array( 'access_token' => $app_token, 'message' => 'test post via app access token' ); $accountid = my_account_id; $post_id = $facebook->api("/$accountid/feed", "post", $args); ?>
...and if do
$post_id = $facebook->api("/$mypageid/feed", "post", $args);
...instead, get:
fatal error: uncaught oauthexception: (#200) user hasn't authorized application perform action
the solution
with kind of mr. mohammed alaghbari, able make reasonable progress on issue , ended coming solution.
as suggested, needed generate user access token first, issuing request this:
https://graph.facebook.com/me/accounts?access_token=user_access_token
in order retrieve actual page access token , post page. realized log in through login link generated php sdk, user_access_token stored me inside $_session variable. took token, issued request https://graph.facebook.com/me/accounts?access_token=user_access_token
, retrieved page_access_token , used in order post test message page.
however, logged out , issued session_destroy();
surprise, minute did that, code started throwing a user access token required request resource.
exception again. frustrated.
then, decided manually request https://graph.facebook.com/my_account_id/accounts/?my_user_access_token
, see happens. bigger surprise - graph api accepted user_access_token , returned valid page tokens. frustrated why getting exception in sdk, no errors when issue request directly graph api, decided start debugging base_facebook.php. php sdk i'm using version 3.2.2.
after hours of searching, turned out code located @ line 899 of base_facebook.php throws exception without reason (sort of)
if (!isset($params['access_token'])) { $params['access_token'] = $this->getaccesstoken(); }
when commented line $params['access_token'] = $this->getaccesstoken();
out, php sdk requests started returning valid tokens again.
weird. i'm wondering if bug in php sdk, or problem code. final code looks this:
<?php require_once("sdk/facebook.php"); $config = array(); $config['appid'] = my_app_id; $config['secret'] = my_app_secret; $config['fileupload'] = false; // optional $facebook = new facebook($config); $token = my_user_token; // extracted $_session $response = $facebook->api("/my_account_id/accounts/?access_token=$token"); // code extracts page token $response goes here $args = array( 'access_token' => my_page_token, 'message' => 'test post via app access token' ); $post_id = $facebook->api("/my_page_id/feed", "post", $args); // works!! ?>
edit #2
after further testing, turned out instead of commenting out line 899, revise
$facebook->api("/my_account_id/accounts/?access_token=my_access_token');
to
$facebook->api("/my_account_id/accounts/", 'get', array('access_token' => 'my_access_token'));
maringtr,
to interact page need use page access token (check access token types: access token types)
types are: user access token, page access token, app access token
which can generated graph this:
https://graph.facebook.com/me/accounts?access_token=user_access_token
the above graph give list of pages manage there details (page name, id, access token)
you can test page access token fetch page feeds graph (replace page_id
page id , page_access_token
page access token generated above graph )
https://graph.facebook.com/page_id/feed?access_token=page_access_token&message=testing
for more information , examples : log in page
regards,
Comments
Post a Comment