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.
Related
I followed these instructions and some others to create an app and get tokens: facebook: permanent Page Access Token?
I used the following php code to post using the app:
<?php
require_once 'src/Facebook/autoload.php'; $fb = new \Facebook\Facebook([ 'app_id' => 'apkey', 'app_secret' => 'appsecret', 'default_graph_version' => 'v2.10', //'default_access_token' => '{access-token}', // optional ]);
$linkData = [
"message" => "Wonderful message.",
]; $pageAccessToken ='pageaccesstoken';
try { $response = $fb->post('/targetfeed/feed', $linkData, $pageAccessToken); } 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();
?>
I replace the keys and specific data with description.
My issue is that when I run this script it posts to the feed, but it posts as the app owner. I created the app in my personal account, and went through the token process using that account. I was granted admin access to the account that I want to post to, but when I post to that account it is posting as my person page and I instead want it to post as if I am posting as the account that I am posting to.
I am sure it some access token setting or permissions things, but I cannot figure it out. Any suggestions?
I am trying to pull back an array of customer reviews (ratings?) from a particular facebook page.
I understand I need the manage_pages permission to do this, however according to the facebook docs I need to submit my app for review, providing the steps needed to reproduce the use of this permission, along with a screencast of the permission in use.
The problem I'm having is that I cannot use the permission to provide the steps and screencast because any attempt to do so results in the API telling me that it needs that permission. So a bit of a paradox really.
I assume there must be some kind of way to reproduce the steps needed in a test environment, but setting up a test app and using the app_id and app_secret from the test app still resulted in the following error:
Fatal error: Uncaught exception 'Facebook\Exceptions\FacebookAuthorizationException' with message '(#200) Requires manage_pages permission to manage the object'
My code is as follows:
<?php
$fb = new Facebook\Facebook([
'app_id' => $test_app_id,
'app_secret' => $test_app_secret,
'default_graph_version' => 'v2.8',
]);
if (isset($_GET['code'])) {
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
if (isset($accessToken)) {
// Logged in!
$_SESSION['facebook_access_token'] = (string) $accessToken;
}
}
if (!isset($_SESSION['facebook_access_token'])) {
$helper = $fb->getRedirectLoginHelper();
$permissions = ['manage_pages'];
$loginUrl = $helper->getLoginUrl($site_url.'/fb-test.php', $permissions);
echo 'Log in with Facebook!';
exit;
}
$access_token = $_SESSION['facebook_access_token'];
$response = $fb->get('/'.$page_id.'?fields=access_token', $access_token);
$page_access_token = $response->getDecodedBody()['access_token'];
$ratings = $fb->get('/'.$page_id.'/ratings', $page_access_token);
var_dump($ratings);
Edit (following comment from luschn)
I have altered my code to include the following, but I still get told that manage_pages permission is required.
<?php
$response = $fb->sendRequest(
'GET',
'/'.$page_id.'?fields=access_token',
array (
'user' => $user_id,
'role' => 'administrators',
'state' => rand(0,9999999)
),
$access_token
);
$page_access_token = $response->getDecodedBody()['access_token'];
$response = $fb->sendRequest(
'GET',
'/'.$page_id.'/ratings',
array (
'user' => $user_id,
'role' => 'administrators',
),
$page_access_token
);
echo '<pre>'.print_r($response,true).'</pre>';
exit;
I use Laravel socialite package and I take
permission('manage_pages',
'publish_pages',
'publish_actions',
'ads_management',
'business_management', 'ads_read'),
After than get access token in callback.
When I try post as page with access token but post seems like visitor post.
What am I supposed to do? Here is my code:
$fb = new Facebook([
'app_id' => 'appid',
'app_secret' => 'appsecret',
'default_graph_version' => 'v2.7',
]);
$params=[
'message'=>'Example Post',
'access_token'=>'ACCESS_TOKEN',
];
try{
$ret=$fb->post('/PAGEID/feed',$params);
} catch(FacebookResponseException $e) {
echo 'Graph returned an error: '.$e->getMessage();
exit;
} catch(FacebookSDKException $e) {
echo 'Facebook SDK returned an error: '.$e->getMessage();
exit;
}
$graphNode = $ret->getGraphNode();
print_r($graphNode);
You need to use a Page Token with the publish_pages permission. If it gets posted as visitor, you are using a User Token.
More information about Tokens and how to generate them:
https://developers.facebook.com/docs/facebook-login/access-tokens
http://www.devils-heaven.com/facebook-access-tokens/
I have tried to following way
but i got Error "The access token does not belong to application "
i have checked lot of time in my app id and secrt id, app id is correct but This error shown again and again I didn't peridict this Error? and also i have tried following way also
https://graph.facebook.com/v2.2/oauth/access_token?grant_type=fb_exchange_token&client_id=CLIENT_ID
&client_secret=SECRED CODE&fb_exchange_token=EAACEdEose0cBAJRZCZBIaDmW3oOO6SHaOkQLKdgyjp1evGoQ19mYcZCXu5wWLwZABJUbV77tjPjiE2pac2fDEmjM1tZAZB8hflSyERXFWIZB2DtzZAGSgVX6Ukb0ZAZAzd6pohnZBXU0T2aqYwf1umUxsfgHQXBNmM15yhdZBG2Br
PHPsdk 5 and v2.8
$fb = new Facebook\Facebook([
'app_id' => 'xxxxxxxxx',
'app_secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxx',
'default_graph_version' => 'v2.8',
]);
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// There was an error communicating with Graph
echo $e->getMessage();
exit;
}
$client = $fb->getOAuth2Client();
try {
// Returns a long-lived access token
$accessTokenLong = $client->getLongLivedAccessToken($accessToken);
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// There was an error communicating with Graph
echo $e->getMessage();
exit;
}
if (isset($accessTokenLong)) {
// Logged in.
$_SESSION['facebook_access_token'] = (string) $accessTokenLong;
}
Reference url :https://www.sammyk.me/upgrading-the-facebook-php-sdk-from-v4-to-v5
I'm trying to display facebook status updates for a sports team on their website using the facebook graph api, but can't seem to get a valid access token.
Here's my code.
require_once APPPATH.'/third_party/facebook-php-sdk-v4/src/Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => '123123123123',
'app_secret' => 'fghdfghtyjdfghdghjfghjfghj',
'default_graph_version' => 'v2.5'
]);
$fb->setDefaultAccessToken('123123123123|dfhjfgytdfghdfhgdsfjd');
$helper = $fb->getCanvasHelper();
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
if (isset($accessToken)) {
// Logged in.
} else {
echo 'access token not set - THIS IS WHERE I ALWAYS SEEM TO END UP';
}
exit;
Is there anything obvious I'm doing wrong?
This is what I used in a Laravel 4 app to connect. Basically when the user clicks on a 'Log in' button,
1- Create the Facebook instance with app_id, app_secret and default_graph_version.
2- Get a redirectLoginHelper object to get a loginUrl with a code that facebook will provide when you call $helper->getLoginUrl($redirectUrl, (array) $permissionsNeeded)
3- Redirect to that url
4- Create facebook instance and getRedirectLoginHelper again as step 1 and 2
5- Check if you received the 'code' parameter in the url the helped returned
6- Get the access token: $accessToken = $helper->getAccessToken()
In the code I'm providing, I do all this steps in one method. I check if there's a code parameter in the request, if not I request it and redirect. The next time the code runs, when I check if code is there, it continues to the next step
$fb = new Facebook([
'app_id' => 'facebook_app_id',
'app_secret' => 'facebook_app_secret',
'default_graph_version' => 'v2.5',
]);
$helper = $fb->getRedirectLoginHelper();
if(Input::get('code') == '') {
$permissions = ['manage_pages, publish_actions'];
$loginUrl = $helper->getLoginUrl('http://yoururl.dev/uri', $permissions);
return Redirect::to($loginUrl);
}
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookSDKException $e) {
//Handle error
}
In this case, the Log in button goes to www.domain.app/connect, the URL that the redirectLoginHelper returns is the same, but with the code parameter: www.domain.app/connect?code=ntasd341251