2012-07-05 240 views
0

我很难实现Facebook连接到我的codeigniter应用程序 - 我现在有一个问题。从这里下载PHP SDK之后:https://github.com/facebook/php-sdk/我将/ src文件复制到我的CI库中(就像所有人一样):)。Facebook连接和Codeigniter - HTTP/1.0 400错误请求

现在我想用这种方法来验证我的用户:

https://developers.facebook.com/docs/authentication/server-side/

我的控制器是在这里(我认为这个问题是在年底,但粘贴完整的文件,因为我不知道) :

<?php 

class Fb25 extends CI_Controller { 

function index() 
{ 
      $app_id  = "MY APP ID"; 
    $app_secret = "MY SECRET KEY"; 
    $site_url = "http://devlocalhost.com/"; 

$this->load->library('facebook',array('appId' => $app_id, 'secret' => $app_secret)); 

    $user = $this->facebook->getUser(); 
    $data['user'] = $user; 

if($user){ 

    $data['user_profile'] = $this->facebook->api('/me');  
    $user_profile = $data['user_profile']; 

} else{ 

    $user = NULL; 

}  

if($user){ 
// Get logout URL 
$data['logoutUrl'] = $this->facebook->getLogoutUrl(array(
    'next' => 'http://devlocalhost.com/', // URL to which to redirect the user after logging out 
    )); 
}else{ 
// Get login URL 
    $data['loginUrl'] = $this->facebook->getLoginUrl(array(
    'scope'  => 'email', // Permissions to request from the user 
    'redirect_uri' => 'http://devlocalhost.com/fb25/good', // URL to redirect the user to once the login/authorization process is complete. 
    )); 
} 

if($user){ 


// Save your method calls into an array 
$data['queries'] = array(
    array('method' => 'GET', 'relative_url' => '/'.$user), 
    array('method' => 'GET', 'relative_url' => '/'.$user.'/home?limit=50'), 
    array('method' => 'GET', 'relative_url' => '/'.$user.'/friends'), 
    array('method' => 'GET', 'relative_url' => '/'.$user.'/photos?limit=6'), 
    ); 

// POST your queries to the batch endpoint on the graph. 
    $batchResponse = $facebook->api('?batch='.json_encode($queries), 'POST'); 


//Return values are indexed in order of the original array, content is in ['body'] as a JSON 
//string. Decode for use as a PHP array. 
$data['user_info']  = json_decode($batchResponse[0]['body'], TRUE); 
$data['feed']   = json_decode($batchResponse[1]['body'], TRUE); 
$data['friends_list'] = json_decode($batchResponse[2]['body'], TRUE); 
$data['photos']   = json_decode($batchResponse[3]['body'], TRUE); 


    } 
    echo 'user_info: '.$data['user_info']; 
    print_r($data); 

    $this->load->view('fb25_view', $data); 
} 

public function good(){ 

$app_id  = "MY APP ID"; 
    $app_secret = "MY SECRET KEY"; 
    $site_url = "http://devlocalhost.com/"; 


echo '<br/><br/>SESJA:<br/>'; 
print_r($_SESSION); 
echo '<br/><br/>GET:<br/>'; 
print_r($_GET); 
echo '<br/><br/>REQUEST:<br/>'; 
print_r($_REQUEST); 
    echo '<br/><br/>najs<br/><br/>'; 




    session_start(); 

    $code = $_GET["code"]; 

    //@@@@@@IMO PROBLEM IS SOMEWHERE HERE: 


$token_url = "https://graph.facebook.com/oauth/access_token?" 
    . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url) 
    . "&client_secret=" . $app_secret . "&code=" . $code; 


$response = file_get_contents($token_url); 

$params = null; 
parse_str($response, $params); 

$graph_url = "https://graph.facebook.com/me?access_token=" 
    . $params['access_token']; 

$user = json_decode(file_get_contents($graph_url)); 
echo("Hello " . $user->name); 



} 
} 
?> 

我的问题是:我按下登录按钮并进入Facebook来验证自己,点击“去应用”,想回去到我的网站 - 它说:

