如何获取需要访问令牌的所有Facebook数据?

时间:2012-04-17 14:27:39

标签: php codeigniter facebook-graph-api facebook-authentication

我正在尝试使用facebook PHP SDK从Facebook获取用户数据。 (我正在使用codeigniter开发网站)。我能够获取所有基本数据。但我无法获取那些需要访问令牌的数据。我通过解析url从logout url获取访问令牌参数。但那个令牌不起作用。它与主访问令牌不同吗?

这是我的控制器文件

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

class UserRegistration2 extends CI_Controller { 
function __construct() {
    parent::__construct(); 
    $this->load->model('test/Facebook_model');
}

function index() {

    $fb_data = $this->Facebook_model->get_data();

    if((!$fb_data['uid']) or (!$fb_data['me'])) {

        echo "<a href='" .$fb_data['loginUrl']. "'>Login</a>";
    }
    else {
        $params = null;
        parse_str($fb_data['logoutUrl'], $params);
        $graph_url = "https://graph.facebook.com/me/music?access_token=" . $params['access_token'];
        //$user = json_decode(file_get_contents($graph_url));

        echo "<pre>";

        print_r($fb_data);

        //print_r($user);
        //print_r($params);
        echo "</pre>";
        echo "<a href='" .$graph_url. "'>Get Music</a>";
        echo "<a href='" .$fb_data['logoutUrl']. "'>Logout</a>";

    }
}
}

这是我的模型文件

public function __construct() {
    parent::__construct();

    $config = array(
                    'appId'  => '130090207121542',
                    'secret' => 'xxxxxxxxxxxxxxxxxxxxx',
                    'fileUpload' => true
                    );

    $this->load->library('facebook/Facebook', $config);

}

function get_data() { 
    $user = $this->facebook->getUser();
    $profile = null;
    if($user)
    {
        try {
            $profile = $this->facebook->api('/me');
        } catch (FacebookApiException $e) {
            error_log($e);
            $user = null;
        }
    }

    $fb_data = array(
                    'me' => $profile,
                    'uid' => $user,
                    'loginUrl' => $this->facebook->getLoginUrl(
                        array(
                            'scope' => 'email,user_interests,user_birthday,publish_stream',
                            'redirect_uri' => 'http://herle.in/flutter/index.php/test/userRegistration2.html'
                        )
                    ),
                    'logoutUrl' => $this->facebook->getLogoutUrl(),
                );

    return $fb_data;
}
}

修改

找到此问题的链接 Using Facebook OAuth 2.0 - How do I fetch the access token?

但不知道它是否会起作用。因为我不知道如何将CODE(THE_CODE_YOU_GOT_FROM_THE_SERVER)传递给url。如果它能够工作那么我怎么能得到这个CODE?

更新18/04/12

现在正在运作。在范围内,我没有从用户那里获得“user_likes”的许可。

3 个答案:

答案 0 :(得分:0)

这是我请求用户和应用程序access_tokens的方式。  我将在此处编辑所需的任何其他答案。


$access_token = $_SESSION['fb_135669679827333_access_token']; 
// replace app id, gets user token

// gets app access token in the form of access_token=xxxxxxxxxxxxxx
$app_access_token = GetCH();
function GetCH(){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/oauth/access_token?client_id=135669679827333&client_secret=xxxxxxxxxxxxxxxxxxxx&grant_type=client_credentials");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
//curl_setopt($ch,CURLOPT_CONNECTTIMEOUT_MS,20000);
if(substr($url,0,8)=='https://'){
    // The following ensures SSL always works. A little detail:
    // SSL does two things at once:
    //  1. it encrypts communication
    //  2. it ensures the target party is who it claims to be.
    // In short, if the following code is allowed, CURL won't check if the 
    // certificate is known and valid, however, it still encrypts communication.
    curl_setopt($ch,CURLOPT_HTTPAUTH,CURLAUTH_ANY);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
}
$sendCH = curl_exec($ch);
curl_close($ch);
return $sendCH;
};

更多信息:要检索用户令牌,您必须至少拥有该用户的基本权限。

