2010-05-10 56 views
4

運行example code for the 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"; 
} 


?> 

注:在我的服務器指標。 php和facebook.php在同一個文件夾中。

+0

喜託託,我有同樣的問題。不過,我的不是一個畫布頁面。這只是在我的PHP文件。我可以知道你是如何解決它的?我曾要求用戶在此之前的頁面上登錄Facebook。謝謝。 – davidlee 2010-12-28 08:02:11

回答

3

您是否已將您的應用關聯?

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 &amp; XFBML: <fb:login-button></fb:login-button> 
</div> 
<div> 
Without using JavaScript &amp; XFBML: 
<a href="<?php echo $loginUrl; ?>"> 
<img src="http://static.ak.fbcdn.net/rsrc.php/zB6N8/hash/4li2k73z.gif"> 
</a> 
</div> 
<?php endif ?> 

代碼取自您提供的鏈接。

+0

Gazler,我現在有點困惑:示例代碼(onw我鏈接)是打算用於我自己的網站,而不是在畫布框架?我正在測試畫布框架,我不想在其他站點提供Facebook身份驗證。 – mmutilva 2010-05-10 19:52:03

+0

這是爲了在您自己的網站上使用,您仍然需要通過Facebook進行身份驗證,創建會話並設置訪問權限。 – Gazler 2010-05-10 19:58:52

+0

此示例代碼仍然不適用於我。不僅登錄/註銷圖像不顯示,如果我在鏈接中放置文本,以便我可以看到它並單擊登錄,它會將我的iframe重定向到一個醜陋的轉到facebook.com鏈接。當我點擊_that_時,它允許我登錄,然後重定向到我的個人資料。這不會改變應用程序的狀態,當我回到它。仍然沒有會話。 – 2011-01-20 13:17:54

1

我不知道你是否已經得到了這個答案,但這是我通過自己的工作發現的。即使用戶登錄Facebook,第一次嘗試獲取會話時,它將爲空。我找到的示例應用程序在表單上放置了一個登錄按鈕 - 如果你這樣做了,那麼用戶將不得不單擊按鈕,即使他們已經登錄到facebook。他們不會再被提示,但它是一個額外的用戶操作。

所以在我的應用程序,並在其他幾個我在論壇裏找到了工作只是重定向到登錄URL。 (如果你檢查網址,其中一個參數是「return_session = 1」。)當它回來時,你將有一個會話,並可以正常進行。

但在我的應用程序,如果我沒有一個應用程序的令牌,然後我不能讓那屆要麼,所以我必須先得到一個應用程序的令牌。要獲得應用程序令牌,請查看http://forum.developers.facebook.com/viewtopic.php?id=56789(來自dynamoman 2010年5月7日發佈的帖子)(大約第四篇文章)的優秀說明。

有一件事我跑進的是,無論在哪裏,我告訴認證是「下一個」頁面,它關係到畫布中配置的頁面。因此,而不是三個單獨的頁面,我只有兩個,畫布回調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 

有可能是一個更好的辦法,我敢肯定,有人更熟悉這個比我能有一個更好的解決方案,或交的通用代碼。

+0

另一個注意事項 - 確保在構造facebook對象時使用應用程序ID,而不是應用程序密鑰。您需要授權網址中的密鑰,但需要facebook對象中的appid。 – 2010-06-18 16:26:11

+0

事實證明,會話有一個嵌入access_token,所以我認爲我不應該需要做整個授權的事情。但是我無法刪除它並仍然能夠正常工作 - 我可能錯過了一些基本的東西。 – 2010-06-18 19:32:42

3

http://forum.developers.facebook.net/viewtopic.php?pid=267627

它爲我檢查後#8。謝謝用戶「standardcombo」。

爲了您的方便,我在這裏複製了它。

  1. 轉到您的開發者頁面 「我的應用」
  2. 選擇您的應用和 '修改設置'
  3. 高級>遷移>啓用 「的OAuth 2.0帆布(測試版)」
+0

證實..這是爲我工作的唯一解決方案 – nyusternie 2011-03-04 19:53:58

相關問題