2010-05-10 96 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