1

我正在尝试使用 facebook PHP SDK 从 facebook 获取用户数据。(我正在使用 codeigniter 开发网站)。我能够获取所有基本数据。但我无法获取那些需要访问令牌的数据。我通过解析 url 从注销 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。如果它可以工作,那么我怎样才能得到这个代码?

2012 年 4 月 18 日更新

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

4

3 回答 3

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;
}
}
于 2012-04-18T11:04:45.533 回答
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;
};

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

于 2012-04-17T14:36:05.900 回答
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>";

    }
}
}
于 2012-04-17T14:51:37.533 回答