5

我試圖運行Facebook的示例代碼連接,我從網上下載http://thinkdiff.net/facebook/php-sdk-3-0-graph-api-base-facebook-connect-tutorial/
我得到這個錯誤消息的信息:OAuthException:激活的訪問令牌必須用於查詢當前用戶

[error] => Array 
      (
       [message] => An active access token must be used to query information about the current user. 
       [type] => OAuthException 
       [code] => 2500 
      ) 

您可以嘗試在我的網站:http://facebook.oliverjordan.net/thinkdiff

這裏是fbmain.php代碼:

<?php 

//facebook application 
$fbconfig['appid' ]  = "463007877113xxx"; 
$fbconfig['secret']  = "-sensor-"; 
$fbconfig['baseurl'] = "http://facebook.oliverjordan.net/thinkdiff/index.php"; //"http://thinkdiff.net/demo/newfbconnect1/php/sdk3/index.php"; 

// 
if (isset($_GET['request_ids'])){ 
    //user comes from invitation 
    //track them if you need 
} 

$user   = null; //facebook user uid 
try{ 
    include_once "facebook.php"; 
} 
catch(Exception $o){ 
    error_log($o); 
} 
// Create our Application instance. 
$facebook = new Facebook(array(
    'appId' => $fbconfig['appid'], 
    'secret' => $fbconfig['secret'], 
    'cookie' => true, 
)); 

//Facebook Authentication part 
$user  = $facebook->getUser(); 
// We may or may not have this data based 
// on whether the user is logged in. 
// If we have a $user id here, it means we know 
// the user is logged into 
// Facebook, but we don’t know if the access token is valid. An access 
// token is invalid if the user logged out of Facebook. 


$loginUrl = $facebook->getLoginUrl(
     array(
      'scope'   => 'email,offline_access,publish_stream,user_birthday,user_location,user_work_history,user_about_me,user_hometown', 
      'redirect_uri' => $fbconfig['baseurl'] 
     ) 
); 

$logoutUrl = $facebook->getLogoutUrl(); 


if ($user) { 
    try { 
    // Proceed knowing you have a logged in user who's authenticated. 
    $user_profile = $facebook->api('/me'); 
    } catch (FacebookApiException $e) { 
    //you should use error_log($e); instead of printing the info on browser 
    d($e); // d is a debug function defined at the end of this file 
    $user = null; 
    } 
} 


//if user is logged in and session is valid. 
if ($user){ 
    //get user basic description 
    $userInfo   = $facebook->api("/$user"); 

    //Retriving movies those are user like using graph api 
    try{ 
     $movies = $facebook->api("/$user/movies"); 
    } 
    catch(Exception $o){ 
     d($o); 
    } 

    //update user's status using graph api 
    //http://developers.facebook.com/docs/reference/dialogs/feed/ 
    if (isset($_GET['publish'])){ 
     try { 
      $publishStream = $facebook->api("/$user/feed", 'post', array(
       'message' => "I love thinkdiff.net for facebook app development tutorials. :)", 
       'link' => 'http://ithinkdiff.net', 
       'picture' => 'http://thinkdiff.net/ithinkdiff.png', 
       'name' => 'iOS Apps & Games', 
       'description'=> 'Checkout iOS apps and games from iThinkdiff.net. I found some of them are just awesome!' 
       ) 
      ); 
      //as $_GET['publish'] is set so remove it by redirecting user to the base url 
     } catch (FacebookApiException $e) { 
      d($e); 
     } 
     $redirectUrl  = $fbconfig['baseurl'] . '/index.php?success=1'; 
     header("Location: $redirectUrl"); 
    } 

    //update user's status using graph api 
    //http://developers.facebook.com/docs/reference/dialogs/feed/ 
    if (isset($_POST['tt'])){ 
     try { 
      $statusUpdate = $facebook->api("/$user/feed", 'post', array('message'=> $_POST['tt'])); 
     } catch (FacebookApiException $e) { 
      d($e); 
     } 
    } 

    //fql query example using legacy method call and passing parameter 
    try{ 
     $fql = "select name, hometown_location, sex, pic_square from user where uid=" . $user; 
     $param = array(
      'method' => 'fql.query', 
      'query'  => $fql, 
      'callback' => '' 
     ); 
     $fqlResult = $facebook->api($param); 
    } 
    catch(Exception $o){ 
     d($o); 
    } 
} 

function d($d){ 
    echo '<pre>'; 
    print_r($d); 
    echo '</pre>'; 
}?> 

任何人都可以幫我嗎?

+0

可能存在[必須使用活動訪問令牌來查詢有關當前用戶信息的信息 - Graph api異常](http://stackoverflow.com/questions/11776234/an-active-access-token-must-be -used-to-query-about-the-current-user) –

+2

仍然沒有得到答案 –

回答

4

您必須確保您的訪問令牌處於活動狀態。或者,您可能處於註銷狀態。或嘗試清潔Cookie和緩存從瀏覽器(ctrl+shift+del)

+0

用戶是否要清理它的緩存? –

2

請檢查您是否越來越像

的主動訪問令牌的任何Facebook的異常必須被用來查詢有關 當前用戶的信息。

這導致0作爲用戶ID從$facebook->getUser()的回報。

所以請檢查getAccessTokenFromCode()base_facebook.php並驗證access_token_response格式,因爲它可能是JSON格式。

所以嘗試使用適當的解碼方法從$access_token_response得到$response_params['access_token']

相關問題