I have an application which is in a Facebook fan page's tab.开发者_如何学Go
When a user access to this page's tab, I request to him some permissions.
My issue is that, when the user accept the permissions, he is redirected to http://apps.facebook.com/ and not the page's tab.
$my_url = "https://apps.facebook.com/<my_app_name>/";
If I change my_url value to http://www.facebook.com/pages/, then the user is on an infinite loop.
My code:
if (isset($_REQUEST["code"])) {
$code = $_REQUEST["code"];
}
if(empty($code)) {
// Get permission from the user to manage their Page.
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&scope=read_stream";
echo('<script>top.location.href="' . $dialog_url . '";</script>');
} else {
//my code
}
Thanks.
That is because the code
variable is never passed to the contents of your iFrame tab via the Fan Page/Application Tab URL (if you use an application tab URL no additional parameters will be passed to the iFrame URL). As such, going by the code example above the, JavaScript redirect to the authentication dialog will always passed to the client browser.
You should look at using the latest PHP SDK (v3.1.1) with the JavaScript SDK to do your authentication: http://developers.facebook.com/blog/post/534/
Code example from the above blog post:
require 'php-sdk/src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
));
// See if there is a user from a cookie
$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) {
echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
$user = null;
}
}
?>
<!DOCTYPE html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<body>
<?php if ($user_profile) { ?>
Your user profile is
<pre>
<?php print htmlspecialchars(print_r($user_profile, true)) ?>
</pre>
<?php } else { ?>
<fb:login-button></fb:login-button>
<?php } ?>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId: '<?php echo $facebook->getAppID() ?>',
cookie: true,
xfbml: true,
oauth: true
});
FB.Event.subscribe('auth.login', function(response) {
window.location.reload();
});
FB.Event.subscribe('auth.logout', function(response) {
window.location.reload();
});
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
</script>
</body>
</html>
精彩评论