2011-02-08 55 views
0

我希望我們的web應用能夠使用脫機訪問權限更新用戶牆。目前我正在圍繞圈子,因爲我似乎無法存儲訪問令牌。我已經使用使用example.php但我不保留會話(我想,請原諒我是一個新手)facebook存儲訪問令牌,用於使用php sdk進行離線訪問 - 非常困惑

我希望它的工作方式是如下嘗試:

用戶單擊Add Facebook的 - 即我們的應用程序被用戶批准(我可以使用圖表api來執行此操作)

令牌已返回,我們將其保存在數據庫中(這一點我正在努力讓我的頭腦)以便稍後啓用帖子。

如果任何人都可以給我一步一步的指導,我會很感激。請不要只是將我重定向到Facebook的開發者頁面。

回答

1

您使用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); 

我認爲這是有幫助的..

+0

非常感謝您的幫助 – Textus 2011-02-09 16:33:39