我有一個劇本,我想通過一個cron作業運行:Facebook的API離線訪問
<?
require_once 'fb_access.php';
$user = $facebook->getUser();
if ($user) {
try {
$page_id = '********';
$page_info = $facebook->api("/$page_id?fields=access_token");
if(!empty($page_info['access_token'])) {
$args = array(
'access_token' => $page_info['access_token'],
'message' => 'This is the message',
'link' => 'http://thisisthelink',
'caption' => 'This is the caption',
'description' => 'This is the description',
);
$post_id = $facebook->api("/$page_id/feed","post",$args);
}
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
?>
然而,當我嘗試通過cron作業來運行它,它不工作。我發現這與offline_access令牌有關。但從我讀到的,用戶必須手動登錄才能獲取令牌。因爲我想通過cron工作來運行它,所以這是不可行的。有任何想法嗎?
更新:
我試過了;這是完全錯誤的嗎?
require_once 'fb_access.php';
$user = $facebook->getUser();
$token = $facebook->getAccessToken();
if ($user) {
try {
$page_id = '**********';
$args = array(
'access_token' => $token,
'message' => 'This is the message',
'link' => 'http://thisisthelink',
'caption' => 'This is the caption',
'description' => 'This is the description',
);
$post_id = $facebook->api("/$page_id/feed","post",$args);
}
catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
更新#2:
<?php
require_once 'fb_access.php';
$user = $facebook->getUser();
$token = '*******LONG_ACCESS_TOKEN*******';
$page_id = '**********';
$args = array(
'access_token' => $token,
'message' => 'This is the message',
'link' => 'http://www.thisisthelink.com',
'caption' => 'This is the caption',
'description' => 'This is the description',
);
$post_id = $facebook->api("/$page_id/feed","post",$args);
?>
使用此我得到:致命錯誤:Uncaught OAuthException: (#200) The user hasn't authorized the application to perform this action thrown in /base_facebook.php on line 1106
等等......現在,即使我手動啓動腳本,這也不起作用。幫幫我!這令人沮喪。 – Andrew 2012-02-20 21:19:17
你的updatE:是的,這是錯誤的。您需要先擁有訪問令牌(登錄用戶)。看看這個頁面,https://developers.facebook。com/tools/explorer /'和「獲取訪問令牌」,並具有manage_pages權限。獲得後,輸入'/ accounts /'到地址欄(在頁面中)並按提交。你應該看到你的頁面和訪問令牌。將'$ facebook-> getAccessToken()'從您的代碼更改爲該訪問令牌,停止嘗試檢查'$ user'並重試。 – 2012-02-20 23:05:52
嗯...試圖做到這一點,但我發現: '{ 「錯誤」:{ 「消息」: 「(#210)除必須的頁面。」, 「類型」:「OAuthException 「, 」code「:210 } }' – Andrew 2012-02-21 05:38:33