How are you supposed to deal with people who signed into Facebook a while ago. Come to your site you should them a continue link (because you detect that they are already logged into Facebook) and then on the page you direct them to you get this error.
Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user. thrown in /var/www/html/lib/base_facebook.php on line 1039
I don't understand how you are supposed to prevent this? Does this have something to do with the signed info that you give back to Facebook to get an access_token? Seems like this signed info can expire (it has an issued_at date). What is the correct way to handle this in your website's flow?
Are you expected to write code like this:
<?php
$user = $facebook->getUser();
try {
// attempt to do a call just to see if you are going to have this issue
$profile = $facebook->api('/me');
} catch (Exception $e) {
$user = false;
}
if ($user) { ?>
Begin
<?php } else { ?>
<fb:login-button scope="email" size="large">Connect</fb:login-button>
<?php } ?>
Instead of this:
<?php
$user = $facebook->getUser();
if ($user) { ?>
Begin
<?php } else { ?>
<fb:login-button scope="email" size="large">Connect</fb:login-button>
<?php } ?>
Getting the $user back from the Facebook SDK only seems to tell you there is a cookie. And not if that will actually work when you go to do the API calls.
UPDATE:
So my only problem with this method is ... when the user does have a cookie on my site, but the API call fails - I show them the connect button. User clicks the connect button, it quickly appears and disappears. Because it wasn't a true 'auth.login' that just occured, the user will not get sent to my start.php page via JavaScript redirect. How do others handle this? I'm stumped. Please tell me if there are other flaws with how I'm attempting to do this.
Try passing the access token to the API call that verifies that the user has authorized your application. Below is what I do, and it should help to alleviate the OAuthException you're getting.
$user = $facebook->getUser();
if($user) {
$access_token = $facebook->getAccessToken();
$params = array('access_token' => $access_token);
try {
$me = $facebook->api("/me", $params);
} catch(FacebookApiException $e) {
$user = null;
}
}
if($me) {
// proceed with authenticated user with an active access token.
}
Are you expected to write code like this:
Yes, you will have to handle the OAuthException.
But rather than using FBML login button, you can redirect the user to facebook login url for your application. You can get the url by using function getLoginUrl provided by Facebook PHP SDK. See getLoginUrl for more information.
For your email permissions you can use following array:
$params = array( 'scope' => 'email' );
Once successfully logged in, the user will be redirected back to the application page which redirected him to facebook login.
Related
I develop a Facebook application and I use PHP-sdk I use the algorithm described in the official documentation to allow user joining my app then I store his/her user_id in the database. My app uses publish_stream permissions so it publishes posts to the user's time line automatically.
I found that the application, always, does not able to publish posts for some of the registered users in the database (not all of them). I account this for they may be registered to the database before they give approval for the required permissions. The following is a blue print for the code that I use from the official documentation of Facebook api:
<?php
require_once('php-sdk/facebook.php');
$config = array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
?>
<html>
<head></head>
<body>
<?php
if($user_id) {
try {
/* $user_profile = $facebook->api('/me','GET');
echo "Name: " . $user_profile['name'];*/
//Here I run the code to save.
saveTheUserIdToRecordInTheDB($user_id);
//hypothetical function to save
} catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
// No user, print a link for the user to login
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
}
?>
</body>
</html>
My, main, question is: If the user clicked on the login link, and then he/she does not continue for any reason (losing the internet connection, refusing giving the permissions to the app, etc), would it possible the hypothetical function saveTheUserIdToRecordInTheDB() to run and save that user's Facebook's ID to the database?
Or What's the explanation for there are some users are not posts publishable?
I have problem with authorization of my facebook application. When I run my application normally on my local web server process goes like this:
**Get facebook user->If user is not logged on -> Redirect user on facebook login page**
**Get facebook user->If user is logged on -> Redirect user to authorize my facebook application->everything goes right without problems**
When I host my application on facebook using iframe, second process is not working (user never get show the authorization window), and I get following exception:
Error validating access token: User xxxxxxx has not authorized application xxxxxxx.
I'm using CodeIgniter PHP framework, and this is my PHP code:
$this->load->library('facebook');
$user = null;
$user_profile = null;
$user = $this->facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$access_token = $this->facebook->getAccessToken();
$user_profile = $this->facebook->api($user);
}
catch (FacebookApiException $e) {
show_error(print_r($e, TRUE), 500);
}
}
else{
$data = array(
'redirect_uri' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx','scope'=>'email');
$access_token = $this->facebook->getAccessToken();
redirect($this->facebook->getLoginUrl($data));
}
$data['facebook']=$user_profile;
$this->load->view('templates/header');
$this->load->view('pages/prijavaKlasicnoFacebook', $data);
$this->load->view('templates/footer');
}
You can not load the OAuth dialog in an iframe – security reasons.
You have to load it into the top-most window instance – so no server-side redirect, but a client-side “redirect” using top.location.href = "…";
Get your script to output this JS snippet (including the correct login URL) inside an otherwise empty document.
You should also provide "Read Stream" and "Publish Data" in your scope.
I'm working on a site that uses the Facebook PHP SDK. It is working great, and after 2 hours, whenever I try to get onto one of the general pages on the site, I get...
Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user.
If I go back to the index page, I get a new access token and everything works properly again...for 2 hours.
What code can I put on my webpages to redirect to the index page when the access token has expired?
Thanks for helping this noob.
You must redirect user to page that has authorized code.
If your user not deauthorized app , the Oauth dialog isn't appear.
For more information go to
Access Token & Handling
You can put a try / catch block in your code.
Put each Facebook API call in such block and if an exception occurs you can redirect the user to the right resource by header('Location: <url>');:
try {
# FB API call
}
catch(OAuthException e) {
header('Location: <url>');
}
Have a file that goes like this and include it in your webpages.
$facebook = new Facebook(array(
'appId' => "app id",
'secret' => "app secret",
));
$user = $facebook->getUser();
$loginUrl = $facebook->getLoginUrl();
if ($user) {
try {
$me = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
}
}
else {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
}
I am trying to integrate my Codeigniter website with the Facebook PHP SDK. What I want to do is let a user share an article from my site on their facebook wall, if they are logged into a facebook account. My library appears to load correctly, but everytime I try to do something, I get some kind of error... primarily with the auth. getUser does not appear to return the correct results. I set up my facebook application and set the config vars for my library, but no luck. It says I am not logged into facebook. When I click on the "login" anchor, the link takes me to the same page, but with the facebook url, and doesn't ask me to login with the app. Here's my code:
function facebook($article_id){
$config = array(
'appId' => '276870792431073',
'secret' => '8d49eee575413fb9a8063d22f65dbf6a'
);
$this->load->library('facebook', $config);
$user = $this->facebook->getUser();
if($user){
try {
$user_profile = $this->facebook->api('/me');
} catch (FacebookApiException $e){
error_log($e);
$user = null;
}
}
if($user){
$article = $this->article->fetch_article($article_id);
$config = array(
'message' => 'I just read an '.anchor('articles/'.url_title($article['title']).'/'.$article_id, 'article').' on '.anchor('', 'TrackTheOutbreak.com').'!',
);
$this->facebook->api('/me/feed', 'post', $config);
} else {
$data['MESSAGE_TITLE'] = 'Authentication Error';
$data['MESSAGE_TEXT'] = 'You must be logged into an existing Facebook account to use this feature. Click '.anchor($this->facebook->getLoginUrl(), 'here').' to login.';
$this->parser->parse('error_body.tpl', $data);
}
}
In order to access a users information and post anything to their wall you first need to get a access token from them. To do that you need to make sure that you have gained their permission through Facebook's FB_login (and then Open Graph). I would double check with this guide and make sure that you have everything set up properly to post to their timeline.
https://developers.facebook.com/docs/reference/javascript/FB.login/
I hope this helps
I have a website and a facebook page for the website. For most of the news update, I post them on the facebook page's wall.
Now I want to show the wall's content of that facebook page on my website, just like the facebook's Like Box with "stream" enabled: http://developers.facebook.com/docs/reference/plugins/like-box/
However, I only want to show the stream, and perfectly can show it in my own presentation. So is it possible to get only the content of a facebook page by facebook's API?
I think if you use the SDK you can get it by using
fb->api("/{id}/feed");
There's a php and a javascript sdk.
But you also need an access token now to get the feed of a page.
EDIT: Here's a copy of example.php (from the php-sdk) modified for your purpose.
include_once("facebook-sdk/facebook.php");
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'APP_ID GOES HERE',
'secret' => 'SECRET GOES HERE',
));
// Get User ID
$user = $facebook->getUser();
// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
// This is where we grab the posts
$wall_posts = $facebook->api('/courseyou/posts');
} 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();
} else {
$loginUrl = $facebook->getLoginUrl();
}
?>
<?php if ($user): ?>
Logout
<?php else: ?>
<div>Login with Facebook</div>
<?php endif ?>
<h3>Wall Posts</h3>
<?php
foreach ($wall_posts["data"] as $post) {
echo "<p>".$post["from"]["name"].": ".$post["message"]."</p>";
}
?>
As far as I know you need an access token to view a page's posts/feed with the api, which makes me think that you need the user to login with facebook.... I'm fairly new to this, but you should look into how to get an access token, because you need one for this.