I was following the tutorial here: http://net.tutsplus.com/tutorials/php/how-to-authenticate-your-users-with-facebook-connect/
But I can't get past the first step in allowing users to authorize my app. The app is at http://apps.facebook.com/freelancergame/ while the canvas URL is http://freelancer.xylotgames.com
I check via PHP whether there is an active session with the user. If it doesn't exist, I redirect the user to the login URL. Here is the code at the top of the page:
// Facebook library
require 'src/facebook.php';
// Create our Application instance
$facebook = new Facebook(array(
'appId' => 'xxxxxx',
'secret' => 'xxxxxxxxxxxx',
'cookie' => true,
));
// Let's see if we have an active session
$session = $facebook->getSession();
$me = null;
// Session based API call.
if (!empty($session)) {
// Active session; let's try getting the user ID and info
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
}
$logoutUrl = $facebook->getLogoutUrl();
print_r($user);
exit();
} else {
// There's no active session; let's generate one
$loginUrl = $facebook->getLoginUrl();
header('Location: ' . $loginUrl);
}
The thing is, upon visiting http://apps.facebook.com/freelancergame/ I am brought to a screen with the blue Facebook logo and a link saying "Go to Facebook.com".
However, if I allow the user to click a link to connnect with Fa开发者_运维问答cebook like so:
<a href="<?php echo $loginUrl ?>" target="_top">Connect</a>
...the "Allow/Don't Allow" dialog shows up directly from Facebook's website, but if I click "Allow" or "Don't Allow", it goes directly to my canvas page (http://freelancer.xylotgames.com) instead of the embedded app page, which I'd prefer.
How can I get this to work? I see apps/games that use a pop-up instead of redirection. Is this preferred? If so, how would I do it? Why won't the NetTuts+ tutorial work for me when it most likely works for others?
I prefer the popup method (or iframe dialog inside Facebook), which you can open with the JS SDK:
Regular HTML popup:
FB.login
function. http://developers.facebook.com/docs/reference/javascript/fb.login/Iframe dialog:
FB.ui
function. How to show the Extended Permission Dialog in FB using new Graph API?
For your redirection issue, I would double check the URLs in the configuration of the app.
I had this problem. I think the thing you need to change is the actual redirecting. Your login URL is loading inside the iframe, because that's all that headers can do. FB detects that and gives you a button to help break out of the frame. If you want to skip that, you need to redirect the top level of the browser to the login url.
Instead of header('Location: ' . $loginUrl);
use:
die("<script type='text/javascript'>top.location.href = '" . $loginUrl. "';</script>");
It works perfectly for me, my app has completely seamless login and session refreshing. You can even spit this out during an ajax request that has an expired session, and it will get a new session for you.
精彩评论