I've been struggling to find out what is happening with this. My scripts were working fine for a bit and suddenly half stopped.
I'm accessing the api and am getting back an access token. With the access token, I can access a users public info just fine. However, when I try to post info to their FB account I get this error.
Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user.
Any idea what is happening here? I'm also using sessions on my site to keep track of internal user ids. Not sure if my sessions could be causing a problem.
This is my upload script where I'm getting an error.
require 'facebook/src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => '12345678',
'secret' => 'REMOVED',
'fileUpload' => true,
'cookie' => true,
));
$facebook->setFileUploadSupport(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();
}
$photo_details = array('message' => 'my place');
$file='photos/my.jpg'; //Example image file
$photo_details['image'] = '@' . realpath($file);
$upload_photo = $facebook->api('/me/photos', 'post', $photo_details);
I had the same issue but I can solve this issue by cleaning the cookies and cache from my browser
(ctrl+shift+R)
. Also, you must ensure that your access token is active.Hope this will help you to solve your problem.
I have got the same issue when tried to get users information without auth.
Check if you have loggen in before any request.
The code above should solve your issue.
After a certain amount of time, your access token expires.
To prevent this, you can request the 'offline_access' permission during the authentication, as noted here: Do Facebook Oauth 2.0 Access Tokens Expire?
So I had the same issue, but it was because I was saving the access token but not using it. It could be because I'm super sleepy because of due dates, or maybe I just didn't think about it! But in case anyone else is in the same situation:
When I log in the user I save the access token:
Now when I make requests to facebook I just do something like this:
Use:
instead of
it works.
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)I've written a tutorial on how to upload a picture to the user's wall.