您使用JavaScript的身份驗證
FB.login(function(response) {
// As of Facebook's transition to OAuth 2.0 session has been replaced with authResponse
// See https://developers.facebook.com/blog/post/525/
// var access_token = response.session.access_token;
var access_token = null;
if (response.authResponse) {
access_token = response.authResponse.accessToken;
if (response.perms) {
// user is logged in, everithig is ok
} else {
// user is logged in, but did not grant any permissions
}
} else {
// user is not logged in
}
}, {perms:'read_stream,publish_stream,offline_access'});
您將獲得訪問令牌在JavaScript變量access_token.You可以保存此訪問令牌在數據庫中,然後發佈使用下面的代碼
牆
function graphStreamPublish(){
var body = document.getElementById("txtTextToPublish").value;
FB.api('/me/feed', 'post', { message: body }, function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response.id);
}
});
}
或者如果你想使用php sdk然後創建authentication.php如下。
<?php
$app_id = "YOUR_APP_ID";
$app_sec = "APP_SEC";
$canvas_page = "APP_CANVAS_PAGE_URL";
$scope = "&scope=user_photos,email,publish_stream"; $auth_url"http://www.facebook.com/dialog/oauth?client_id=" . $app_id . "&redirect_uri=" . urlencode($canvas_page).$scope;
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode(".", $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, "-_", "+/")), true);
if (empty($data["user_id"])) {
echo(""); }
$access_token = $data["oauth_token"];
$user_id = $data["user_id"];
$user = json_decode(file_get_contents("https://graph.facebook.com/me?access_token=" . $access_token));
function get_facebook_cookie($app_id, $application_secret) {
$args = array();
parse_str(trim($COOKIE["fbs" . $app_id], "\""), $args);
ksort($args);
$payload = "";
foreach ($args as $key => $value) {
if ($key != "sig") {
$payload .= $key . "=" . $value;
}
}
if (md5($payload . $application_secret) != $args["sig"]) {
return null;
}
return $args;
}
$cookie = get_facebook_cookie($app_id, $app_sec);
?>
在您需要發佈到牆上的頁面包括這個頁面和Facebook API頁面(facebook.php),那麼代碼發佈到牆壁
$attachment = array('message' => 'some meesgae',
'name' => 'This is my demo Facebook application!',
'caption' => "Caption of the Post",
'link' => 'mylink.com',
'description' => 'this is a description',
'actions' => array(array('name' => 'Get Search', 'link' => 'google.com')));
$result = $facebook->api('/me/feed?access_token='.$access_token, 'post', $attachment);
我認爲這是有幫助的..
非常感謝您的幫助 – Textus 2011-02-09 16:33:39