我终于有一个工作脚本远程提交Facebook事件,并已完成解决将我的网站事件RSS提要转换为FB事件数据的问题。利用RSS2HTML,我添加了一个基于模板的调用,在事件发生前两天发送每个事件。这是代码:
// Post Today's Game
if (strstr($template, "~~~TwitterToday~~~"))
{
//Build Arrays for games (when there are more than one per day...
$name = array( 'name' );
$desc = array( 'description' );
$venue = array( 'location' );
$s_time = array( 'start_time' );
$e_time = array( 'end_time' );
$pic = array( 'picture' );
$priv = array( 'privacy' );
//Build Main Facebook Array for All games to draw from
$fbook = array(
$name,
$desc,
$venue,
$s_time,
$e_time,
$pic,
$priv,
);
$template = str_replace("~~~TwitterToday~~~", "", $template);
$mycount = 1;
for ($y = 1; $y < count($rss_parser->Items)+1; $y++) //come back through events
{
//find each event's information to look for today's
$gamedate = date('n/j/Y', $rss_parser->Items[$y]->pubDate_t);
$todaysdate = date('n/j/Y');
$tomorrowsdate = date('n/j/Y',mktime(0,0,0,date('m'), date('d')+1, date('Y')));
$gametime = date('Y-m-d H:i:s',$rss_parser->Items[$y]->pubDate_t);
$title = $rss_parser->Items[$y]->title;
$description = $rss_parser->Items[$y]->description;
if ($gamedate == $tomorrowsdate) //found it
{
$mycount++;
//Fill the arrays
$name[] = $title;
$desc[] = $description;
$venue[] = "Home";
$s_time[] = $gametime;
$e_time[] = "";
$pic[] = "";
$priv[] = "OPEN";
}
} // end $y loop
//Populate Main Facebook Array
$fbook[0] = $name;
$fbook[1] = $desc;
$fbook[2] = $venue;
$fbook[3] = $s_time;
$fbook[4] = $e_time;
$fbook[5] = $pic;
$fbook[6] = $priv;
// Let's run with it
if (strpos($title,"Special Event") === false)
{
$page_id = "xxxxxxxxxxxxxx"; //First Page Id
}
else
{
$page_id = "xxxxxxxxxxxxxxxxxxx"; //Special Event Page Id
}
$app_id = "xxxxxxxxxxxxx";
$app_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";
$my_url = "http://mydomain.com/feeds/rss2html.php"; // URL to THIS script
//Going to get the PAGE access code
//First to get USER Access Code
session_start();
$code = $_REQUEST["code"];
if (empty($code))
{
$_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
$dialog_url = "https://www.facebook.com/dialog/oauth?client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url) . "&state=" . $_SESSION['state'] . "&scope=create_event&scope=manage_pages";
echo("<script> top.location.href='" . $dialog_url . "'</script>");
}
if ($_REQUEST['state'] == $_SESSION['state'])
{
$token_url = "https://graph.facebook.com/oauth/access_token?" . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url) . "&client_secret=" . $app_secret . "&code=" . $code;
$access_token = @file_get_contents($token_url);
$params = null;
parse_str($access_token, $params);
$graph_url = "https://graph.facebook.com/me?access_token=" . $params['access_token'];
$user = json_decode(file_get_contents($graph_url));
}
else
{
echo("The state does not match. You may be a victim of CSRF.");
}
//Now, getting the PAGE Access token, using the user access token
$page_token_url = "https://graph.facebook.com/" . $page_id . "?fields=access_token&" . $access_token;
$response = file_get_contents($page_token_url);
// Parse the return value and get the Page access token
$resp_obj = json_decode($response,true);
$page_access_token = $resp_obj['access_token'];
for ($s = 1; $s < $mycount+1; $s++)
{
//Let's go post it up!
$url = "https://graph.facebook.com/" . $page_id . "/events?access_token=" . $page_access_token;
$params = array();
// Prepare Event fields
$params = array(
'name' => $fbook[0][$s],
'description' => $fbook[1][$s],
'location' => $fbook[2][$s],
'start_time' => $fbook[3][$s],
// 'end_time' => $fbook[4][$s], //These need to be excluded if they are empty
// 'picture' => $fbook[5][$s],
'privacy' => $fbook[6][$s],
);
// Start the Graph API call
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$result = curl_exec($ch);
$decoded = json_decode($result, true);
curl_close($ch);
if (is_array($decoded) && isset($decoded['id']))
{
$msg = "Event created successfully: {$decoded['id']}";
}
echo '<hr />' . $msg;
}
/* End FaceBook Code */
}
当我从浏览器中调用它时,此脚本可以创建奇迹,但是当从cron作业调用它时,我在rss2html脚本中出现“无法打开模板”错误。在过去,我总是能够通过为cron作业创建一个单独的脚本来解决这个问题,主要是使用cURL调用feed,它可以创建奇迹。
不幸的是,这种技术不适用于FaceBook Auth脚本,因为它返回“状态不匹配。您可能是CSRF的受害者。”
所以,我在一块岩石和一块坚硬的地方之间。如果没有cURL调用,则无法运行rss2html脚本,并且cURL调用会阻止Facebook登录。 Here's文本版本的rss2html脚本,如果有人想看到它。
任何人都可以通过某种方式想到一个好的解决方法吗?
感谢DCMS,解决方案如此: 在https://developers.facebook.com/docs/authentication/使用Facebook的身份验证文档并在我的通话中添加'&amp; scope = offline_access',我能够获取一些离线访问令牌,并因此改变了我的上述代码:
//Going to get the PAGE access code
//First to get USER Access Code
session_start();
for ($s = 1; $s < $mycount+1; $s++)
{
//Let's go post it up!
$url = "https://graph.facebook.com/" . $page_id . "/events?access_token=" . $page_access_token;
$params = array();
// Prepare Event fields
$params = array(
'name' => $fbook[0][$s],
'description' => $fbook[1][$s],
'location' => $fbook[2][$s],
'start_time' => $fbook[3][$s],
// 'end_time' => $fbook[4][$s], //These need to be excluded if they are empty
// 'picture' => $fbook[5][$s],
'privacy' => $fbook[6][$s],
);
// Start the Graph API call
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$result = curl_exec($ch);
$decoded = json_decode($result, true);
curl_close($ch);
if (is_array($decoded) && isset($decoded['id']))
{
$msg = "Event created successfully: {$decoded['id']}";
}
echo '<hr />' . $msg;
}
/* End FaceBook Code */
}
感谢您的帮助,我希望这有助于任何人在将来遇到同样的问题!
答案 0 :(得分:2)
您的解决方案可能是存储访问令牌。请求offline_access
并抓住该令牌并抓住它。然后将该令牌用于Graph API调用。