2011-01-28 101 views
0

这里是我一直在测试代码:Facebook的Javascript API第 - 有麻烦“连接”

<?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获取我的ID /密码,并在注册时指定的目录中运行上述脚本。

我在做什么错?

回答

0

我认为你正在使用旧的休息API获取访问令牌为什么你不使用图形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'])); 

我认为它prettey简单

1

你在这里混的事情,试图用一个App access_token访问用户信息!

请仔细阅读Authentication document,User login部分。我也会强烈建议使用Facebook PHP-SDK,其中图书馆将负责所有的认证和授权交换过程。

+1

我其次,Facebook的PHP更好! – Ravikiran 2011-01-28 15:46:49