我有一个我想通过cron作业运行的脚本:
<?
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
答案 0 :(得分:3)
header("Location: $facebook->getLoginUrl(array("scope" => "publish_stream,manage_pages")));
以下代码对我有用:
<?php
include(dirname(__FILE__)."/fb/facebook.php");
define('PAGE_ID', '123456498798');
$facebook = new Facebook(array(
'appId' => '178645249555182',
'secret' => 'csdf64sd65f4sd6f54f1c',
));
$a = array(
'access_token' => 'werf564s6d1cr98f965d6gf49w8sd49f87w9ed5c16d5f49s8f74w9e8rf74',
'message' => 'This is the message',
'caption' => 'This is the caption',
'description' => 'This is the description'
);
print_r($facebook->api('/'.PAGE_ID.'/feed', 'post', $a));
答案 1 :(得分:2)
如果这是一个cron工作,那里没有人可以进行授权。你无法做你想做的事。你需要寻找替代方案。
另一种方法,例如从您请求扩展令牌的页面管理员中获取有效的身份验证令牌(offline_access正在快速弃用,请参阅https://developers.facebook.com/docs/offline-access-deprecation/)。
这个60天令牌需要放入cron作业可以访问的某个配置中。
您需要每两个月手动更新60天令牌。
您可以从https://developers.facebook.com/tools/explorer获得有效的访问权限。