Facebook GraphApi -> Error : UOAuthException: (#282) Requires extended permission: share_item thrown - php

i get an error while trying to automatically post a link with Facebook Graph API
My php code
<?
$config = array();
$config['appId'] = '3545XXXXXXX6207';
$config['secret'] = '60068XXXXXXXXXXXe45a426';
$facebook = new Facebook($config);
$retour_login = json_decode(stripslashes($_GET['session']), true);
$uid = $retour_login['uid'];
$access_token = $retour_login['access_token'];
$me = null;
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
}
}
if(!isset($_GET['session'])) { // Asking permissions
$params = array(
'locale' => 'fr_FR',
'display' => 'popup',
'req_perms' => 'email,user_about_me,user_birthday,user_location',
'ext_perms' => 'publish_stream,share_item,manage_pages'
);
header("Location: ".$facebook->getLoginUrl($params));
}
$args = array(
'message' => "XXXX",
'link' => 'XXXX',
'name' => 'XXXX',
'picture' => 'XXXX',
"caption"=> "lien.com",
"type"=> "link");
$post_id = $facebook->api("/me/links", "post", $args); // execute this to automatically post on FB
?>
I get this error : OAuthException: (#282) Requires extended permission: share_item thrown
Any ideas ?
Thanks!

What are 'req_perms' and 'ext_perms' there? the permissions should be in a 'scope' parameter according to the documentation - https://developers.facebook.com/docs/reference/dialogs/oauth/ and https://developers.facebook.com/docs/authentication/
Check you're not working from an outdated example or tutorial - always check the official documentation first

Related

Post to facebook fan page as a user

Can anyone tell me how to do this using php sdk.
I have using this code. This code was suppose to post to user's timeline as well as the fan page. The person who is logged in would be the one who is doing the post. So on the fan page
this would have appeared under recent activity. However, now the code has stopped working saying that the user has not authorised the app. I do not understand why this could be happening.
include("../php-sdk/facebook.php");
/*START FACEBOOK lOGIN*/
$facebook = new Facebook(array(
'appId' => Appid,
'secret' => Appsecret,
'cookie' => true
));
$pageId = PageId;
$user= $facebook->getUser();
$newfbuser = 0;
if (!empty($user)) {
$uid = $facebook->getUser();
// $fb_access_token = $facebook->getAccessToken();
$url = $facebook->getLoginUrl(array(
'req_perms' => 'email,status_update,publish_stream, manage_pages'
));
$user = $facebook->api('/me');
$fb_access_token = $facebook->getAccessToken();
$param = array(
'method' => 'users.getInfo',
'uids' => $uid,
'fields' => array('name','sex')
);
$users_getinfo = $facebook->api($param);
$save['oauth_uid'] = $users_getinfo['0']['uid'];
$save['oauth_provider'] = 'facebook';
$save['facebook_email'] = $users_getinfo['0']['email'];
$save['name'] = $users_getinfo['0']['name'];
$link_url = "http://google.com";
$pic = 'http://xxxxx/images/Testing.jpg';
$attachment = array(
'access_token' =>$fb_access_token,
'message' => 'This is a message by bob',
'link' => 'http://xxxxx/');
// print_r($fb_access_token);
echo 'on my timeline<br />';
$facebook->api("/me/feed",'post',$attachment);
$facebook->api("/$pageId/feed",'post',$attachment);
echo 'successfully posted';
}
// $fb_access_token = $facebook->getAccessToken();
$url = $facebook->getLoginUrl(array(
'req_perms' => 'email,status_update,publish_stream, manage'
));
No such thing call "manage", the correct permission was manage_pages

no login page php send message to facebook friends

I want to use PHP to send message to my Facebook friends. I found some code but it just lets me log in. Could anyone help me?
<?php
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => 'XXXXX',
'secret' => 'XXXXX',
'cookie' => true,
));
$session=$facebook->getUser();;
if ($session) {
$logoutUrl = $facebook->getLogoutUrl();
$access_token = $facebook->getAccessToken();
try {
$result = $facebook->api(
'me/feed',
'post',
array('access_token' => $access_token , 'message' => 'test')
);
} catch (FacebookApiException $e) {
error_log($e);
}
}
1) Did you provided the correct permissions to the app?
$login_url = $facebook->getLoginUrl( array(
'scope' => 'publish_stream'
));
2) Check this answer: Update facebook status using php and this: Facebook PHP SDK , Post to another users wall whats wrong?
3) it's /me/feed, not me/feed:
$result = $facebook->api(
'/me/feed',
'post',
array('access_token' => $access_token , 'message' => 'test')
);

Facebook session not being created

