Javascript登录助手CodeIgniter-无法在服务器端检索用户



我正在使用CodeIgniter,我已经安装了Facebook SDK 4。我不得不在包含文件的情况下这样做,因为自动加载不起作用。我正在使用Javascript登录用户。

这是facebook.php(在库根目录中)。我有facebook在配置中自动加载。

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// Autoload the required files
require_once( APPPATH.'libraries/Facebook/FacebookSession.php' );
require_once( APPPATH.'libraries/Facebook/FacebookRequest.php' );
require_once( APPPATH.'libraries/Facebook/FacebookResponse.php' );
require_once( APPPATH.'libraries/Facebook/FacebookSDKException.php' );
require_once( APPPATH.'libraries/Facebook/FacebookRequestException.php' );
require_once( APPPATH.'libraries/Facebook/FacebookAuthorizationException.php' );
require_once( APPPATH.'libraries/Facebook/GraphObject.php' );
require_once( APPPATH.'libraries/Facebook/FacebookSignedRequestFromInputHelper.php' );
require_once( APPPATH.'libraries/Facebook/FacebookJavaScriptLoginHelper.php' );

use FacebookFacebookJavaScriptLoginHelper;
use FacebookFacebookRequest;
use FacebookFacebookSession;
class Facebook {
    var $ci;
    var $permissions;

  public function __construct() {
    $this->ci =& get_instance();
    $this->permissions = $this->ci->config->item('permissions', 'facebook');
    // Initialize the SDK
    $this->ci->config->load('facebook');
    FacebookSession::setDefaultApplication( $this->ci->config->item('api_id', 'facebook'), $this->ci->config->item('app_secret', 'facebook') );
    $helper = new FacebookJavaScriptLoginHelper(); //This row gives me a blank "white page" and no errror is given, nothing in log or whatsoever.
    try {
        $session = $helper->getSession();
    } catch(FacebookRequestException $ex) {
        // When Facebook returns an error
    } catch(Exception $ex) {
        // When validation fails or other local issues
    }
    if ($session !== null) {
        $request = new FacebookRequest( $session, 'GET', '/me' )->execute();
        $user = $request->getGraphObject()->asArray();
    }
    else {
        echo 'no user';
    }
 }


}

为什么我总是没有用户$会话总是空的,即使我登录了。我确信appid和secret是正确的。

更新

