[英]Facebook API: Login using JavaScript SDK then checking login state with PHP
當用戶單擊“登錄”按鈕時,我正在使用Facebook的JavaScript SDK彈出“登錄彈出窗口”。
正如Facebook在文檔中提供的那樣,代碼是:
$(".loginButton").click(function(){
FB.login(function(response) {
FB.api('/me', function(response) {
console.log(response.id);
//User ID shows up so I can see that the user has accepted the app.
});
});
我還可以使用FB.getLoginStatus()
來檢查用戶是否確實已登錄並接受了該應用程序。
但是,現在我想用PHP執行一個函數。 據我了解,PHP具有$user
,在成功登錄后會為其分配UserID。
問題是在JS Login $user
仍然為0
。 我被卡住了,無法弄清楚為什么它不能為$user
分配正確的用戶ID。
這是我的JS:
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({
appId : '<?php echo $AppId; ?>',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true,
channelUrl : '<?php echo $ServerPath; ?>channel.php' // custom channel
});
};
//Get Login Status
function amILoggedIn(){
var toreturn = "";
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
window.userId = response.authResponse.userID;
toreturn = "GetLoginStatus: logged in" + " " + "<?php echo $user; ?>";
} else {
toreturn = "GetLoginStatus: Logged Out";
}
console.log(toreturn);
});
};
// Load the SDK asynchronously
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
var obj = {
method: 'feed',
link: 'http://www.google.com'
};
$(".loginPopupButton").click(function(){
FB.login(function(response) {
FB.api('/me', function(response) { //user-specific stuff
console.log(response.id);
});
});
});
</script>
這是PHP:
<?php
include_once('fbapi/facebook.php');
include_once('fbapi/Authenticated_User.php');
include_once('config.php');
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => $AppId,
'secret' => $AppSecret
));
//Facebook Authentication part
$user = $facebook->getUser();
$perms = "manage_pages";
if(isset($_GET['backlink']))
$redirectUrl = urldecode($_GET['backlink']);
else
$redirectUrl = $fullserverpath;
$cancelUrl = $ServerPath."index.php?page=loginWFb";
//AA - where to go to get FB popup.
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => $perms,
'redirect_uri' => $redirectUrl,
'cancel_uri' => $cancelUrl
)
);
//AA- Defining that a powerUSER is someone who's logged in
if(isset($user)){
$_SESSION['loggedIn'] = $user;
}
?>
使用JS SDK登錄將登錄數據存儲在Facebook cookie中,而使用PHP SDK登錄將數據存儲在PHP會話中。 因此,如果您使用JS SDK登錄,PHP SDK將不會意識到這一點。
要使用JS SDK數據登錄PHP SDK,您需要使用查詢參數中帶有簽名的請求來調用PHP頁面:
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// Full redirect or ajax request according to your needs
window.location.href = 'login.php?signed_request='+response.authResponse.signedRequest;
}
});
然后$facebook->getUser()
將在您的PHP代碼中提供用戶ID。
在PHP中初始化Facebook對象時,請嘗試將sharedSession
參數設置為true
:
$facebook = new Facebook(array(
'appId' => $AppId,
'secret' => $AppSecret,
'sharedSession' => true
));
您可以從這里找到工作示例
http://liferaypower.blogspot.com/2013/10/index.html
樣例代碼
FB.init({
appId: '490986011016528', // App ID
channelUrl: 'http://localhost:8000/', // Channel File
status: true, // check login status
cookie: true, // enable cookies to allow the server to access session
xfbml: true // parse XFBML
});
FB.Event.subscribe('auth.authResponseChange', function(response)
{
if (response.status === 'connected')
{
document.getElementById("message").innerHTML += "ConnectedtoFacebook";
}
else if (response.status === 'not_authorized')
{
document.getElementById("message").innerHTML += "<br>Failed to Connect";
//FAILED
}
else
{
document.getElementById("message").innerHTML += "<br>Logged Out";
//UNKNOWN ERROR
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.