2017-04-06 118 views
1

這是我的控制器,它工作正常。Facebook使用Codeigniter登錄?

<?php 
defined('BASEPATH') OR exit('No direct script access allowed'); 

class Welcome extends CI_Controller { 


    public function __construct(){ 
     parent::__construct(); 
     $this->load->library('session'); 
    } 

    function index(){ 
     $this->load->view("welcome_message"); 
    } 

    function fblogin(){ 

    $fb = new Facebook\Facebook([ 
      'app_id' => '213234637467346734', 
      'app_secret' => '7346762476374673647', 
      'default_graph_version' => 'v2.8', 
     ]); 

    $helper = $fb->getRedirectLoginHelper(); 

    $permissions = ['email','user_location','user_birthday','publish_actions']; 
// For more permissions like user location etc you need to send your application for review 

    $loginUrl = $helper->getLoginUrl('http://localhost/facebooklogin/welcome/fbcallback', $permissions); 

    header("location: ".$loginUrl); 

} 
function fbcallback(){ 

     $fb = new Facebook\Facebook([ 
     'app_id' => '1767865763452676', 
     'app_secret' => '2454f5b88ea788b407297f39400b796b', 
     'default_graph_version' => 'v2.5', 
     ]); 

     $helper = $fb->getRedirectLoginHelper(); 
     $this->session->set_userdata('state',$_GET['state']); 

     try { 

      $accessToken = $helper->getAccessToken(); 

     }catch(Facebook\Exceptions\FacebookResponseException $e) { 
      // When Graph returns an error 
      echo 'Graph returned an error: ' . $e->getMessage(); 
      exit; 
     } catch(Facebook\Exceptions\FacebookSDKException $e) { 
      // When validation fails or other local issues 
      echo 'Facebook SDK returned an error: ' . $e->getMessage(); 
      exit; 
     } 


     try { 
      // Get the Facebook\GraphNodes\GraphUser object for the current user. 
      // If you provided a 'default_access_token', the '{access-token}' is optional. 
      $response = $fb->get('/me?fields=id,name,email,first_name,last_name,birthday,location,gender', $accessToken); 
     // print_r($response); 
     } catch(Facebook\Exceptions\FacebookResponseException $e) { 
      // When Graph returns an error 
      echo 'ERROR: Graph ' . $e->getMessage(); 
      exit; 
     } catch(Facebook\Exceptions\FacebookSDKException $e) { 
      // When validation fails or other local issues 
      echo 'ERROR: validation fails ' . $e->getMessage(); 
      exit; 
     } 

     // User Information Retrieval begins................................................ 
     $data['me'] = $response->getGraphUser(); 
     $data['accessToken'] =$accessToken; 

     $this->load->view("home",$data); 


    } 


} 

當我刷新主頁它給我這個錯誤 的Facebook SDK返回了一個錯誤:跨站請求僞造驗證失敗。持久數據中缺少必需的參數「狀態」。 在這裏,我改變了APP_ID和app_secret安全reason.and我還加載autload文件中的會話庫

回答

1

我有解決的疑難問題只是在try塊從哪裏獲得訪問令牌添加以下代碼

if(!$this->session->userdata('token')){ 
      $accessToken = $helper->getAccessToken(); 
      $this->session->set_userdata('token',$accessToken); 
      } 
      else{ 
      $accessToken = $this->session->userdata('token'); 
      }