我想将事件从 facebook 页面提取到官方网站,并与图形和 oauth 斗争以获取信息。
简化代码,相同代码中的第 1 阶段和第 2 阶段:
<?php
$app_id = '...........4639';
$app_secret = '7547xxxxxxxxxxxxxxxxxxxxxxxx';
if (isset($_GET['code'])) {
//stage 2
$state = $_GET['state'];
$code = $_GET['code'];
//the code doesn't work. If I use graph api explorer to get key, I'm all fine.
//the code here from graph api explorer.
//$code = 'AAAIERWeZCHZxxxxxxxxxxxxxxxxxxxxx';
$s = "https://graph.facebook.com/_PAGE_ID?fields=feed.fields(story,message,picture)&access_token=" . $code;
$json = file_get_contents($s);
die($json);
} else {
//stage 1
$my_url = 'http://www.myself.com/fb/index.php';
$_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'];
//now redirect to myself - phase2
header("Location: " . $dialog_url);
exit();
}