樣品使用PHP ICM簽名的請求:
<?php
$signedRequestObject = parse_signed_request($_POST["signed_request"],YOUR_APPLICATION_SECRET);
if ($signedRequestObject["oauth_token"]){
// there is no token, something went wrong
exit;
}
$token = $signedRequestObject["oauth_token"];
$data = array(
"message" => "happy joy joy message",
"link" => "www.myjoyfullsite.com",
"access_token" => $token,
"picture" => "www.myjoyfullsite.com/avatar.jpg",
"name"=> "funky title",
"caption"=> "awesome caption",
"description"=> "useful description"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/".$id."/feed");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$op = curl_exec($ch);
if (!$op){
echo "Curl Error : ".curl_error($ch);
curl_close($ch);
exit;
}
curl_close($ch);
$res = get_object_vars(json_decode((string)$op));
print_r($res);
//used functions
function parse_signed_request($signed_request, $secret) {
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$sig = $this->base64_url_decode($encoded_sig);
$data = json_decode($this->base64_url_decode($payload), true);
if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') {
echo 'Unknown algorithm. Expected HMAC-SHA256 : ';
return false;
}
$expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
if ($sig !== $expected_sig) {
echo = 'Bad Signed JSON signature!';
return false;
}
return $data;
}
function base64_url_decode($input) {
return base64_decode(strtr($input, '-_', '+/'));
}
?>
樣品JS:
var body = 'Reading JS SDK documentation';
FB.api('/me/feed', 'post', { message: body }, function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response.id);
}
});
希望它能幫助!
乾杯!
你應該做一個實際的HTTP POST請求,而不僅僅是添加method = post到你的GET請求。這就是說,我不知道他們爲什麼會在這種情況下給你一個ID。 – 2011-12-19 04:58:34
我應該澄清一點,我在C#中使用WebRequest來發出實際的POST請求。該帖子現在顯示在我的測試業務頁面上。 – imnotsean 2011-12-19 05:27:02