post message on facebook wall not working - php

I want to post a message on facebook wall from our site. First I am getting error as
Uncaught OAuthException: (#200), when trying to post on wall
Now I am not getting the error but the code is not working.
$facebook = new Facebook(array(
'appId' => APP_ID,
'secret' => APP_SECRET,
'cookie' => true,
'req_perms' => 'email,read_stream,read_friendlists,publish_stream,offline_access,manage_pages',
));
$user = $facebook->getUser();
$token = $facebook->getAccessToken();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if (!empty($user_profile )) {
$username = $user_profile['name'];
$uid = $user_profile['id'];
try {
$post=$facebook->api("/".$uid."/feed", "post", array(
'access_token' => $token,
'message' => 'test',
));
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
But no message is coming what to do? Is there any problem while creating the application?

Here's my wild guess, since our Comment Q&A wasn't going to work very well.
If you authorize your app with some set of permissions, and subsequently change the permissions that your app requests (at the app admin page), your existing authorization does not update to include the new permissions. Your app will still function, but the actions that require the new permissions will fail.
You can test if this is causing your error by simply removing the app from your personal account app settings page, and re-approving the app with the new permissions. If your problem vanishes, then congratulations. If not, you'll need to do more detective work. Your code appears to be fine.

Related

How to fix "(#200) The user hasn't authorized the application to perform this action" error while posting on facebook wal

I am getting "(#200) The user hasn't authorized the application to perform this action" error when posting message on user's facebook wall. I am using facebook graph API. I have also set the "publish_actions" permission in the scope, but didn't working. My code is
require 'facebook/facebook.php';
$facebook = new Facebook(array(
'appId' => APP_ID,
'secret' => APP_SECRET,
));
$user = $facebook->getUser();
if ($user) {
$accessToken = $facebook->getAccessToken();
try {
//create message with token gained before
$post = array(
'access_token' => $accessToken,
'message' => 'Test message'
);
$res = $facebook->api('/me/feed', 'POST', $post);
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
} else {
# There's no active session, let's generate one
$login_url = $facebook->getLoginUrl(array(
'scope' => 'publish_actions'
));
header("Location: " . $login_url);
}
Can you please suggest me what i am missing in the code or in the app?
Since you mentioned that it does work for Admins, but not for anyone else, it´s very clear what the problem is: You need to go through Login Review with publish_actions - else, that permission will only work for users with a role in the App.
All the information you need about Login Review is in the docs: https://developers.facebook.com/docs/facebook-login/review

Facebook authentication fails

I'm about to give up. for about a week i'm trying the simplest thing, authentication via facebook. every time I authenticatiom (via facebook login url) , getUser() and api('me'
) returns me null.
why?
the code:
$facebook = new Facebook(array(
'appId' => '306597542762982',
'secret' => '88XXXXXX7f1',
));
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if (!$user) {
$loginUrl = $facebook->getLoginUrl(array('scope'=>'user_actions.news,publish_stream,user_about_me,email,publish_actions'));
print $loginUrl; exit;
}
var_dump($facebook->api('me'));
SOS.
EDIT: POST :
$params = array (
'article' => 'http://fb.raal.co.il/content/view/$id/$title',
'access_token' => $facebook->getAccessToken()
);
$out = $facebook->api( '/me/news.reads','post', $params);
var_dump($out); exit;
Continuing what NullPointer quoted earlier, from user ifaour:
Just check for the current Facebook user id $user and if it returned null then you need to reauthorize the user (or use the custom $_SESSION user id value - not recommended)
require 'facebook/src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'APP_ID',
'secret' => 'APP_SECRET',
));
$user = $facebook->getUser();
$photo_details = array('message' => 'my place');
$file='photos/my.jpg'; //Example image file
$photo_details['image'] = '#' . realpath($file);
if ($user) {
try {
// We have a valid FB session, so we can use 'me'
$upload_photo = $facebook->api('/me/photos', 'post', $photo_details);
} catch (FacebookApiException $e) {
error_log($e);
}
}
// login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
// redirect to Facebook login to get a fresh user access_token
$loginUrl = $facebook->getLoginUrl();
header('Location: ' . $loginUrl);
}
I've written a tutorial on how to upload a picture to the user's wall.
With the additional information, does this help? There is additional information I've found related to this, stating to attempt
$facebook->getAccessToken();
Apparently, that call can give either an app access token or a user access token. Depending on what you are trying to do with the access token, you will need the appropriate kind.
Alternatively, you may need to request certain permissions for what you are trying to do, which you can find here.

Unable to post message to my facebook using php sdk

Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user. thrown in C:\xampp\htdocs\fyp\plugin\facebook-sdk\src\base_facebook.php on line 1106
The problem is like mentioned above. I have spend sometime on searching the solution but that still not work after i modified the code. I notice this there is website to get one
https://graph.facebook.com/oauth/authorize?
type=user_agent&
client_id=116122545078207&
redirect_uri=http%3A%2F%2Fxyz.com&
scope=user_photos,email,user_birthday,user_online_presence
Can i include it into my script? so that i don't have to going this url everytime
.And how to include it into my phpscript so that the problem can be fixed?
Thank you for any kind of help.
<?require 'plugin/facebook-sdk/src/facebook.php';
$session='123456';
$facebook = new Facebook(array(
'appId' => '1234567',
'secret' => '123456789',
'cookie' => true,
));
$me = null;
// Session based API call.
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
}
}
// login or logout url will be needed depending on current user state.
if ($me) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
}
if ($session) {
// We have a valid FB session, so we can use 'me'
$result = $facebook->api('/me/feed','post',array('message' => 'testmessage'));
} elseif( isset($_SESSION['user_id']) ) {
$result = $facebook->api("/{$_SESSION['user_id']}/feed",'post',array('message' => 'testmessage'));
}
?>
Edit Scope ,
scope=user_photos,email,user_birthday,user_online_presence,publish_stream
& visit this link again
https://graph.facebook.com/oauth/authorize?
type=user_agent&
client_id=116122545078207&
redirect_uri=http%3A%2F%2Fxyz.com&
scope=user_photos,email,user_birthday,user_online_presence,publish_stream

