代码之家  ›  专栏  ›  技术社区  ›  mmutilva

$facebook->getsession()在示例代码中返回空值。这样行吗?

  •  4
  • mmutilva  · 技术社区  · 16 年前

    运行 example code for the Facebook API 我得到一个空会话对象,我应该得到一个非空对象,在代码中给出注释。 我做错什么了?

    换句话说,在my 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位于同一文件夹中。

    3 回复  |  直到 15 年前
        1
  •  3
  •   Gazler    16 年前

    你链接你的应用程序了吗?

    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 ?>
    

    代码取自您提供的链接。

        2
  •  3
  •   Frank Young    15 年前

    请查看8号岗 http://forum.developers.facebook.net/viewtopic.php?pid=267627

    这对我很有用。感谢用户“StandardCombo”。

    为了您的方便,我把它抄在这里。

    1. 转到开发人员页面“我的应用程序”
    2. 选择应用程序并“编辑设置”
    3. 高级迁移启用“OAuth 2.0 for Canvas(beta)”。
        3
  •  1
  •   ALI SHAH    16 年前

    我不确定你是否已经得到了这个答案,但这是我从自己的工作中发现的。即使用户登录到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
    

    也许有更好的方法,我相信比我更熟悉这个的人会有更好的解决方案,或者发布通用代码。

    推荐文章