Facebook访问令牌过期。

8

根据Facebook指南(场景4),我正在使用以下URL:

https://graph.facebook.com/oauth/access_token?client_id=APP_ID&client_secret=CLIENT_SECRET& grant_type=fb_exchange_token&fb_exchange_token=OLD_ACCESS_TOKEN

我想获取新的访问令牌,但是我得到了以下结果:
{ "error": { "message": "验证访问令牌时出错:会话已在Unix时间1365257820过期。当前Unix时间为1365759029。", "type": "OAuthException", "code": 190, "error_subcode": 463 } }
不起作用。任何帮助都将不胜感激。
编辑:我明白了! 这样可以运行
如果访问令牌过期,请先在服务器上存储它,然后在浏览器上运行以下PHP脚本
<?php
  $app_id = "your app id";
  $app_secret = "your app secret"; 
  $my_url = "http://apps.facebook.com/your_app_name";

  // known valid access token stored in a database 
  $access_token = "your old access token";

  $code = $_REQUEST["code"];

  // If we get a code, it means that we have re-authed the user 
  //and can get a valid access_token. 
  if (isset($code)) {
    $token_url="https://graph.facebook.com/oauth/access_token?client_id="
      . $app_id . "&redirect_uri=" . urlencode($my_url) 
      . "&client_secret=" . $app_secret 
      . "&code=" . $code . "&display=popup";
    $response = file_get_contents($token_url);
    $params = null;
    parse_str($response, $params);
    $access_token = $params['access_token'];
  }


  // Attempt to query the graph:
  $graph_url = "https://graph.facebook.com/me?"
    . "access_token=" . $access_token;
  $response = curl_get_file_contents($graph_url);
  $decoded_response = json_decode($response);

  //Check for errors 
  if ($decoded_response->error) {
  // check to see if this is an oAuth error:
    if ($decoded_response->error->type== "OAuthException") {
      // Retrieving a valid access token. 
      $dialog_url= "https://www.facebook.com/dialog/oauth?"
        . "client_id=" . $app_id 
        . "&redirect_uri=" . urlencode($my_url);
      echo("<script> top.location.href='" . $dialog_url 
      . "'</script>");
    }
    else {
      echo "other error has happened";
    }
  } 
  else {
  // success
    echo("success" . $decoded_response->name);
    echo($access_token);
  }

  // note this wrapper function exists in order to circumvent PHP’s 
  //strict obeying of HTTP error codes.  In this case, Facebook 
  //returns error code 400 which PHP obeys and wipes out 
  //the response.
  function curl_get_file_contents($URL) {
    $c = curl_init();
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($c, CURLOPT_URL, $URL);
    $contents = curl_exec($c);
    $err  = curl_getinfo($c,CURLINFO_HTTP_CODE);
    curl_close($c);
    if ($contents) return $contents;
    else return FALSE;
  }
?>

上面的脚本将在浏览器上给你一个像下面这样的URL:https://graph.facebook.com/oauth/access_token?code=...
然后获取字符串(应该类似于这样:AQCn41Svv5DbWrnFY0Wf.....YbNm_yz2rE#_),在下面的code = URL中粘贴它并在浏览器上运行下面的URL。

https://graph.facebook.com/oauth/access_token?client_id=App_Id&redirect_uri=http://apps.facebook.com/poemsoflove&client_secret=App_Secret&code=AQCn41Svv5DbWrnFY0Wf.....YbNm_yz2rE#_&display=popup

你会得到以下响应,这是一个新的60天访问令牌。
access_token=<Extended_Access_Token>&expires=5180130

将access_token=后的字符串复制并粘贴到位于您服务器上用于发布新帖的脚本中。

1
这意味着您的旧访问令牌已经过期。您能否使用新的短期访问令牌重试相同的操作? - Anvesh Saxena
谢谢,我已经用新的短期访问令牌完成了它。 - stefanosn
3
你刚刚将访问令牌公开了,请小心使用。任何人都可能会滥用它。 - Anvesh Saxena
2个回答

0
  1. 创建应用程序 Facebook Developer Page

然后可以使用php获取实时访问令牌

$app_id = '{Application Id}';
$app_secret = '{Application Secret}';
$access_token = "https://graph.facebook.com/oauth/access_token?client_id=$app_id&client_secret=$app_secret&grant_type=client_credentials";
$access_token = file_get_contents($access_token); // returns 'accesstoken=APP_TOKEN|APP_SECRET'
$access_token = str_replace('access_token=', '', $access_token);

-1
$app_id = 'APP_ID';
$app_secret = 'APP_SECRET';
$access_token_url = "https://graph.facebook.com/oauth/access_token?client_id=$app_id&client_secret=$app_secret&grant_type=client_credentials";
$access_token_data = file_get_contents($access_token_url);
$access_token_arr = json_decode($access_token_data);
$access_token = $access_token_arr->access_token;

6
请勿仅仅发布代码作为答案。请详细说明您的逻辑。 - Harsh Wardhan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接