I’m using the Facebook PHP SDK (2.1.2). All I want to do is what almost every Facebook application with req_perms has. The stupid “request for permissions” box to pop up when you install it.
I do not want a button that the user has to push. I do not want a popup to appear. I do not want to use FBML, since they’re doing away with it.
This is the standard Facebook permissions dialog that shows up where my application should show up in the canvas iframe.
I tried:
<?php if(!$me): ?>
<head>
<meta HTTP-EQUIV="REFRESH" content="0; <?php echo $loginUrl; ?>">
</head>
<?php die(); ?>
<?php endif; ?>
That, for some reason, showed a Facebook logo linked to the correct URL I wanted (not what I want!)
<?php if($me): ?>
window.fbAsyncInit = function() {
FB.init({
appId : '<?php echo $appid; ?>',
session : <?php echo isset($session) ? "'".json_encode($session)."'" : 'null'; ?>, // don't refetch the session when PHP already has it
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
// Whenever the user logs in, we refresh the page
FB.Event.subscribe('auth.login', function() {
window.location.reload();
});
};
(function() {
var e = document.createElement('script');
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
</script>
<fb:redirect url="<?php echo $loginUrl; ?>" />
<?php die("</html>"); ?>
This one showed a blank page.
<?php if($me): ?>
window.fbAsyncInit = function() {
FB.init({
appId : '<?php echo $appid; ?>',
session : <?php echo isset($session) ? "'".json_encode($session)."'" : 'null'; ?>, // don't refetch the session when PHP already has it
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
// Whenever the user logs in, we refresh the page.
FB.Event.subscribe('auth.login', function() {
window.location.reload();
});
};
(function() {
var e = document.createElement('script');
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
</script>
<script>
FB.login(function(response) {
if (response.session) {
if (response.perms.indexOf('publish_stream') != -1) {
//User has logged in and given us publish_stream permissions
);
else {
//User has logged in but not given us publish_stream
}
}
else {
//User is not logged in
}, {perms:'offline_access,publish_stream'});
</script>
This one also shows a blank page.
What I want isn’t unpopular, but the Facebook documentation is the worst documentation I’ve ever come accross. Nothing works. Something this idiotically simple should not take two days to figure out.
Here’s the solution I ended up with:
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '<?php echo $appid; ?>',
session : <?php echo isset($session) ? "'".json_encode($session)."'" : 'null'; ?>, // don't refetch the session when PHP already has it
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
FB.getLoginStatus(function(response) {
if (response.session) {
// Logged in and connected user, someone you know.
} else {
// No user session available, redirect them to login.
window.top.location = "<?php echo $loginUrl; ?>";
}
});
// Whenever the user logs in, we refresh the page.
FB.Event.subscribe('auth.login', function() {
window.location.reload();
});
};
(function() {
var e = document.createElement('script');
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
</script>
Basically, you use the PHP SDK to get the login URL and then do a JavaScript redirect to that URL. The example
http://github.com/facebook/php-sdk/blob/master/examples/example.phpdoes everything for you except for the redirect. In the example, they have a link you click to login. If you want it to be automatic, do the following.