【问题标题】:How to fetch all facebook data which needs access token?如何获取所有需要访问令牌的 Facebook 数据?
【发布时间】:2012-05-14 00:48:12
【问题描述】:

我正在尝试使用 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”的许可。

【问题讨论】:

  • 您在哪里获取页面、用户和应用程序访问令牌?
  • 这就是我要问的。如何获取访问令牌以将其传递给“graph_url”变量?如果这是错误的方式,那么请给我一个替代解决方案。
  • 1.以供将来参考,请不要公开您的应用程序秘密,这很危险并且违反 Facebook TOS
  • 2.让我在添加了令牌的新答案中重新发布您的代码。
  • 谢谢。我忘记抹掉了。再次感谢。

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


【解决方案1】:

这就是我请求用户和应用程序 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;
};

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

【讨论】:

  • 感谢您的回复。当我将这个 url graph.facebook.com/oauth/… 放在浏览器中时,它显示“access_token=130090207121542|xm3P_L6SrAMVY3-DZRGbl9E_o6k”。这是我可以将它传递给“graph url”变量的令牌吗?实际上我想获取像这个 url graph.facebook.com/me/… 这样的数据,然后我会对其进行解码并将其放入我的数据库中。
  • 如果答案有效,请务必将答案标记为正确,以便其他用户找到解决方案。
【解决方案2】:

进行了更改。它现在正在工作

这是我的控制器文件

<?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;
}
}

【讨论】:

    【解决方案3】:
        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>";
    
        }
    }
    }
    

    【讨论】:

    • 谢谢兄弟。我不知道我们也可以通过这种方式获取访问令牌($access_token = $_SESSION['fb_130090207121542_access_token']; )。我会检查的。
    • 它不工作。该变量返回的标记与我从注销 url 中提取的标记相同。每当我将它放在“graph_url”变量中时,它都没有任何数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多