我有一个剧本,我想通过一个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