2011-01-28 100 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