2011-04-14 48 views
6

我成功使用Facebook SDK(PHP)将用户连接到我的网站,但我在验证其帐户时遇到问题。他们的帐户已成功通过身份验证,但由于某种原因我的网站会话已被清除。Facebook API SDK(PHP)清除网站会话

流量:

  • 用户登录到我的网站(本地用户名和密码)
  • 用户连接到Facebook在弹出的
  • Facebook的身份验证的用户,并返回到我的网站
  • 我的网站会话现在无效(在弹出窗口和主窗口中)导致用户被注销

我正在使用Facebook SDK(PHP)和我的网站使用CakePHP框架

任何帮助将不胜感激。

+0

您是使用JavaScript SDK来显示弹出框还是手动执行操作? – pleasedontbelong 2011-04-14 08:47:04

+0

我正在手动做。我只是使用PHP SDK – bpneal 2011-04-14 09:07:41

回答

1

我找不到了为什么会话被重置,因此决定不使用SDK进行身份验证。这是我用来代替的。

$code = (isset ($_REQUEST['code']) ? $_REQUEST['code'] : null); 

if (empty ($code)) { 
    $dialogUrl = 'http://www.facebook.com/dialog/oauth?client_id=' . $this->appId . '&redirect_uri=' . urlencode($this->url) . '&scope=' . implode(',', $this->scope); 
    header('Location: ' . $dialogUrl); 
    die; 
} 
else { 
    $tokenUrl = 'https://graph.facebook.com/oauth/access_token?client_id=' . $this->appId . '&redirect_uri=' . urlencode($this->url) . '&client_secret=' . $this->secret . '&code=' . $code; 
    $accessToken = file_get_contents($tokenUrl); 

    $this->Session->write('facebookAccessToken', $accessToken); 

    $graphUrl = 'https://graph.facebook.com/me?' . $accessToken; 
    $fbUser = json_decode(file_get_contents($graphUrl)); 

    if ($fbUser) { 
     ... 
    } 
} 
1

可能值得检查Cake安全级别,它可能是做引用检查(我认为它在“高”设置,也许是“中等”),这会使会话无效。

+0

感谢Griff,这是我的第一个想法,但我已经尝试将关卡降低到“低”,仍然会遇到同样的问题。此外,我以相同的方式使用Twitter API,并且不会产生相同的错误。它必须是在Facebook SDK – bpneal 2011-04-14 09:03:55

2

我不能告诉你什么是删除您的会话,但你可能想尝试这(对我的作品)

使用JavaScript SDK显示登录按钮,将打开弹出连接到FB

的JS SDK添加到您的网页是这样的:

<div id="fb-root"></div> 
<script> 
    window.fbAsyncInit = function() { 
    FB.init({appId: '<?php echo FB_API_ID; ?>', status: true, cookie: true, xfbml: true}); 
    FB.Event.subscribe('auth.login', function() { 
     new Request({ 
      'method': 'get', 
      'url': '<?php echo $this->Html->url(array('controller'=>'users','action'=>'login_fb'));?>', 
      'onSuccess': function(result){ 
       window.location.reload();  
      } 
     }).send(); 
    }); 
    }; 
    (function() { 
    var e = document.createElement('script'); e.async = true; 
    e.src = document.location.protocol + 
     '//connect.facebook.net/en_US/all.js'; 
    document.getElementById('fb-root').appendChild(e); 
    }()); 
</script> 

auth.login事件我使用Ajax调用/用户/ login_fb将使用Facebook的SDK,检查了Facebook会话:

App::import('Lib', 'facebook_sdk/facebook'); 
    // "MyAuth" is a custom Auth Component that extends the normal Auth component 
    $this->MyAuth->facebook = new Facebook(array(
     'appId' => FB_API_ID, 
     'secret' => FB_SECRET, 
     'cookie' => true, 
    )); 

    $me = null; 
    $session = $this->MyAuth->facebook->getSession(); 
    if ($session) { 
     try { 
     $uid = $this->MyAuth->facebook->getUser(); 
     $me = $this->MyAuth->facebook->api('/me'); 
     } catch (FacebookApiException $e) { 
     error_log($e); 
     } 
    } 

    if ($me) { 
     $this->Session->write('FbLogin.session',$session); 
     $this->Session->write('FbLogin.user',$me); 
     $UserModel = ClassRegistry::init('User'); 
     $user = $UserModel->findByUid($me['id']); 
     if(!$user){ 
      $UserModel->create(); 
      $user_data = array('username'=>$me['username'], 
         'name'=>$me['first_name'], 
         'lastname'=>$me['last_name'], 
         'email'=>$me['email'], 
         'group_id'=>GROUP_VISITOR, 
         'uid'=>$me['id'] 
         ); 
      $UserModel->save($user_data); 
      $user['User']['id'] = $UserModel->id; 
     } 
     $this->Session->write($this->MyAuth->sessionKey, $user['User']); 
     $this->MyAuth->_loggedIn = true; 
     } 
} 

主要的想法是,在..我的js调用Ajax来检查FB会话,然后将其保存在蛋糕会话,JS会刷新页面

+0

谢谢,这看起来不错,但我已决定不再使用SDK进行验证 – bpneal 2011-04-14 11:53:51

+0

这是一个聪明的做法,看起来很酷kool – Herr 2011-05-10 10:37:08