【问题标题】:facebook storing access token for offline access with php sdk - really confusedfacebook存储访问令牌以使用php sdk进行离线访问 - 真的很困惑
【发布时间】:2011-06-23 16:13:12
【问题描述】:

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

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

用户点击添加 Facebook - 即我们的应用程序已获得用户批准(我可以使用图形 api 做到这一点)

令牌被退回,我们将其保存在数据库中(这点我很难理解)以便稍后启用帖子。

如果有人能给我一个分步指南,我将不胜感激。请不要只是将我重定向到 facebook 上的开发人员页面。

【问题讨论】:

    标签: php facebook graph access-token


    【解决方案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);
    

    我认为这很有帮助..

    【讨论】:

      【解决方案2】:

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

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

      希望对某人有所帮助。

      【讨论】:

        猜你喜欢
        • 2011-07-10
        • 2011-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-04
        • 2013-01-03
        • 1970-01-01
        相关资源
        最近更新 更多