Trying to post a status update using facebook php sdk. Code posted below. As I understand it, I need ask for extended persmission, which I do in the login URL with '&scope=publish_stream', but this still isn't working.
The resultant login URL looks like:
https://www.facebook.com/login.php?api_key=174954539217971&cancel_url=http%3A%2F%2Fexample.com%2facebook.post.php&display=page&fbconnect=1&next=http%3A%2F%2Fexample.com%2Ffacebook.post.php&return_session=1&session_version=3&v=1.0&scope=publish_stream
So you can see it is being set. While this is essentially the same question as: Facebook OAuthException: "user hasn't authorized the application to perform this action"
The suggested solution in that thread is not working here.
<?php
echo "Posting to facebook..<br/>";
require './fb_src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'appId',
'secret' => 'secret',
'cookie' => true,
));
$session = $facebook->getSession();
$me = null;
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
} catch (FacebookApiException $e) {
header('Location: ' . $facebook->getLoginUrl(array('scope' => 'publish_stream')));
error_log($e);
}
}
else {
header('Location: ' . $facebook->getLoginUrl(array('scope' => 'publish_stream')));
}
print_r($me);
try {
$feed = $facebook->api('/me/feed', 'post', array('message' => 'Hello world!', 'cb' => ''));
} catch (FacebookApiException $e) {
print($e);
}
print_r($feed);
?>
parameter is 'req_perms' not 'scope'. So hard to find good documentation...
Works fine now.
Related
I am trying to update Facebook user status using Graph api. My code is
<?php
require 'facebook.php';
$facebook = new Facebook(array(
'appId' =>'389694921095423',
'secret' =>'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
'cookie' => true
));
$access_token = $facebook->getAccessToken();
echo($access_token);
$me = null;
try
{
$uid = $facebook->getUser();
$me = $facebook->api('/me');
echo "Welcome User: " . $me['name'] . "<br />";
//access permission
$permissions_needed = array('publish_stream', 'read_stream', 'offline_access', 'manage_pages');
foreach($permissions_needed as $perm)
{
if( !isset($permissions_list['data'][0][$perm]) || $permissions_list['data'][0][$perm] != 1 )
{
$login_url_params = array(
'scope' => 'publish_stream,read_stream,offline_access,manage_pages',
'fbconnect' => 1,
'display' => "page",
'next' => 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']
);
$login_url = $facebook->getLoginUrl($login_url_params);
header("Location: {$login_url}");
exit();
}
}
//Access permission
$post_id = $facebook->api("/$uid/feed", "post", array("message"=>"Hello World!"));
if(isset($post_id))
{
echo "A new post to your wall has been posted with id: $post_id";
}
}
catch (FacebookApiException $e)
{
echo($e);
}
?>
The problem is that it shows a run time error like Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user. I have generated the Access Tocken. Where i have to use that to remove this exception
Thanks in advance
Try changing the line:
$me = $facebook->api('/me');
to
$me = $facebook->api('/'.$uid);
Also try upgrading your sdk if you are not using the latest version and if you just upgraded the sdk and it caused the trouble, than you can try to downgrade the version and check.
Clear your cache & cookies (if you are testing or developing application)
and
Please do as follow:-
try
{
$fb_user_info = $facebook->api('/me');
} catch (Exception $ex)
{
$facebook->destroySession();
$params = array(
'scope' => 'YOUR_PERMISSIONS',
'redirect_uri' => 'YOUR_SITE_URL'
);
$fb_login_url = $facebook->getLoginUrl($params);
header("Location:" . $fb_login_url);
}
i am using basic facebook php sdk to login users to my page via facebook.
facebook.php, base_facebook.php and:
$facebook = new Facebook(array(
'appId' => '',
'secret' => '',
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if (!$user) {
header("Location: /");
} else {
$loginUrl = $facebook->getLogoutUrl();
and i want to allow my users to post (or skip) something to their wall's when logging in. like that:
http://developers.facebook.com/attachment/web_dialog.png
any ideas?
to post to a user's wall you'd do something like this:
if (isset($_POST['status'])){
try {
$post = stripslashes($_POST['status']);
$statusUpdate = $facebook->api('/<fb_user_id>/feed?access_token='.$session['access_token'], 'post', array('message'=> strip_tags($post), 'cb' => ''));
} catch (FacebookApiException $e) {
echo "<pre>";
echo $e;
echo "</p>";
}
}
You can style the form that posts to that script to match that in the png file.
hope that is what you are looking for and helps you.
This is my code:
<?php
require 'src/facebook.php';
$facebook = new Facebook(array(
'appId' => '25856*****93875',
'secret' => '7c3e95185d681ee6****8bcef1305352',
'cookie' => true
));
$user = $facebook->getUser();
if($user) {
try {
$statusUpdate = $facebook->api('/me/feed', 'post', array('link' => 'http://www.firsatdergisi.com/'));
} catch (FacebookApiException $e) {
echo $e->getMessage();
}
}
else
{
$url = 'https://www.facebook.com/dialog/oauth?client_id=25856*****93875&redirect_uri=http://apps.facebook.com/gunlukburcpaylas/&scope=email,read_stream,publish_stream';
echo "<a href='". $url ."'>login</a>";
}
?>
Error: (#100) The post's links must direct to the application's connect or canvas URL.
what i should?
The error appears to tell you all you need to know - you can only post a link to your application's connect or canvas url, not directly to an OAuth dialog.
I have developed a Facebook application that runs inside an iframe in the Facebook canvas. For it to work properly I request extended permissions from the user. If the user hasn't authorized the application I send him/her to a login page with the getLoginUrl() method in the PHP SDK.
It works, but it's not pretty. The method sends the user to a landing page before the authentication page. It looks like this:
When I click "Go to Facebook.com" I see the actual page for permission requests (I also get right to the permissions page if I print the url, copy it and enter it into a new browser window). How do I make Facebook skip this step when I do the redirect from an Iframe?
My code looks like this (using CodeIgniter and Facebook PHP SDK):
$this->facebook = new Facebook(array(
'appId' => '{MY_APP_ID}',
'secret' => '{MY_SECRET}',
'cookie' => TRUE,
'domain' => $_SERVER['SERVER_NAME']
));
$this->facebook->getSession();
try {
$this->me = $this->facebook->api('/me');
}
catch (FacebookApiException $e) {
$this->me = NULL;
}
if ( is_null($this->me) ) {
redirect($this->facebook->getLoginUrl(array(
'req_perms' => 'offline_access,read_stream,publish_stream,user_photos,user_videos,read_friendlists',
'next' => $this->config->item('base_url').'fblogin.php?redirect_uri='.$this->uri->uri_string()
)));
}
I think you need to redirect the parent frame (i.e. _top) rather than the iFrame itself?
The way I do it is set up an INDEX.PHP file with the following
//if user is logged in and session is valid.
if ($fbme){
//fql query example using legacy method call and passing
parameter
try{
$fql = "select name, hometown_location, sex,
pic_square from user where uid=" .
$uid;
$param = array(
'method' => 'fql.query',
'query' => $fql,
'callback' => 'http://apps.facebook.com/yoursite/'
);
$fqlResult = $facebook->api($param);
}
catch(Exception $o){
d($o);
}
}
Then point your canvas url to http://yoursite.com/INDEX.php
The callback url in the above code which will be in INDEX.PHP sets where to look after permissions are granted.
FBMain.php looks like this
//set application urls here
$fbconfig['http://www.yoursite.com/iframeapp/YOURMAINPAGE.php/']
= "http://www.tyoursite.com/YOURMAINPAGE.php/";
$fbconfig['http://apps.facebook.com/CANVASBASEURL']
= "http://apps.facebook.com/CANVASBASEURL";
$uid = null; //facebook user id
try{
include_once "facebook.php";
}
catch(Exception $o){
echo '<pre>';
print_r($o);
echo '</pre>';
}
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => $fbconfig['APPID'],
'secret' => $fbconfig['SECRET'],
'cookie' => true,
));
//Facebook Authentication part
$session = $facebook->getSession();
$loginUrl = $facebook->getLoginUrl(
array(
'canvas' => 1,
'fbconnect' => 0,
'req_perms'=>'email,publish_stream,status_update,user_birthday,user_location'
)
);
$fbme = null;
if (!$session) {
echo "<script type='text/javascript'>top.location.href
= '$loginUrl';";
exit;
}
else {
try {
$uid = $facebook->getUser();
$fbme = $facebook->api('/me');
} catch (FacebookApiException $e) {
echo "<script type='text/javascript'>top.location.href
= '$loginUrl';";
exit;
}
}
function d($d){
echo '<pre>';
print_r($d);
echo '</pre>';
} ?>
Hope its a little clearer. It took me a while to figure it out, but I got there, thought I would help.
I'd like to post on page - throught my site. . I didn't find anything that could help me in documentation. Also none of google results gave mi answer.
function post_facebook($data=null){
$result = "";
require_once (ROOT. "/apps/configuration/models/ConfigurationItem.php");
require_once (ROOT . "/components/facebook/facebook.php");
$this->ConfigurationItem = new ConfigurationItem($this->getContext());
$row=$this->ConfigurationItem->findByCatKeyItemKey('system','facebook_login');
$apiid=$row['value'];
$row=$this->ConfigurationItem->findByCatKeyItemKey('system','facebook_pass');
$secret=$row['value'];
$facebook = new Facebook(array(
'appId' => $apiid,
'secret' => $secret,
'cookie' => true,
));
$session = $facebook->getSession();
print_r($session);
$me = null;
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
}
$message=$data['facebook_text'];
$attachment = array(
'message' => $data['facebook_text'],
'name' => $data['name'],
'link' => $this->getLinkToLatestNews(),
'description' => '',
);
if($data['thumb_file_tree_id'] !== NULL) $attachment = $_SERVER['HTTP_HOST']."media/file/image_by_id/".$data['thumb_file_tree_id']."/?w=400&h=500";
try {
$facebook->api('/162618213751448/feed/', 'post', $attachment);
$result = "Facebook: Sent";
} catch (FacebookApiException $e) {
$result = "Facebook: Failed";
error_log($e);
}
} else {
$login_url = $facebook->getLoginUrl();
header("Location: ".$login_url);
exit;
}
return $result;
}
The wrong part is:
$session = $facebook->getSession();
$me = null;
if ($session) {
(...)
} else {
$login_url = $facebook->getLoginUrl();
header("Location: ".$login_url);
exit;
}
I want to allow user to login on specified FB account (this with page), and then send. App settings allow only this account to post, so it should be ok... But isn't. When I log out FB account, session still exists, but returns exception. What's wrong?
if you want to post on a fan page where you are adminstrator.. do the next
Your application needs to have this permission minimum:
$this->loginUrl = $this->facebook->getLoginUrl(
array(
'scope' => 'manage_pages,offline_access,publish_stream',
'redirect_uri' => $url,
)
);
After you are logged into:
//get pages or applications where you are administrator
$accounts = $this->facebook->api('/me/accounts');
//page where i want to post
$page_id = '227870047252297';
foreach($accounts['data'] as $account)
{
if($account['id'] == $page_id)
{
$token = $account['access_token'];
}
}
$attachment = array(
'access_token' => $token,
'message' => 'mensaje',
'name' => 'titulo',
'link' => 'http://...',
'description' => 'xxxxx',
'picture'=> 'http://...',
);
try{
$res = $this->facebook->api('/'.$page_id.'/feed','POST',$attachment);
} catch (Exception $e){
echo $e->getMessage();
}
If I understand this correctly, you want to send a message to the user's wall who's connected to your application via your website the moment they log in? If so, try this. Rework the script to prevent constant posting, but this model, in theory, should work.
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
// Place messaging portion here instead.
$message = $facebook->api('/me/feed', 'post', array(
'message'=> '<Message>',
'picture' => '<Picture URL>',
'link'=> '<URL>',
'description'=>'Description',
'name'=> '<Name of Post>',
'privacy'=> '<ALL_FRIENDS (Default)>',
'caption'=> '<Caption>',
)
);
} catch (FacebookApiException $e) {
error_log($e);
}
So if the user is connected with your application and the session is valid, it will automatically send the post to their news feed (Wall).