Trouble programatically posting links to a Facebook Page (PHP SDK) - php

I'm having real issues posting links to a Facebook Page via the Facebook SDK for PHP (v5.4). I'm using v2.6 of the Facebook Graph API.
I'm using a user who has admin access to the page.
I've got an access token that never expires, with the following permissions: user_managed_groups, user_photos, user_posts, email, manage_pages, publish_pages, pages_show_list, publish_actions, public_profile.
This is my code:
use \Facebook\Facebook;
use \Facebook\Exceptions\FacebookResponseException;
use \Facebook\Exceptions\FacebookSDKException;
$app_id = '123';
$app_secret = 'ABC';
$access_token = 'XXX';
$page_id = '123';
$fb = new Facebook\Facebook([
'app_id' => $app_id,
'app_secret' => $app_secret,
'default_graph_version' => 'v2.6',
'access_token' => $access_token,
]);
$helper = $fb->getRedirectLoginHelper();
try {
$response = $fb->get('/' . $page_id . '?fields=access_token', $access_token);
// This returns the same access token as I've already got,
// so I don't know if that's a problem,
// or if there's just no point doing this.
$page_access_token = $response->getAccessToken();
$response = $fb->post('/' . $page_id . '/feed', [
'message' => 'Test message with link',
'link' => 'http://example.com',
], $page_access_token);
} catch (FacebookResponseException $exception) {
echo '<p>Graph returned an error: ' . $exception->getMessage() . '</p>';
exit;
} catch (FacebookSDKException $exception) {
echo '<p>Facebook SDK returned an error: ' . $exception->getMessage() . '</p>';
exit;
}
At the moment, I'm getting the following exception:
Graph returned an error: (#200) Permissions error
Any help with this would be really appreciated. It feels like I'm so close.

Related

Get user email from Facebook Web SDK API v5

I need your help.
I am using Kohana 3.0.4 framework for my web app and i want to use facebook login so i am using the Facebook Web SDK v5. I get every information from the the user profile except the user email. In my login action this what i did:
$fb = new Facebook\Facebook([
'app_id' => 'xxxxxx',
'app_secret' => 'xxxxxxx',
'default_graph_version' => 'v2.8',
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email', 'user_birthday', 'public_profile']; // optional
$callback = 'http://www.example.com';
$loginUrl = $helper->getLoginUrl($callback, $permissions); echo "<a href='.$loginUrl.'>Login With Facebook</a>";
then in the redirected action:
# login-callback.php
$fb = new Facebook\Facebook([
'app_id' => '1200538793346947',
'app_secret' => '3cf44820ffae5f16c8122174701a558f',
'default_graph_version' => 'v2.8',
]);
// Use one of the helper classes to get a Facebook\Authentication\AccessToken entity.
$helper = $fb->getRedirectLoginHelper();
// $helper = $fb->getJavaScriptHelper();
// $helper = $fb->getCanvasHelper();
// $helper = $fb->getPageTabHelper();
try {
$accessToken = $helper->getAccessToken();
// Get the \Facebook\GraphNodes\GraphUser object for the current user.
// If you provided a 'default_access_token', the '{access-token}' is optional.
$response = $fb->get('/me?fields=email,name,birthday,gender', $accessToken);
} 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;
}
$me = $response->getGraphUser();
echo 'Logged in as ' . $me->getName(). ' ' . $me->getId() . ' '. $me->getGender() . ' '. $me->getEmail() . '<br />';
print_r($me->getBirthday());
Please i need help because i want to also get the user email. Thanks in advance.

Post Image on Facebook Page Feed as Page

Using this example from fb-sdk:
require_once 'fb-sdk/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => '123456',
'app_secret' => 'abc1233',
'default_graph_version' => 'v2.6',
]);
$data = [
'message' => 'Testupload',
'source' => $fb->fileToUpload('image.jpg'),
];
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->post('/PageID/photos', $data, 'XXXXXXYYYYYY');
} 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();
echo 'Photo ID: ' . $graphNode['id'];
i am able to post as myself to the wall of my facebook-page.
'app_id' and 'app_secret' come from the app i created for this.
If i set $response = $fb->post('/me/photos', $data, 'XXXXXXYYYYYY'); the post shows up on my timeline.
What do i need to change, so get a photo, posted by "Page" on the feed of "Page"?
You need to use a Page Token with the publish_pages permission to post "as Page". You can get a Page Token with the following API call, after authorizing with manage_pages and publish_pages:
/page-id?fields=access_token
More information:
https://developers.facebook.com/docs/facebook-login/access-tokens
http://www.devils-heaven.com/facebook-access-tokens/

Get facebook page review and rating