答案 1 :(得分:0)

    public function __construct() {
        parent::__construct();

        $config = array(
                        'appId'  => '130090207121542',
                        'secret' => 'xxxxxxxxxxxxxxxxxxxxx',
                        'fileUpload' => true
                        );

        $this->load->library('facebook/Facebook', $config);

    }
   $access_token = $_SESSION['fb_130090207121542_access_token'];
    function get_data() { 
        $user = $this->facebook->getUser();
        $profile = null;
        if($user)
        {
            try {
                $profile = $this->facebook->api('/me');

    // replace app id, gets user token 
            } catch (FacebookApiException $e) {
                error_log($e);
                $user = null;
            }
        }

        $fb_data = array(
                        'me' => $profile,
                        'uid' => $user,
                        'loginUrl' => $this->facebook->getLoginUrl(
                            array(
                                'scope' => 'email,user_interests,user_birthday,publish_stream',
                                'redirect_uri' => 'http://herle.in/flutter/index.php/test/userRegistration2.html'
                            )
                        ),
                        'logoutUrl' => $this->facebook->getLogoutUrl(),
                    );

        return $fb_data;
    }

    //
    // gets app access token in the form of access_token=xxxxxxxxxxxxxx
    $app_access_token = GetCH();
    function GetCH(){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/oauth/access_token?client_id=135669679827333&client_secret=xxxxxxxxxxxxxxxxxxxx&grant_type=client_credentials");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    //curl_setopt($ch,CURLOPT_CONNECTTIMEOUT_MS,20000);
    if(substr($url,0,8)=='https://'){
        // The following ensures SSL always works. A little detail:
        // SSL does two things at once:
        //  1. it encrypts communication
        //  2. it ensures the target party is who it claims to be.
        // In short, if the following code is allowed, CURL won't check if the 
        // certificate is known and valid, however, it still encrypts communication.
        curl_setopt($ch,CURLOPT_HTTPAUTH,CURLAUTH_ANY);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
    }
    $sendCH = curl_exec($ch);
    curl_close($ch);
    return $sendCH;
    };

class UserRegistration2 extends CI_Controller { 
function __construct() {
    parent::__construct(); 
    $this->load->model('test/Facebook_model');
}

function index() {

    $fb_data = $this->Facebook_model->get_data();

    if((!$fb_data['uid']) or (!$fb_data['me'])) {

        echo "<a href='" .$fb_data['loginUrl']. "'>Login</a>";
    }
    else {
        $params = null;
        parse_str($fb_data['logoutUrl'], $params);
        $graph_url = "https://graph.facebook.com/me/music?access_token=" . $access_token;
        //$user = json_decode(file_get_contents($graph_url));

        echo "<pre>";

        print_r($fb_data);

        //print_r($user);
        //print_r($params);
        echo "</pre>";
        echo "<a href='" .$graph_url. "'>Get Music</a>";
        echo "<a href='" .$fb_data['logoutUrl']. "?access_token=" .$access_token. "'>Logout</a>";

    }
}
}

答案 2 :(得分:0)

做出改变。它现在正在工作

这是我的控制器文件

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

class UserRegistration2 extends CI_Controller { 
function __construct() {
    parent::__construct(); 
    $this->load->model('test/Facebook_model');
}

function index() {

    $fb_data = $this->Facebook_model->get_data();

    if((!$fb_data['uid']) or (!$fb_data['me'])) {

        echo "<a href='" .$fb_data['loginUrl']. "'>Login</a>";
    }
    else {
        $params = null;
        parse_str($fb_data['logoutUrl'], $params);
        $graph_url = "https://graph.facebook.com/me/music?access_token=" . $params['access_token'];
        //$user = json_decode(file_get_contents($graph_url));

        echo "<pre>";

        print_r($fb_data);

        //print_r($user);
        //print_r($params);
        echo "</pre>";
        echo "<a href='" .$graph_url. "'>Get Music</a>";
        echo "<a href='" .$fb_data['logoutUrl']. "'>Logout</a>";

    }
}
}

这是我的模型文件

public function __construct() {
    parent::__construct();

    $config = array(
                    'appId'  => '130090207121542',
                    'secret' => 'xxxxxxxxxxxxxxxxxxxxx',
                    'fileUpload' => true
                    );

    $this->load->library('facebook/Facebook', $config);

}

function get_data() { 
    $user = $this->facebook->getUser();
    $profile = null;
    if($user)
    {
        try {
            $profile = $this->facebook->api('/me');
        } catch (FacebookApiException $e) {
            error_log($e);
            $user = null;
        }
    }

    $fb_data = array(
                    'me' => $profile,
                    'uid' => $user,
                    'loginUrl' => $this->facebook->getLoginUrl(
                        array(
                            'scope' => 'email,user_interests,user_birthday,publish_stream,user_likes',
                            'redirect_uri' => 'http://herle.in/flutter/index.php/test/userRegistration2.html'
                        )
                    ),
                    'logoutUrl' => $this->facebook->getLogoutUrl(),
                );

    return $fb_data;
}
}