0
    private $_api_key;
    private $_api_secret;
    private $_token_url     = 'oauth/access_token';
    private $_user_url      = 'me';
    private $_data          = array();

    function __construct()
    {
        $this->_obj =$CI =& get_instance();

        $fb_api_id     = $CI->db->get_where('settings', array('code' => 'SITE_FB_API_ID'))->row()->string_value;
        $fb_api_secret = $CI->db->get_where('settings', array('code' => 'SITE_FB_API_SECRET'))->row()->string_value;

        $this->_api_key     = $fb_api_id;
        $this->_api_secret  = $fb_api_secret;

        $this->_token_url   = $this->_obj->config->item('facebook_api_url').$this->_token_url;
        $this->_user_url    = $this->_obj->config->item('facebook_api_url').$this->_user_url;

        $this->_set('scope', $this->_obj->config->item('facebook_default_scope'));

        $this->connection = new facebookConnection();

        if ( !$this->logged_in() )
        {
             // Initializes the callback to this page URL.
            $this->set_callback();
        }

    }

    public function logged_in()
    {
        return ( $this->get() === NULL ) ? FALSE : TRUE;
    }

    public function logout()
    {
        $this->_unset('token');
        $this->_unset('user');
    }

    public function login_url($scope = NULL)
    {
        $url = "http://www.facebook.com/dialog/oauth?client_id=".$this->_api_key.'&redirect_uri='.urlencode($this->_get('callback'));

        if ( empty($scope) )
        {
            $scope = $this->_get('scope');
        }
        else
        {
            $this->_set('scope', $scope);
        }

        if ( !empty($scope) )
        {
            $url .= '&scope='.$scope;
        }

        return $url;
    }

    public function login($scope = NULL)
    {
        $this->logout();

        if ( !$this->_get('callback') ) $this->_set('callback', current_url());

        $url = $this->login_url($scope);

        return redirect($url);
    }

    public function get()
    {
        $token = $this->_find_token();
        if ( empty($token) ) return NULL;

        // $user = $this->_get('user');
        // if ( !empty($user) ) return $user;

        try 
        {
            $user = $this->connection->get($this->_user_url.'?'.$this->_token_string());
        }
        catch ( facebookException $e )
        {
            $this->logout();
            return NULL;
        }

        // $this->_set('user', $user);
        return $user;
    }

    private function _find_token()
    {
        $token = $this->_get('token');

        if ( !empty($token) )
        {
            if ( !empty($token->expires) && intval($token->expires) >= time() )
            {
                // Problem, token is expired!
                return $this->logout();
            }

            $this->_set('token', $token);
            return $this->_token_string();
        }

        if ( !isset($_GET['code']) )
        {
            return $this->logout();
        }

        if ( !$this->_get('callback') ) $this->_set('callback', current_url());

        $token_url = $this->_token_url.'?client_id='.$this->_api_key."&client_secret=".$this->_api_secret."&code=".$_GET['code'].'&redirect_uri='.urlencode($this->_get('callback'));
        try 
        {
            $token = $this->connection->get($token_url);
        }
        catch ( facebookException $e )
        {
            $this->logout();
            redirect($this->_strip_query());
            return NULL;
        }

        $this->_unset('callback');
        if ( $token->access_token )
        {
            if ( !empty($token->expires) )
            {
                $token->expires = strval(time() + intval($token->expires));
            }

            $this->_set('token', $token);
            redirect($this->_strip_query());
        }

        return $this->_token_string();
    }

    private function _get($key)
    {
        $key = '_facebook_'.$key;
        return $this->_obj->session->userdata($key);
    }

    private function _set($key, $data)
    {
        $key = '_facebook_'.$key;
        $this->_obj->session->set_userdata($key, $data);
    }

    private function _unset($key)
    {
        $key = '_facebook_'.$key;
        $this->_obj->session->unset_userdata($key);
    }

    public function set_callback($url = NULL)
    {
        $this->_set('callback', $this->_strip_query($url));
    }

    private function _token_string()
    {
        return 'access_token='.$this->_get('token')->access_token;
    }

    public function append_token($url)
    {
        if ( $this->_get('token') ) $url .= '?'.$this->_token_string();

        return $url;
    }

    private function _strip_query($url = NULL)
    {
        if ( $url === NULL )
        {
            $url = ( empty($_SERVER['HTTPS']) ) ? 'http' : 'https';
            $url .= '://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        }

        $parts = explode('?', $url);
        return $parts[0];
    }

请告诉我当用户尝试在站点中使用 fb 帐户登录时,从 facebook 检索到的所有内容作为令牌。

我在此代码中收到以下错误:

消息:试图获取非对象的属性

文件名:库/Facebook_Lib.php

行号:454

遇到 PHP 错误

严重性:通知

消息:试图获取非对象的属性

文件名:库/Facebook_Lib.php

行号:493

严重性:通知

消息:试图获取非对象的属性

文件名:库/Facebook_Lib.ph

行号:493

有关 access_token 的所有错误

4

1 回答 1

0

由于 facebook 应用程序未上线,因此未创建会话,这是一个数据库错误。

于 2012-05-11T22:22:19.823 回答