I have created a facebook app and approved the app to access manage_pages.I am looking for php code to get page access from page to get the page information.
For getting reviews and rating I am using the below code
require 'facebook-php-sdk-master/src/facebook.php';
$config = array();
$config['appId'] = '1489047331XXXXX';
$config['secret'] = '6ac210360aad27ab1044e4201XXXX';
$facebook = new Facebook($config);
print_r($facebook);
try {
// 466400200079875 is Facebook id of Fan page https://www.facebook.com/pontikis.net
$ret = $facebook->api("/page_id/ratings?field=open_graph_story", 'GET');
print_r($ret);
} catch(Exception $e) {
echo $e->getMessage();
}
I am getting the below error
(#210) This call requires a Page access token.
Any help will be highly appreciated.
Create new object like this and set access_token if not exists:
$fb = new Facebook([
'app_id' => FB_APP_ID,
'app_secret' => FB_APP_SECRET,
'default_graph_version' => 'v2.5',
'default_access_token' => isset($_SESSION['facebook_access_token']) ?
$_SESSION['facebook_access_token'] : FB_APP_ID . '|'. FB_APP_SECRET
]);
Change FB_APP_ID and FB_APP_SECRET, only with yours. Now you have access token, after that you can make requests and get data that u need access token for it like this (for the example):
$request = $fb->request('GET', '/'.$page_id.'/');
// Send the request to Graph
try {
$response = $fb->getClient()->sendRequest($request);
} 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;
}
$graphNode = $response->getGraphPage();
var_dump($graphNode->all());
Where $page_id is id of some page, where you can find it with its FB page URL .

Facebook API Giving Server error 500

I'm having a lot of trouble getting the Facebook API to work at all. Any example code I use gives me Server Error 500. I'm trying to post a link to my facebook page (this code is from the example code Facebook gives in its documentation). To clarify as well, I do have an app-id, secret-id, and I believe I got the right access token from the Graph API. I know I need a public_action permission to post a link, but the way I read it, I think the access token defaults with public_action.
<?php
$appid = '{app-id}';
$appsecret = '{app-secret}';
$redirect_uri = 'http://mywebsite.com';
// Define the root directoy
define( 'ROOT', dirname( __FILE__ ) . '/' );
// Autoload the required files
require_once( ROOT . 'facebook-php-sdk-v4-4.0-dev/autoload.php' );
$fb = new Facebook\Facebook([
'app_id' => '{app-id}',
'app_secret' => '{app-secret}',
'default_graph_version' => 'v2.2',
]);
$linkData = [
'link' => 'http://www.example.com',
'message' => 'User provided message',
];
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->post('/me/feed', $linkData, '{access-token}');
} 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();
echo 'Posted with id: ' . $graphNode['id'];
?>
I really do not know what I am doing wrong. Likewise, where it says "me" should that be my facebook user id? Thank you.

Tag persion in page post/photo with Facebook SDK

I can programmatically post on behalf of a page. I want to be able to post to a person, or tag them in the post, or tag them in a comment. I just need anyway to create a post as a page, and have it tied to a user.
Edit: I just really want to be able to make a page post, and tag a user, or tag them in a comment, or tag them in a photo. Any form of tagging where they'd be notified.
I've tried tagging people in a photo. The tags appear as pending, there's no notification, and when I approve the tag, it vanishes permanently. So probaly not possible.
Here's my code so far:
<?php
$pageId = 'removed';
// Pass session data over.
// Include the required dependencies.
require( __DIR__.'/facebook/autoload.php' );
session_start();
//$_SESSION["accessToken"] = null;
// Initialize the Facebook PHP SDK v5.
$fb = new Facebook\Facebook([
'app_id' => 'removed',
'app_secret' => 'removed',
'default_graph_version' => 'v2.5',
]);
if(isset($_SESSION["accessToken"])){ //if authorized
$photo = [
'message' => 'Test message.',
'source' => $fb->fileToUpload('glow.jpg'),
'tags' => array(
array(
'tag_uid'=> removed,
'tag_text' => 'name',
'x' => '50',
'y' => '50',
)
)
];
//auth page
$response = $fb->get('/' . $pageId . '?fields=access_token', $_SESSION["accessToken"]);
$pageNode = $response->getGraphNode();
$pageToken = $pageNode['access_token'];
$response = $fb->post('/' . $pageId . '/photos', $photo, $pageToken);
$postNode = $response->getGraphNode();
$postId = $postNode['id'];
echo 'Page Token: ' . $pageToken . '<br />Post Id:' . $postId;
$response = $fb->post('/' . $postId . '/comments', array (
'message' => 'This is a test comment. #[{19292868552}:1:{Martin Firth}] tag thing.',
), $_SESSION["accessToken"]);
} else { //not authorized yet
$helper = $fb->getRedirectLoginHelper();
$accessToken = $helper->getAccessToken();
$_SESSION["accessToken"] = $accessToken;
// Send the request to Graph
try {
$response = $fb->get('/' . $pageId . '?fields=access_token', $accessToken);
} 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();
$permissions = ['email', 'user_likes', 'publish_pages', 'publish_actions'];
$loginUrl = $helper->getLoginUrl('http://localhost/terranova/site/', $permissions);
echo ' Log in with Facebook!';
exit;
}
}
?>

Categories