因此,我一直在努力使这项工作发挥作用。我一直在查看现有问题的堆栈溢出并发现:Facebook: Post on Page as Page issue (access token / php)但它似乎仍然不能解决我的问题。
我每天都在尝试使用 cron 作业发布到我的 Facebook 页面。我遇到了身份验证问题。这是我的代码:
//Post to Facebook
//Variables
$app_id = "...";
$app_secret = "...";
$page_id = "...";
$my_url = "http://.../";
$access_token = "taken from page access token (developers.facebook.com/tools/explorer/)";
//Create the facebook object
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
//Get the access token using Facebook Graph API
//This gives permission to post to the wall
$token_url="https://graph.facebook.com/oauth/access_token?client_id=" . $app_id
. "&client_secret=" . $app_secret
. "&code=" . $access_token
. "&redirect_uri=" . $my_url;
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$user_access_token = $params['access_token'];
//Get a list of pages and loop through
//If one matches one in the variables above, that's the one
//We're posting to
$attachment_1 = array(
'access_token' => $user_access_token
);
$result = $facebook->api("/me/accounts", $attachment_1);
foreach($result["data"] as $page) {
if($page["id"] == $page_id) {
$page_access_token = $page["access_token"];
break;
}
}
//Write to the Page wall
try {
$attachment = array(
'access_token' => $page_access_token,
'message'=> "Hello World"
);
$result = $facebook->api('/me/feed','POST', $attachment);
} catch(Exception $e) {
//Send me an email
...
mail($to, $subject, $message, $headers);
}
如果我在浏览器中访问脚本(我假设它正在使用我的 facebook 会话),则此方法有效,但当它由 cron 作业触发时则无效。
我真的很感激任何帮助。谢谢。