Facebook使用php sdk存储访问令牌以进行离线访问 - 真的很困惑

时间:2011-02-08 19:38:18

标签: php facebook graph access-token

我希望我们的网络应用能够使用离线访问权限更新用户墙。我此刻正在转圈,因为我似乎无法存储访问令牌。我已经尝试过使用example.php,但我没有保留会话(我想,请原谅我,我是新手)

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

用户点击添加Facebook - 即我们的应用程序已获得用户批准(我可以使用图表api执行此操作)

返回令牌并将其保存在数据库中(这一点我正在努力解决)以便稍后启用帖子。

如果有人能给我一步一步的指导,我真的很感激。请不要只是将我重定向到Facebook的开发者页面。

2 个答案:

答案 0 :(得分: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中获取访问令牌。您可以将此访问令牌保存在数据库中,然后使用以下代码发布到墙上

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);

我认为这很有用..

答案 1 :(得分:0)

我创建了一个简单的演示iframe应用程序,它使用facebook php sdk来完成所需的操作。

http://eagerfish.eu/using-facebook-off-line-access-to-post-on-users-wall/

希望它可以帮到某人。