Facebook Javascript API - 无法“连接”

时间:2011-01-28 06:53:02

标签: javascript api facebook

以下是我一直在测试的代码:

<?php
$accessToken = file_get_contents('https://graph.facebook.com/oauth/access_token?client_id=APP_ID&client_secret=APP_SECRET&grant_type=client_credentials');
$accessToken = explode('=', $accessToken);
$accessToken = $accessToken[1];
?>
<div id="fb-root"></div>

<script>
window.fbAsyncInit = function()
{
    FB.init({
        appId:  'APP_ID',
        status: true,
        cookie: true,
        xfbml:  true
    });

    FB.api('/me?access_token=<?php echo $accessToken; ?>', function(user)
    {
        if (user != null)
        {
            console.log(user);
        }
    });
};

(function()
{
    var e   = document.createElement('script');
    e.type  = 'text/javascript';
    e.src   = document.location.protocol + '//connect.facebook.net/en_GB/all.js';
    e.async = true;

    document.getElementById('fb-root').appendChild(e);
}());
</script>

我收到以下错误:

An active access token must be used to query information about the current user.

我在Facebook上注册了我的网站URL以获取我的ID / Secret,并在注册时指定的目录中运行上述脚本。

我做错了什么?

2 个答案:

答案 0 :(得分:1)

您正在混淆这里,尝试使用App access_token访问用户信息!

阅读Authentication documentUser login部分。此外,我 强烈 建议使用Facebook PHP-SDK,其中库将负责所有身份验证和授权交换过程。

答案 1 :(得分:0)

我认为你使用旧的rest api获取访问令牌为什么你不使用graph api获取访问令牌?

define('FACEBOOK_APP_ID', 'Your API ID');
define('FACEBOOK_SECRET', 'YOUR SECRET');
function get_facebook_cookie($app_id, $application_secret)
{
    $args = array();
    parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args);
    ksort($args);
    $payload = '';
    foreach ($args as $key => $value)
    {
        if ($key != 'sig')
        {
            $payload .= $key . '=' . $value;
        }
    }
    if (md5($payload . $application_secret) != $args['sig'])
    {
        return null;
    }
    return $args;
}
$cookie = get_facebook_cookie(FACEBOOK_APP_ID, FACEBOOK_SECRET);
$name=json_decode(file_get_contents('https://graph.facebook.com/me?access_token='.$cookie['access_token']));

我觉得它的简洁

相关问题