【发布时间】:2013-04-04 18:31:35
【问题描述】:
基于facebook instructions (Scenario 4),我正在使用以下网址
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
获取新的访问令牌,但我得到以下信息:
{ “错误”:{ "message": "验证访问令牌时出错:会话已在 unix 时间 1365257820 过期。当前 unix 时间为 1365759029。", “类型”:“OAuthException”, “代码”: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= 之后并将其粘贴到 code= 下面的 URL 并在浏览器上运行下面的 URL
您将收到以下响应,这是 60 天的新访问令牌
access_token=<Extended_Access_Token>&expires=5180130
将 access_token= 后面的字符串复制并粘贴到服务器上的脚本中,该脚本会在您的页面上发布新帖子
【问题讨论】:
-
这意味着您的旧访问令牌已经过期。你能用一个新的短期访问令牌重试同样的事情吗
-
感谢我使用新的短期访问令牌完成了这项工作
-
你刚刚向世界写入了访问令牌,请小心。任何人都可以滥用它。
标签: facebook access-token facebook-access-token