[英]$facebook->getSession() returns null in the example code. is that ok?
運行Facebook API的示例代碼我得到一個空會話對象,我應該得到一個非空對象,在代碼中給出注釋。 我究竟做錯了什么?
換句話說,在我的index.php中,當我在瀏覽器中訪問http://apps.facebook.com/my_app
時,示例代碼中的這個片段顯示“無會話” :
<?php
require './facebook.php';
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => '...', // actual value replaced by '...' for this post
'secret' => '...', // actual value replaced by '...' for the post
'cookie' => true,
));
// We may or may not have this data based on a $_GET or $_COOKIE based session.
//
// If we get a session here, it means we found a correctly signed session using
// the Application Secret only Facebook and the Application know. We dont know
// if it is still valid until we make an API call using the session. A session
// can become invalid if it has already expired (should not be getting the
// session back in this case) or if the user logged out of Facebook.
$session = $facebook->getSession();
if ($session) {
echo "session ok";
}
else {
echo "no session";
}
?>
注意:在我的服務器中,index.php和facebook.php在同一個文件夾中。
你有鏈接你的應用程序了嗎?
if ($session) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
}
<?php if ($me): ?>
<a href="<?php echo $logoutUrl; ?>">
<img src="http://static.ak.fbcdn.net/rsrc.php/z2Y31/hash/cxrz4k7j.gif">
</a>
<?php else: ?>
<div>
Using JavaScript & XFBML: <fb:login-button></fb:login-button>
</div>
<div>
Without using JavaScript & XFBML:
<a href="<?php echo $loginUrl; ?>">
<img src="http://static.ak.fbcdn.net/rsrc.php/zB6N8/hash/4li2k73z.gif">
</a>
</div>
<?php endif ?>
代碼取自您提供的鏈接。
請在http://forum.developers.facebook.net/viewtopic.php?pid=267627查看#8的帖子
它對我有用。 謝謝用戶“standardcombo”。
為方便起見,我在這里復制了它。
我不確定你是否已經得到了這個答案,但這是我自己完成的工作。 即使用戶登錄到Facebook,第一次嘗試獲取會話時也會為空。 我發現的示例應用程序在表單上放置了一個登錄按鈕 - 如果你這樣做,那么用戶必須點擊按鈕,即使他們已經登錄到Facebook。 它們不會再次被提示,但這是一個額外的用戶操作。
因此,在我的應用程序和我在論壇中找到的其他幾個工具中,只是重定向到登錄URL。 (如果你檢查了url,它的一個參數是“return_session = 1”。)當它返回時,你將有一個會話並且可以正常進行。
但是在我的應用程序中,如果我沒有應用程序令牌,那么我也無法獲得該會話,因此我必須先獲取應用程序令牌。 要獲取應用程序令牌,請查看http://forum.developers.facebook.com/viewtopic.php?id=56789中的優秀說明,2010年5月7日來自dynamoman的帖子(關於第四篇文章)。
我遇到的一件事是,無論我在哪里告訴身份驗證是“下一頁”,它都會轉到畫布中配置的頁面。 因此,除了三個單獨的頁面,我只有兩個,並且畫布回調URL必須處理它所處的任何狀態。
我的實際代碼在框架內部,因此不能直接應用; 作為算法,它是:
landing page:
if the facebook api token is not loaded,
redirect to the authorization url
try to load the user id // to validate that the session
if the user id is not loaded,
redirect to the loginurl from the facebook api
// if it reaches here, then we have both an api token and a session
authorization page:
get authorization token // see the post for how to do that
redirect back to the page configured as the canvas url
可能有更好的方法,我敢肯定比我更熟悉這個的人可以有更好的解決方案,或者發布通用代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.