我完全沮喪與我的第一個Facebook應用程序項目。我遇到了一些似乎是一件簡單任務的重大問題。Facebook:在頁面發佈頁面問題(訪問令牌/ PHP)
我想在我的服務器上設置一個cron作業(簡單),它將在Facebook頁面(不是我的個人檔案)上張貼一些聰明的東西,並且它應該作爲頁面發佈。我將自己編入一個角落,現在完全混淆了......請任何人幫忙嗎? 「OAuthException:錯誤校驗應用程序」
我一直通幾乎每一個錯誤信息,我現在被困在
這是我現在有:
的第一個PHP - >獲取新的訪問令牌爲我的頁面訪問。這部分似乎工作正常,因爲我得到下一個頁面調用並接收新的訪問令牌。
<?php
require_once 'facebook.php';
$app_id = "1234....";
$app_secret = "5678....";
$my_url = "http://.../next.php";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
// known valid access token stored in a database
$access_token = "abc....";
$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);
}
function curl_get_file_contents($URL) {
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
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;
}
?>
Next php - >閱讀訪問令牌併發布在牆上。我從查詢字符串中獲取訪問令牌,但爲什麼會收到錯誤消息。我的ID,祕密和頁面ID都是爲了...
<?php
require_once 'facebook.php';
$app_id = "1234....";
$app_secret = "5678....";
$page_id = "0909....";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
$access_token = $_GET['code'];
echo($access_token);
try {
$attachment = array(
'access_token' => $access_token,
'message'=> "Hello World"
);
$result = $facebook->api('/'.$page_id.'/feed','POST',
$attachment);
var_dump($result);
} catch(Exception $e) {
echo $e;
}
?>
我敢肯定有一個簡單的方法,這樣做....和方式其實就是使它的工作! 任何幫助表示讚賞!
我跟着這個腳本 Update facebook page status from that page itself 這 https://developers.facebook.com/blog/post/500
謝謝。
我在你的代碼不清楚(不是PHP dev的我自己)爲你在哪裏提取的頁面訪問令牌就像http://stackoverflow.com/questions/4309154/update-facebook- page-status-from-that-page-itself-graph-api-php。 – DMCS 2012-01-30 20:31:51
我在第一個腳本中獲取訪問令牌,因爲當我使用從鏈接的示例中獲取的訪問令牌時,令牌限制爲2小時。第一個腳本檢查舊的訪問令牌 - >意識到它是舊的 - >並返回給我一個新的查詢字符串... – Peter1178373 2012-02-01 13:09:06