我确信appid和secret是正确的,但我错了。。。但现在我遇到了另一个问题。当我尝试创建FacebookJavascriptLoginHelper()时,会出现某种异常。我只收到一张空白页。这可能是什么?(当我没有登录时,它会显示网站正常。

我试着记录错误,但我的日志文件中没有任何错误。(我已经将其设置为在配置中记录所有erorrs)

我的猜测是没有找到所需的文件。我已尝试使用自动加载例程,如以下文档所示:http://www.benmarshall.me/facebook-sdk-php-v4-codeigniter/但我无法让它发挥作用(这些fb文件都不包括在内)。然后没有包含任何文件。

更新2我知道你真的应该使用Composer来正确地自动加载Facebook文件。

我试了一下:基于此。我只需要在composer.json中添加一个条目https://developers.facebook.com/docs/php/gettingstarted/4.0.0

当我有这个:(我从https://github.com/facebook/facebook-php-sdk-v4/archive/master.zip)

{
    "name": "facebook/php-sdk-v4",
    "description": "Facebook SDK for PHP",
    "keywords": ["facebook", "sdk"],
    "type": "library",
    "homepage": "https://github.com/facebook/facebook-php-sdk-v4",
    "license": "Facebook Platform",
    "authors": [
        {
            "name": "Facebook",
            "homepage": "https://github.com/facebook/facebook-php-sdk-v4/contributors"
        }
    ],
    "require": {
        "php": ">=5.4.0",
        "ext-mbstring": "*"
    },
    "require-dev": {
        "phpunit/phpunit": "~4.0",
        "mockery/mockery": "~0.8",
        "guzzlehttp/guzzle": "~4.0"
    },
    "suggest": {
        "guzzlehttp/guzzle": "Allows for implementation of the Guzzle HTTP client"
    },
    "autoload": {
        "psr-4": {
            "Facebook\": "src/Facebook/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Facebook\Tests\": "tests/"
        }
    },
    "extra": {
        "branch-alias": {
            "dev-master": "4.1.x-dev"
        }
    }
}

它可以在命令窗口(Windows)中输入composer install,我的库文件夹中的供应商文件夹已经创建,看起来是正确的。

BUt当我将"facebook/php-sdk-v4":"4.0.*"添加到Require部分时:

"require": {
        "php": ">=5.4.0",
        "ext-mbstring": "*",
        "facebook/php-sdk-v4" : "4.0.*"
    },

我收到这个错误(来自composer):在任何版本中都找不到请求包Facebook/php-sdk-v4。

更新3现在我已经从Composer下载了Facebook SDK,但仍然无法自动加载。我已经能够从composer下载facebooksdk进行此调整。

{
  "minimum-stability": "dev",
  "require" : {
    "facebook/php-sdk-v4" : "4.0.*"
  }
}

我已经放弃了自动加载,因为我无法让它以这种方式工作。但我现在开始工作了!!!!

我已经安装了Composer(有了WAMP,你需要更改openssl,使其在PHP文件夹的PHP ini中可用(有两个用于WAMP的PHP ini文件)

我在CI应用程序的库文件夹中创建了一个文件

composer.json
{
  "minimum-stability": "dev",
  "require" : {
    "facebook/php-sdk-v4" : "4.0.*"
  }
}

然后在命令提示符下安装composer。

我把src/facebook文件夹移到了libraries/fiacebook(所有的facebook类都来自SDK)。(https://www.webniraj.com/2014/06/07/facebook-api-setting-up-the-facebook-php-sdk-4-0-x/)

然后创建一个Facebook.php(在根库中),如下所示:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// Autoload the required files
require_once( 'Facebook/HttpClients/FacebookHttpable.php' );
require_once( 'Facebook/HttpClients/FacebookCurl.php' );
require_once( 'Facebook/HttpClients/FacebookCurlHttpClient.php' );
require_once( 'Facebook/Entities/AccessToken.php' );
require_once( 'Facebook/Entities/SignedRequest.php' );
require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookSignedRequestFromInputHelper.php' );
require_once( 'Facebook/FacebookJavaScriptLoginHelper.php' );
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookOtherException.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( 'Facebook/GraphObject.php' );
require_once( 'Facebook/GraphSessionInfo.php' );
use FacebookHttpClientsFacebookHttpable;
use FacebookHttpClientsFacebookCurl;
use FacebookHttpClientsFacebookCurlHttpClient;
use FacebookEntitiesAccessToken;
use FacebookEntitiesSignedRequest;
use FacebookFacebookSession;
use FacebookFacebookRedirectLoginHelper;
use FacebookFacebookJavaScriptLoginHelper;
use FacebookFacebookRequest;
use FacebookFacebookResponse;
use FacebookFacebookSDKException;
use FacebookFacebookRequestException;
use FacebookFacebookOtherException;
use FacebookFacebookAuthorizationException;
use FacebookGraphObject;
use FacebookGraphSessionInfo;
class Facebook {
    var $ci;
    var $permissions;

  public function __construct() {
    $this->ci =& get_instance();
    $this->permissions = $this->ci->config->item('permissions', 'facebook');
    // Initialize the SDK
    $this->ci->config->load('facebook');
    FacebookSession::setDefaultApplication( $this->ci->config->item('api_id', 'facebook'), $this->ci->config->item('app_secret', 'facebook') );
    //Get info based on javascript popup session
    $helper = new FacebookJavaScriptLoginHelper(); 
    //If token not already set, then try to fetch session from current FB Login!
    //
    if ( !$this->ci->session->userdata('fb_token') ) {
        try {
            $session = $helper->getSession();
        } catch(FacebookRequestException $ex) {
            // When Facebook returns an error
            log_message('INFO', 'FB ERROR' . print_r($ex,true));
        } catch(Exception $ex) {
            // When validation fails or other local issues
            log_message('INFO', 'FB VALIDATION FAILS' . print_r($ex,true));
        }    
    }
    else {
        //FB Token already set. Then fetch user information based on token!
         $fb_token = $this->ci->session->userdata('fb_token');         
         $session = new FacebookSession( $fb_token );        
    }    
    //Now session is set by current logged in session or by current token set
    //(Session is retrieved from FB)
    if (isset($session)) {            
            $this->ci->session->set_userdata( 'fb_token', $session->getToken() );             
            $request = new FacebookRequest( $session, 'GET', '/me' );
            $response = $request->execute();
            $user = $response->getGraphObject()->asArray();
    }
 }


}

我让它工作起来没有任何错误。你不能每次都尝试进行登录检查,因为FB会返回一个(恼人的)错误。因此,我在第一次登录后存储会话中收到的令牌。第二次(及以后)。

相关内容

  • 没有找到相关文章

最新更新