Facebook's PHP SDK gives OAuth error to some users only
I've setup Facebook Connect so that users can register for my site with one click, and it works flawlessly for the majority of users, but I've gotten a few e-mails from users that get the following error:
PHP Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user.\n thrown in /home/example/public_html/facebook.php on line 453
I am unable to duplicate this error, and so I am having a hard time figuring out what is causing this.
My Facebook Connect button code:
<fb:login-button size="large" length="long" v="2" perms="email,user_birthday" onlogin="fb_login()"></fb:login-button>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({appId: '.....', status: true, cookie: true, xfbml: true});
};
开发者_开发百科 (function() {
var e = document.createElement('script');
e.type = 'text/javascript';
e.src = document.location.protocol +
'//static.ak.fbcdn.net/connect/en_US/core.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
function fb_login() {
FB.getLoginStatus(function(response) {
if (response.session) {
window.location = 'http://example.com/register_fb.php';
}
});
}
</script>
My PHP code:
require './facebook.php';
$facebook = new Facebook(array(
'appId' => '....',
'secret' => '....',
'cookie' => true,
));
$session = $facebook->getSession();
$user = $facebook->api('/me');
//// register the user or log them in...
Narrowing down what browsers they are using would help to debug this.
Absent that and based on your code, I can think of one thing that would cause this issue; the inability of the Javascript SDK to set the cookie via the login-button.
This can happen in certain browsers depending on framing, etc.
Handling this issue, you can detect if the facebook->getSession() is null and then redirect to $facebook->getLoginUrl() (which is a good idea regardless).
精彩评论