Facebook login with either PHP or Javascript SDK not working
as the title states, I am having some trouble getting the user logged in on my Facebook application.
When the user is logged out, and they click log in
, the page reloads and then displays a Facebook logo with the text Go to Facebook.com
below it, and when I try using the Javascript SDK, I do get the login page in a popup box, but once I press Login
, the popup redirects to a XD Proxy
page, where I get stuck, and I have to manually close the popup to have the callback execute.
In both cases I get this error in the Chrome console,
Error I get in console, when using the Javascript SDK:
Unsafe JavaScript attempt to access frame with URL https://www.facebook.com/dialog/permissions.request?etcetc... from frame with URL http://subdomain.mysite.com/folder/?etcetc... Domains, protocols and ports must match.
Error I get in console, when using the PHP SDK:
Unsafe JavaScript attempt to access frame with URL http://apps.facebook.com/my_app/ from frame with URL https://www.facebook.com/login.php?etcetc... Domains, protocols and ports must match.
Here is my code,
PHP:
<?php
require 'libs/facebook/src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'APP ID',
'secret' => 'SECRET',
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
echo "<a href='".$logoutUrl."'>Login</a>";
} else {
$loginUrl = $facebook->getLoginUrl();
echo "<a href='".$loginUrl."'>Login</a>";
}
?>
I am using version 3.0 of the PHP SDK.
After the PHP failed, I tried using the Javascript SDK instead, to handle the login and logout,
Javascript:
FB.init({
appId : 'APP ID',
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) {
$('#login').hide();
$('#logout').show();
} else {
$('#logout').hide();
$('#login').show();
}
});
$("#logout").live('click', function(){
FB.logout(function(response) {
window.location.reload();
});
});
$("#lo开发者_开发问答gin").live('click', function(){
FB.login(function(response) {
window.location.reload();
});
});
Any help would be greatly appreciated, thank you!
Ok, so after banging my head against the board for a while, I came up with this, and it does the trick!
Using the PHP SDK, it is redirecting the user to the login page if they are not logged in, and setting the Logout
URL with the next
parameter for later use wherever/whenever you want it.
And no more XSS errors! :)
if ($user) {
$logoutUrl = $facebook->getLogoutUrl(array('next'=>'http://apps.facebook.com/your_app/'));
} else {
$loginUrl = $facebook->getLoginUrl(array('redirect_uri'=>'http://apps.facebook.com/your_app/'));
echo "<script>top.window.location='".$loginUrl."';</script>";
}
I hope this helps!
精彩评论