这是使用图形 API 的代码:
事件.php:
<?php
$facebook_appid = "your appid"; // Facebook appplication id
$facebook_secret = "your secret"; // Facebook secret id
$facebook_pageid = "your pageid"; // Facebook secret id
$redirect_uri = "https://localhost/facebook_page/events.php"; // return url to our application after facebook login ## should be SAME as in facebook application
//$redirect_uri = "https://localhost/facebook_page/fb_login.php"; // return url to our application after facebook login ## should be SAME as in facebook application
$scope = "user_photos,email,user_birthday,user_online_presence,offline_access,manage_pages,publish_stream,user_events,friends_events"; // User permission for facebook
$code = $_REQUEST["code"]?$_REQUEST["code"]:"";
if(empty($code)) {
$_SESSION['state'] = rand(); // CSRF protection
$dialog_url = "https://www.facebook.com/dialog/oauth?client_id=". $facebook_appid . "&redirect_uri=" . urlencode($redirect_uri) . "&state=". $_SESSION['state'] . "&scope=".$scope;
header("location:".$dialog_url);
}
if($_SESSION['state'] && ($_SESSION['state'] == $_REQUEST['state'])) {
$token_url = "https://graph.facebook.com/oauth/access_token?". "client_id=" . $facebook_appid . "&redirect_uri=" . urlencode($redirect_uri). "&client_secret=" . $facebook_secret . "&code=" . $code;
$response = @file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$account_url = "https://graph.facebook.com/".$facebook_pageid."?fields=access_token&access_token=".$params['access_token'];
$resp = @file_get_contents($account_url);
$dt = json_decode($resp);
echo "Page accesstoken :".$dt->access_token;
echo "<br>";
echo "Page id : ".$dt->id;
}
?>
请注意:$redirect_uri = https://localhost/facebook_page/events.php
应与 facebook 应用程序页面设置中的网站 url 相同。