【问题标题】:facebook connect: javascript logs session but php doesn'tfacebook 连接:javascript 记录会话但 php 没有
【发布时间】:2012-03-07 21:58:18
【问题描述】:

我有示例 facebook 登录实现,但刚刚意识到它停止工作:

 <?php
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => 'myappid',
  'secret' => 'mybigsecreet',
  '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();

$me = null;
// Session based API call.
if ($session) {
  try {
    $uid = $facebook->getUser();
    $me = $facebook->api('/me');

        $r = new registro_usuarios();
        $r->facebook($uid,$me['name'],'https://graph.facebook.com/'.$me['id'].'/picture');

        echo '----------------------------'.$me;
  } catch (FacebookApiException $e) {
    error_log($e);
    echo '----------------------------'.$e;
  }
}else echo 'nosession';
echo $session;

打印 nosesion 但是当我单击登录按钮 (facebook) 萤火虫日志:FB.login() called when user is already connected. 并且登录弹出窗口 (facebook) 不会打开。

我错过了什么?一年后facebook-api过时了吗??

【问题讨论】:

    标签: php facebook facebook-graph-api facebook-php-sdk facebook-login


    【解决方案1】:

    $facebook-&gt;getSession() 不再工作。这就是为什么它可能会从第一个 if 循环中退出。

    $session = $facebook->getSession();

    $session 始终为空。

    您可以通过$facebook-&gt;getUser() 检查seesion,如果它给出0,则没有活动会话,否则您将获得会话用户的facebook id。

    查看此链接也可能会获得更多信息:

    【讨论】:

    • 非常感谢。会尝试让你知道。他们不应该重新映射这个函数吗?
    • 您可以通过$facebook-&gt;getUser()查看当前会话。如果它给出 0 则没有活动会话。这就是不推荐使用 $facebook-&gt;getSession() 函数的原因。无需与另一个函数映射。
    • 您是否得到$facebook-&gt;getUser() 的结果?
    • 我改变了 if ($session) ---> if ($facebook->getUser()>0) 结果是一样的:(
    • 你用的是哪个php-sdk版本?
    【解决方案2】:

    您应该从 Github 下载最新的 SDK,地址为 https://github.com/facebook/php-sdk

    然后执行以下操作,将记录用户会话。

    require '../src/facebook.php';
    
    // Create our Application instance (replace this with your appId and secret).
    $facebook = new Facebook(array(
      'appId'  => '344617158898614',
      'secret' => '6dc8ac871858b34798bc2488200e503d',
    ));
    
    // Get User ID
    $user = $facebook->getUser();
    
    // We may or may not have this data based on whether the user is logged in.
    //
    // If we have a $user id here, it means we know the user is logged into
    // Facebook, but we don't know if the access token is valid. An access
    // token is invalid if the user logged out of Facebook.
    
    if ($user) {
      try {
        // Proceed knowing you have a logged in user who's authenticated.
        $user_profile = $facebook->api('/me');
      } catch (FacebookApiException $e) {
        error_log($e);
        $user = null;
      }
    }
    
    // Login or logout url will be needed depending on current user state.
    if ($user) {
      $logoutUrl = $facebook->getLogoutUrl();
    } else {
      $loginUrl = $facebook->getLoginUrl();
    }
    

    这取自示例代码。对我来说效果很好。

    【讨论】:

      【解决方案3】:

      自 10 月 1 日起,OAuth 2.0 身份验证是强制性的(请参阅此处的更改路线图:https://developers.facebook.com/roadmap/completed-changes/),您必须更改一些不再受支持的功能。

      您可以在此处查看新的 php 文档:https://developers.facebook.com/docs/reference/php/

      以及认证文档: https://developers.facebook.com/docs/authentication/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-04
        • 2012-05-10
        • 2016-06-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多