Message: file_get_contents(https://graph.facebook.com/oauth/access_token? client_id=487560264592073&redirect_uri=http%3A%2F%2Fdevlocalhost.com%2F&client_secret=mysecretcode&code=AQAgnmNWwDub9yaRB6Vf73gg8Xvwo8KhIM077lB67_bu1Z3rAvyk3Ckl54qK7hh9o3VkG0rFIBTfRXwtrSBFVWEpqYfm1o7e5CQg3jVctq-EE1ZxWrgWrfesLpQ2oF3wlmEMb5o6ORobGmibT06kqe5f2N0ch4kSYBJ4SiTcdV-612fGOJHGcipeyU_GJJ0Jvsg) 

[function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request 

UPDATE

看这里,请:

我添加功能到你的控制器:

public function link(){ 
$fb_usr = $this->fb_connect->user; 
$firstname = $fb_usr['first_name']; 

$my_url="http://devlocal.pl"; 
echo 'user_id'; 


$token_url = "https://graph.facebook.com/oauth/access_token?" 
    . "client_id=myclientid&redirect_uri=" . urlencode($my_url) 
    . "&client_secret=mysecretcode&code=" . $_GET['code']; 

    redirect($token_url); 

}

为了让我的访问令牌 - 无需访问令牌我无法获取我的用户名.. 我使您的loginbyfacebook函数重定向接入链接功能。它不显示fb用户名。

当我重定向到我TOKEN_URL它显示:

{ 
"error": { 
    "message": "Error validating verification code.", 
    "type": "OAuthException", 
    "code": 100 
    } 
} 
+0

是否有对'ACCESS_TOKEN?'和'CLIENT_ID之间的空格='的要求? – 2012-07-05 15:36:32

+0

不,只是在这里 – pawel 2012-07-05 22:29:45

回答

1

遵循以下步骤:

1.Put the facebook php sdk in library folder 
2.create facebook config with app detail 
3.Inherit facebook class in fb_connect 
4.add these function in controller then it will work like charm. 

fb_connect.php

<?php 
include(APPPATH.'libraries/facebook/facebook.php'); 

class Fb_connect extends Facebook{ 

    //declare public variables 
    public $user  = NULL; 
    public $user_id = FALSE; 
    public $fb   = FALSE; 
    public $fbSession = FALSE; 
    public $appkey  = 0; 

    //constructor method. 
    public function __construct() 
    { 
       $CI = & get_instance(); 
       $CI->config->load("facebook",TRUE); 
       $config = $CI->config->item('facebook'); 
       parent::__construct($config);     
       $this->user_id = $this->getUser(); // New code 

       $me = null; 
       if ($this->user_id) { 
        try { 
         $me = $this->api('/me'); 
         $this->user = $me; 
         } catch (FacebookApiException $e) { 
          error_log($e); 
         } 
     } 
    } 

} // end class 

控制器功能

function loginByFacebook(){ 
     $this->load->library('fb_connect');  
$param = array(
      'scope' =>'email,user_location,user_birthday,offline_access', 
       'redirect_uri' => base_url() 
      ); 
     redirect($this->fb_connect->getLoginUrl($param)); 


} 

function facebook() { 

     if (!$this->fb_connect->user_id) {  

     } else {   
      $fb_uid = $this->fb_connect->user_id; 
      $fb_usr = $this->fb_connect->user; 
      $firstname = $fb_usr['first_name']; 
        } 
     } 

这样

$fb_usr = $this->fb_connect->user; 
$firstname = $fb_usr['first_name']; 
+0

你的意思是,在Facebook和Facebook的fb_connect中继承base_facebook? fb应该有哪些功能连接? – pawel 2012-07-08 19:11:21

+0

当我用我的facebook键配置文件时,我应该在哪里使用这些变量? – pawel 2012-07-08 20:30:42

+0

是的,你是正确的类层次结构.....和配置文件在fb_connect中使用。我已将fb_connect置于答案中。 – 2012-07-09 03:29:15