Unable to post message to my facebook using php sdk - php

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

Related

Trouble with Facebook Api login $user is not returning properly

When I run this code, and my cookies have been cleared and I am not logged into facebook. It directs me to facebook and I log in but when it brings me back to my page it is the same, where as it should be show me profile pic and so on...
I seem to have narrowed down the problem of the return statement of the $user because it returns a 0. I have stared at this code for a long time and I havent found what I am doing wrong.
What do i need to change to get it so that When i return from logging in from facebook it will show the profile pic and so on...
<?php
require_once 'libs/facebook.php';
require 'connections/connection.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxx',
));
// Get User ID
$user = $facebook->getUser();
echo $user;
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
echo $user;
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
//$logoutUrl = $facebook->getLogoutUrl();
$logoutUrl = $facebook->getLogoutUrl(array('next' => ($fbconfig['baseurl'] . 'logout.php')));
} else {
$statusUrl = $facebook->getLoginStatusUrl();
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'user_about_me',
'scope' => 'read_friendlists'
));
}
?>
Not to worry my friend, you are not doing anything wrong. I'm having same issue with my DEMO application which was running alright till yesterday.
I'm sure you are having issue with getting access token as well. I think issue is with PHP SDK only.So probably will be fixed in some time.
SOLUTION : This worked for me after trying for many solutions for this issue.
In base_facebook.php file, find the makeRequest() method and check for following Line.
$opts = self::$CURL_OPTS;
Immediately following it, add this line
$opts[CURLOPT_SSL_VERIFYPEER] = false;
More details can be found here - http://net.tutsplus.com/tutorials/php/how-to-authenticate-your-users-with-facebook-connect/

Same facebook problems ... OAuthException: An active access token must be used to query information about the current user

So this is working fine... But when I refresh the page twice(or click on two pages who include this script fast) it gives this error
OAuthException: An active access token must be used to query information about the current user.
Any ideas?
<?php
$app_id = '***************';
$app_secret = '**************';
$app_namespace = '****************';
$app_url = 'http://apps.facebook.com/' . $app_namespace . '/';
$scope = 'email,publish_actions';
// Init the Facebook SDK
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
));
// Get the current user
$user = $facebook->getUser();
// If the user has not installed the app, redirect them to the Login Dialog
if (!$user) {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => $scope,
'redirect_uri' => $app_url,
));
print('<script> top.location.href=\'' . $loginUrl . '\'</script>');
}
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me', 'POST');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
//<img src="https://graph.facebook.com/?php echo $user; ?/picture">
//?php print_r($user_profile); ?
?>
I personally find it easier to manage the access_token myself. Facebook's Graph API secures protected endpoints by requiring that an access_token be passed in. The PHP SDK abstracts this away, but I have never been comfortable with Facebook handling the information because sometimes it just doesn't work. This isn't to say that the library is bad, but only that I haven't been using it correctly. Keep this caveat in mind.
It looks like you're working with a Facebook Canvas App. When the user successfully authenticates for the first time, Facebook will send an access_token in $_GET. At this point, you should save this to your database, since that access token is good for 3 months or so.
At the point, from then on, you can pass in the access_token in the call parameters:
try {
$user_profile = $facebook->api('/me', 'POST', array(
"access_token" => '' // access token goes here
));
} catch (FacebookApiException $e) {
// error handling
}
Given that Facebook is returning the error that you need an access token in order to call the /me resource, it looks like $facebook->getUser(); is returning something. You may want to double-check what it is.
While I'm here, you're using this logic:
if (!conditional) {
// do something
}
if (conditional) {
// do something else
}
Confusing. Use else:
if (conditional) {
// do something
} else {
// do something else
}

post message on facebook wall not working

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.

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.

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