Login using SDK 3.1, $facebook->getUser() is zero?

So I've setup the basic example code the SDK suggests.
I'm having such a problem, well many problems as I'm sure you all are too.
I've used both the session approach with SDK 2.12, my current problem is, even though I'm passing the getLoginUrl a redirect_uri, it ignores this and redirects it to my base_domain +
'{"session_key":"badsdsadsdsdasdsd.0-1268121831","uid":"badsdsadsdsdasdsd","expires":0,"secret":"730aa9badsdsadsdsdasdsd","base_domain":"slumber-roo.co.uk","access_token":"badsdsadsdsdasdsdEZBZAZAvdRAZC6PwPeBZBYMZD","sig":"badsdsadsdsdasdsdd761385"}'
Obviously filled in with keyboard smash.
Now, my attempt with SDK 3.1 is weird. I have a valid getUser, but my colleague doesn't and gets stuck in the infinite loop.
I do understand the differences between getSession and getUser with SDK 2 and 3 respectively.
Here is my code:
<?php
$user = null; //facebook user uid
try{
include_once "facebook.php";
}
catch(Exception $o){
error_log($o);
}
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'cookie' => true
));
//Facebook Authentication part
$session = $facebook->getSession();
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
print_r($me);
} catch (FacebookApiException $e) {
error_log($e);
}
} else {
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'email,offline_access,publish_stream,user_hometown',
'redirect_uri' => 'http://www.facebook.com/pages/CharlesTestPage/225802194155435?sk=app_252946408094785'
)
);
exit('<script> top.location.href="'.$loginUrl.'"; </script>');
}
function d($d){
echo '<pre>';
print_r($d);
echo '</pre>';
}
?>
Oh, in addition, I'm running this app solely inside a tab.
Thanks in advance.
Is the application in sandbox mode? Have you added your collegue as an ADMIN || DEVELOPER || TESTER in the app config?
I just got that this morning, my project manager was in an infinite loop, added her as tester in the app but she hadn't accepted the pending invite and was looping.

How to use facebook connect on mobile web application using the OAuth Dialog

Now I'm developing a mobile web application using facebook connect, but I am having problems with the OAuth Dialog. I use the following facebook documents for my reference:
http://developers.facebook.com/docs/guides/mobile/#web
I choose to use the OAuth Dialog instead of the Login Button because mobile browsers do not recognize the FBML (I use the Blackberry browser on testing). The OAuth Dialog also lets me add a list of permissions within the parameters of scope. But the problem is when I've logged in using the OAuth Dialog, the parameter $me wasn't recognized so that the login button still appears and doesn't not change to the logout button.
Here is an example of my code:
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'cookie' => true,
));
$session = $facebook->getSession();
$me = null;
// Session based API call.
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
$accessToken = $facebook->getAccessToken();
} catch (FacebookApiException $e) {
error_log($e);
}
}
if ($me) {
echo "<img src=\"images/logoutFB.gif\">";
} else {
echo "<img src=\"images/loginFB.gif\">";
}
Is the parameter $me can't be used if I use the OAuth Dialog to connects to facebook?
So how do I know that I am already logged into facebook or not if I use the OAuth Dialog?
Please help if you guys have the solution.
Looks like you need to use the updated PHP-SDK
getSession() has been replaced by getUser():
require './src/facebook.php';
$facebook = new Facebook(array(
'appId' => '135669679827333',
'secret' => 'xxxxxxxxxxxxxxxx',
));
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
$user = null;
}
}

Categories