facebook访问令牌无效

时间:2015-10-04 15:34:30

标签: php codeigniter

我试图实施Facebook登录,但它不起作用。我得到一个登录Facebook的链接,成功登录后,我收到用户的电子邮件。但是当我在另一个标签上访问同一个网站时,我必须再次登录。

我在这里做错了什么?我怎样才能在此使用codeigniter会话库?因为它需要session_start()功能,但我可以使用会话库。

这是我的代码:

<?php 
    session_start();
    if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    require_once __DIR__.'/Facebook/autoload.php';
    class Social{
        const APP_ID = '123';
        const APP_SECRET = '1d1f';
        const APP_NAMESPACE = 'secret';
        const APP_DOMAIN = 'secrent.domain.net';
        const SECURE_CANVAS_URL = 'https://secrent.domain.net/';
        const DEFAULT_GRAPH_VERSION = 'v2.4';
        private $fb = null;
        private $permissions = ['email','publish_actions'];
        private $accessToken = null;
        private $helper = null;
        function __construct()
        {
            $CI =& get_instance();
            $this->fb = new Facebook\Facebook([
                'app_id'=>self::APP_ID,
                'app_secret'=>self::APP_SECRET,
                'default_graph_version'=>self::DEFAULT_GRAPH_VERSION,
            ]);
            $this->helper = $this->fb->getRedirectLoginHelper();
            try{
                if(isset($_SESSION['facebook_access_tokens'])){
                    $this->accessToken = $_SESSION['facebook_access_token'];
                }else{
                    $this->accessToken = $this->helper->getAccessToken();
                }
            }catch(Facebook\Exceptions\FacebookResponseException $e){
                log_message('error','Graph returned an error '.$e->getMessage());
            }catch(Facebook\Exceptions\FacebookSDKException $e){
                log_message('error','Graph returned an error '.$e->getMessage());
            }
        }
        function register()
        {
            $return_array = null;
            if(isset($this->accessToken))
            {
                if(isset($_SESSION['facebook_access_token'])){
                   $this->fb->setDefaultAccessToken();
                }else{
                    $_SESSION['facebook_access_token'] = (string) $this->accessToken;
                    $oAuth2Client = $this->fb->getOAuth2Client();
                    $longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);
                    $_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;
                    $this->fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
                }
                try{
                    $profile_request = $this->fb->get('/me?fields=email');
                    $profile = $profile_request->getGraphNode()->asArray();
                }catch(Facebook\Exceptions\FacebookResponseException $e){
                    log_message('error','Graph returned an error :'.$e->getMessage());
                }catch(Facebook\Exceptions\FacebookSDKException $e){
                    log_message('error','Facebook SKD retunred an error :'.$e->getMessage());
                }
                return $return_array = array(
                    'code'=>200,
                    'message'=>$profile,
                );
            }
            else
            {
                return $return_array = array(
                    'code'=>404,
                    'message'=>$this->helper->getLoginUrl(self::SECURE_CANVAS_URL,$this->permissions),
                );
            }
        }
    }
?>

0 个答案:

没有答案