I am using this code to post to the user's wall :
require 'fb/src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'xxx',
));
$params = array(
'canvas' => 1,
'scope' => 'publish_stream,email,user_about_me,user_birthday,user_website',
'redirect_uri' => 'urlhere',
);
$fb_session = $facebook->getUser();
// Session based API call.
if ($fb_session)
{
try
{
$args = array(
'message' => $_GET['by'],
'link' => 'linkhere/',
'caption' => $_GET['test']
);
$post_id = $facebook->api("/me/feed", "post", $args);
}
catch (Exception $e)
{
$fb_login_url = $facebook->getLoginUrl($params);
echo $e->getMessage();
}
}
else
{
$fb_login_url = $facebook->getLoginUrl($params);
}
The code is working till the point uuer clicks on login part to post. After that the url contains a code = xxxzz and a state =yyy but the $fb_session is 0.
This works sometimes without any changes. Please help!
You should include the access token when you try to post to some User Facebook wall.
First get access token:
$fbToken = NULL;
$fbToken = $facebook->getAccessToken();
right after $fb_session = $facebook->getUser();
and then include it in the $args array:
'access_token' => $fbToken
if session is NOT created or access token is not valid, you should redirect the user back to re-authorize your Facebook App.
So in ELSE, you should add
header("Location: $fb_login_url ");
after $fb_login_url = $facebook->getLoginUrl($params);
assuming that everything else is correct, but the access_token is not valid, you should also include header("Location: $fb_login_url "); before echo $e->getMessage();
in both cases you are using getloginurl
shouldn't it be
if ($fb_session) {
try {
$args = array(
'message' => $_GET['by'],
'link' => 'linkhere/',
'caption' => $_GET['test']
);
$post_id = $facebook->api("/me/feed", "post", $args);
} catch (Exception $e) {
$fb_logout_url = $facebook->getLogoutUrl;
.
.
.

facebook PHP error 102, Session key invalid or no longer valid

I'm trying to make a simple login by using PHP and Facebook. THe problem is that I've this result when It's supposed to be shown the "iframe","popup" or "page" (none of them are working)
API Error Code: 102
API Error Description: Session key invalid or no longer valid
Error Message: Iframe dialogs must be called with a session key
The code I'm using for now is this:
$config = array(
'appId' => 'CODE1',
'secret' => 'CODE2',
'cookie' => true
);
$params = array(
'redirect_uri' => 'http://domain.dev',
'display'=>'iframe'
);
$facebook = new Facebook($config);
$user = $facebook->getUser();
if ($user) {
//something
} else {
$user = null;
$loginUrl = $facebook->getLoginUrl($params);
//redirect to loginURL
}
Try this :
if (!(isset($_REQUEST['state'])) {
$loginUrl = $facebook->getLoginUrl(array(
'redirect_uri' => 'http://domain.dev',
'display' => 'popup',
'scope' => 'user_photos'
));
echo $loginUrl;
For more reference you can use this :
http://www.9lessons.info/2011/02/login-with-facebook-and-twitter.html

Publish with Facebook PHP api => UOAuthException: (#282) Requires extended permission: share_item thrown

Here is My code :
<?
session_start();
require_once($_SERVER['DOCUMENT_ROOT'].'/admin/config.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/admin/fonctions.php');
include '../api/facebook.php';
include '../api/xhttp.php';
$config = array();
$config['appId'] = '35455XXXXXXX207';
$config['secret'] = '6006855f3aXXXXXXXXX9bce45a426';
$config['fileUpload'] = false; // optional
$facebook = new Facebook($config);
$args = array(
'message' => 'Hello from my App!',
'link' => 'http://www.masteringapi.com/',
'caption' => 'Visit MasteringAPI.com For Facebook API Tutorials!',
'access_token' => $access_token
);
$retour_login = json_decode(stripslashes($_GET['session']), true);
$uid = $retour_login['uid'];
$access_token = $retour_login['access_token'];
$post_id = $facebook->api("/me/feed", "post", $args);
?>
But it returns : Uncaught OAuthException: An active access token must be used to query information about the current user.
Any ideas ?
Thanks!
Here is My new code :
<?
$config = array();
$config['appId'] = '3545XXXXXXX6207';
$config['secret'] = '60068XXXXXXXXXXXe45a426';
$facebook = new Facebook($config);
$retour_login = json_decode(stripslashes($_GET['session']), true);
$uid = $retour_login['uid'];
$access_token = $retour_login['access_token'];
$me = null;
// Session based API call.
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
}
}
if(!isset($_GET['session'])) {
$params = array(
'locale' => 'fr_FR',
'display' => 'popup',
'req_perms' => 'email,user_about_me,user_birthday,user_location',
'ext_perms' => 'publish_stream,share_item,manage_pages'
);
header("Location: ".$facebook->getLoginUrl($params));
}
$args = array(
'message' => "XXXX",
'link' => 'XXXX',
'name' => 'XXXX',
'picture' => 'XXXX',
"caption"=> "lien.com",
"type"=> "link");
$post_id = $facebook->api("/me/links", "post", $args);
?>
This time i get this error : OAuthException: (#282) Requires extended permission: share_item thrown
Any ideas ?
Thanks!
You're not passing it the access token.
Check out getAccessToken().
From your code sample it looks like you are trying to use the variable $access_token to create your $args array, before you define the $access_token variable. I would switch the order of variable definitions:
$retour_login = json_decode(stripslashes($_GET['session']), true);
$uid = $retour_login['uid'];
$access_token = $retour_login['access_token'];
$args = array(
'message' => 'Hello from my App!',
'link' => 'http://www.masteringapi.com/',
'caption' => 'Visit MasteringAPI.com For Facebook API Tutorials!',
'access_token' => $access_token
);
This is assuming $retour_login['access_token'] contains a valid Facebook access token. Otherwise you will need to fetch the current user's access token using the Facebook SDK:
$access_token = $facebook->getAccessToken();

Categories