基於facebook instructions (Scenario 4)我使用以下網址Facebook的訪問令牌過期
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
,以獲得新的訪問令牌,但我得到如下:
{「錯誤」:{ 「消息「:」驗證訪問令牌的錯誤:會話在unix時間已過期1365257820.當前unix時間爲1365759029.「, 」type「:」OAuthException「, 」code「:190, 」error_subcode「:463}}
不起作用。任何幫助讚賞。
編輯:明白了!工作原理是這樣
如果訪問令牌到期必須將其存儲在服務器上之後,瀏覽器
<?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首先運行下面的PHP腳本
https://graph.facebook.com/oauth/access_token?code= ...
然後得到的字符串(應該是這樣的:AQCn41Svv5DbWrnFY0Wf ..... YbNm_yz2rE#_) 碼=和粘貼的代碼URL =下方運行下面的網址上的瀏覽器
,你會得到下面的響應是60天
access_token=<Extended_Access_Token>&expires=5180130
複製一個新的訪問令牌並粘貼後的字符串access_token =到您的服務器上的腳本,在您的頁面上發佈新帖子
這意味着你的舊訪問令牌已過期。你可以用一個新的短期訪問令牌重試同樣的事情 –
謝謝我用新的短期訪問令牌做了它 – stefanosn
你剛剛寫訪問令牌給世界,請注意它。任何人都可以濫用它。 –