[英]Facebook login with either PHP or Javascript SDK not working
如標題所述,我在讓用戶登錄我的 Facebook 應用程序時遇到了一些麻煩。
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彈出框,但是一旦我按下Login
,彈出窗口就會重定向到XD Proxy
頁面,我被卡住了,我必須手動關閉彈出窗口才能執行回調。
在這兩種情況下,我都會在 Chrome 控制台中收到此錯誤,
使用 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 ,協議和端口必須匹配。
使用 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.
這是我的代碼,
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>";
}
?>
我正在使用 PHP SDK 的 3.0 版。
在 PHP 失敗后,我嘗試使用 Javascript SDK 來處理登錄和注銷,
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();
});
});
$("#login").live('click', function(){
FB.login(function(response) {
window.location.reload();
});
});
任何幫助將不勝感激,謝謝!
好的,所以在我的頭撞在板上一會兒之后,我想出了這個,它成功了!
使用 PHP SDK,如果用戶未登錄,它會將用戶重定向到登錄頁面,並設置Logout
URL 以后隨時隨地使用next
參數。
不再有 XSS 錯誤::)
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>";
}
我希望這有幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.