如何使用PHP SDK和Graph API将事件发布到Facebook粉丝页面?

The following code works, but it creates the event as the currently logged-in page administrator. I need it to post the event to the fan page itself. FYI: My app has been granted the permissions scope: manage_pages

<?php

// Initialize a Facebook instance from the PHP SDK
$config = array(
  'appId' => FACEBOOK_APP_ID,
  'secret' => FACEBOOK_APP_SECRET,
  'cookie' => false,
  'scope' => 'manage_pages,create_event'
);

$facebook = new Facebook($config);

$access_token = $facebook->getAccessToken();

$page_id = 'XXXXXXXXXXXX';

// 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'];

// Declare the variables we'll use to demonstrate
// the new event-management APIs
$event_id = 0;
$event_name = "New Event API Test Event";
$event_start = time() + rand(1, 100) * rand(24, 64) * 3600;
$event_privacy = "SECRET";//We'll make it secret so we don't annoy folks.

// We'll create an event in this example.
// We'll need create_event permission for this.
$params = array(
  'name' => $event_name,
  'start_time' => $event_start,
  'privacy_type' => $event_privacy,
  'access_token' => $page_access_token,
  'page_id' => $page_id //where $page_id is the ID of the page you are managing
);

// Create an event
$ret_obj = $facebook->api("/$page_id/events", 'POST', $params);
if(isset($ret_obj['id'])) {
 //Success
  $event_id = $ret_obj['id'];
  printMsg('Event ID: ' . $event_id);
} else {
  printMsg("Couldn't create event.");
}

// Convenience method to print simple pre-formatted text.
function printMsg($msg) {
   echo "
$msg
";
}

?>
4
额外 编辑
意见: 1
这是上面修改的代码吗?我现在可以真正使用一些帮助! :)
额外 作者 Jody Heavener,
我想通了:我添加了参数 page_id$ params 的列表中,并给它赋值 $ page_id ,它马上就开始了!如果我能够在接下来的几个小时内回忆起由于信誉低下而能够帮助其他人的情况,我会发布修改后的代码。不管谢谢!
额外 作者 lincolnberryiii,
@JodyHeavener我刚刚为你更新了它。
额外 作者 lincolnberryiii,
致命错误:未捕获OAuthException:无效的OAuth访问令牌。扔在/home/scott/public_html/facebook-php-sdk/src/base_facebook.p‌ hp on line 1340
额外 作者 Scott Fleming,

1 答案

Answer is here: Facebook PHP API, Post an Event on a Page

为方便起见复制如下:

我认为你需要页面access_token,在这里你如何得到它:

$page_id = "XXXXXXXXX";
$page_access_token = "";
$result = $facebook->api("/me/accounts");
foreach($result["data"] as $page) {
    if($page["id"] == $page_id) {
        $page_access_token = $page["access_token"];
        break;
    }
}

确保您授予manage_pages权限并将access_token添加到$ event_info数组中。

0
额外