首页 文章

Facebook API:如何在没有offline_access权限的情况下在用户离线时发布到页面Feed

提问于
浏览
14

使用Facebook _1263239已成功发布给用户's feed while the user is offline with only the publish_stream permission. I don' t需要offline_access权限 . 我在这里解释了我是怎么做到的:Facebook Stream Publish while offline

当用户离开时,我没有成功发布到页面 . 这是我的情景:

用户U是Page P. P的管理员,授权并安装我的应用程序 . U授权我的应用程序并授予我以下扩展权限:

  • publish_stream

  • manage_pages

一旦用户离线,如果我尝试使用相同的技术发布到用户的流(没有offline_access权限)而是发布到页面,我得到“用户没有授权应用程序执行此操作” . 这是技术:

1)获取我的应用程序的access_token

2)使用我的应用's access_token to publish to Page P'的Feed: POST https://graph.facebook.com/ / feed

如果在步骤2中使用代替,那么它会毫无问题地发布到用户的Feed . 但是,我想发布到Page的Feed . 是否有可能做到这一点?或者我是否需要用户的offline_access权限才能执行此操作?

谢谢,约翰尼

3 回答

  • 2

    问题是你需要使用访问令牌为通过获得的提交提供的页面...

    呃...更容易说出来是这样的:

    您的应用请求预先设置“manage_pages” - 一旦您接受/授予预授权,那么您有一个access_token用于应用程序预置(离线只会使expires = 0)

    所以现在你的应用程序已预先管理你的页面,但它需要特定页面的令牌......

    因此,如果您使用第一个令牌发出 /me/accounts (或 /UID/accounts ),您将获得应用程序可以访问的页面列表及其各自的令牌...

    从那里只需获取页面的标记,然后使用该标记发出命令

    使用facebook类( facebook.php 和cert文件)

    require_once 'library/facebook.php';
    
    $app_id = "YOURAPPID";
    $app_secret = "YOURSECRET";
    
    $facebook = new Facebook(array(
        'appId' => $app_id,
        'secret' => $app_secret,
        'cookie' => true
    ));
    
    $token =  array(
        'access_token' => 'THE Token from app / manage_page auth'
    );
    
    $userdata = $facebook->api('/me/accounts', 'GET', $token);
    
    echo '<pre'>;
    print_r($userdata);
    echo '</pre>';
    

    您应该看到一个页面ID列表及其访问权限...

    通常我做一个 foreach $userdata['data'] 并查找页面ID,然后我从该子阵列中获取令牌...

  • 7

    全流程 - 四页示例(更容易编辑和理解示例)

    所以配置只有基本的应用信息......

    当你加载 index.php - 它重定向到Facebook以获得页面的授权..(可能已经有这个...但覆盖所有基础)

    然后Facebook重定向回redirecturl( backfromfb.php )...

    Facebook将访问令牌作为哈希而不是get变量返回,因此该页面将使用哈希作为变量进行刷新...

    然后它加载 PageUpdate.php ,它处理通过app / admin令牌的循环,并找到你要发布到的页面的正确值 .

    然后它构建帖子并提交它..

    听起来你对大部分内容都有所了解......所以希望这会帮助你最后一点 .

    config.php

    <?php
    $config = array(
        'appId' => 'YOUR APP ID',
        'secret' => 'YOUR APP SECRET',
        'cookie' => true;
        );
    
    $baseurl = 'http://yoursite.com/';
    $returnpage = 'backfromfb.php';
    
    require_once('library/facebook.php');
    
    ?>
    

    index.php

    <?php require_once('config.php'); ?>
    
    <html><head><title>Redirecting for auth</title></head><body><script type="text/javascript">
                window.location.href = 'https://www.facebook.com/dialog/oauth?client_id=<?php echo $config['appId']; ?>&redirect_uri=<?php echo $baseurl . $returnpage; ?>&scope=manage_pages&response_type=token';
    </script></body></html>
    

    backfromfb.php

    <?php
    require_once('config.php');
    // this page just passes the access token from the hash to a GET arg...
    if (!isset($_GET['access_token'])) {
    ?>
    <html><head><title>Redirecting</title></head><body><script type="text/javascript">
                accessToken = window.location.hash.substring(1);
                window.location.href = '<?php echo $baseurl . $returnpage; ?>?' + accessToken;
    </script></body></html>
    <?php
    } else {
    require_once('PageUpdate.php');
    } ?>
    

    PageUpdate.php

    <?php
    require_once('config.php');
    
    $pageID = "123456 WHatever you page id is";
    
    $AppToken = array(
        'access_token' =>  $_REQUEST['acess_token']
    );
    
    $fb = new Facebook($config);
    
    // Load APP page access rights for user via AppToken
    $pageAdmin = $fb->api('/me/accounts', 'GET', $AppToken);
    
    // Find the page access token
    foreach ($pageAdmin['data'] as $data) {
        if ($data['id'] == $pageID) {
            $pageToken['access_token'] = $data['access_token'];
            continue;
        }
    }
    
    // compile the post
    $WallPost = array(
        'message' => 'Test post from my app!'
    );  // you can also use 'picture', 'link', 'name', 'caption', 'description', 'source'.... 
        //http://developers.facebook.com/docs/reference/api/
    
    
    // post to wall
    $response = $fb->api($pageID . '/feed','POST',$WallPost);
    
    if($response) {
    
        echo 'Success!';
        echo '<pre>' . $response . '</pre>';
    
    } else echo "failed";
    
    
    ?>
    
  • 1

    这是我的答案 . 上面的代码对我不起作用 . 但我为自己制作了一个,完美无缺 . 这是代码 .

    服务器端代码:

    <?php
    @session_start();
    require "fblib/facebook.php";
    define('YOUR_APP_ID','');
    define('YOUR_APP_SECRET','');
    $facebook = new Facebook(array(
        'appId'  => YOUR_APP_ID,
        'secret' => YOUR_APP_SECRET,
    ));
    
    if($_SESSION['access_token']!='') {
        $access_token = $_SESSION['access_token'];
    $user_id = $_SESSION['user_id'];
    } else {
        $access_token = $_REQUEST['access_token'];
        $_SESSION['access_token'] = $_REQUEST['access_token'];
        $user_id = $_REQUEST['user_id'];
        $_SESSION['user_id'] = $_REQUEST['user_id'];
    }
    
    $user_id = $_REQUEST['user_id'];
    
    $facebook->setAccessToken($_REQUEST['access_token']);
    
    $post =  array(
        'message' => 'This message is posted with access token - ' . date('Y-m-d H:i:s')
    );
    
    // and make the request
    $response = $facebook->api('/me/feed', 'POST', $post);
    ?>
    

    客户端代码:

    <?php
    require "fblib/facebook.php";
    define('YOUR_APP_ID','387647494631464');
    define('YOUR_APP_SECRET','857f41bdd23c26ae132a1c75a343ddc9');
    $facebook = new Facebook(array(
        'appId'  => YOUR_APP_ID,
        'secret' => YOUR_APP_SECRET,
    ));
    
    $user = $facebook->getUser();
    if ($user) {
        try {
            $user_profile = $facebook->api('/me');
        } catch (FacebookApiException $e) {
            // The access token we have is not valid
            $user = null;
        }
    }
    ?>
    <div id="fb-root"></div>
    <script language="javascript" src="js/jquery-1.7.2.min.js"></script> 
    <script>
        var accessToken;
        var uid;
        // Load the SDK Asynchronously
        (function(d){
            var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
            if (d.getElementById(id)) {
                return;
            }
            js = d.createElement('script'); 
            js.id = id; 
            js.async = true;
            js.src = "//connect.facebook.net/en_US/all.js";
            ref.parentNode.insertBefore(js, ref);
        }(document));
    
        // Init the SDK upon load
        window.fbAsyncInit = function() {
            FB.init({
                appId      : '', // App ID
                channelUrl : '//'+window.location.hostname+'/channel', // Path to your Channel File
                status     : true, // check login status
                cookie     : true, // enable cookies to allow the server to access the session
                xfbml      : true  // parse XFBML
            });
    
            // listen for and handle auth.statusChange events
            FB.Event.subscribe('auth.statusChange', function(response) {
                if (response.authResponse) {
                    // user has auth'd your app and is logged into Facebook
                    FB.api('/me', function(me) {
                        if (me.name) {
                            //   document.getElementById('auth-displayname').innerHTML = me.name;
                            accessToken = response.authResponse.accessToken;
                            uid = response.authResponse.userID;
                        }
                    })
                    //  document.getElementById('auth-loggedout').style.display = 'none';
                    //  document.getElementById('auth-loggedin').style.display = 'block';
                } else {
                    // user has not auth'd your app, or is not logged into Facebook
                    // document.getElementById('auth-loggedout').style.display = 'block';
                    // document.getElementById('auth-loggedin').style.display = 'none';
                }
            });
    
            // respond to clicks on the login and logout links
            document.getElementById('auth-loginlink').addEventListener('click', function() {
                // FB.login();
                FB.login(function(response) {
                    // handle the response
                }, {scope: 'offline_access,publish_stream'});
            });
        }
    
        function gettoken() {
            //  alert("User Token :"+accessToken+", User id :"+uid);          
            $.post('fbpost.php',{access_token:accessToken,user_id:uid},function(data) {
                //  alert(data);
            });
        }
    </script>
    <?php if (!$user): ?>
        <a href="Javascript:void(0)" id="auth-loginlink">Login with Facebook</a>
    <?php else: ?>
        <a href="Javascript:void(0)" id="auth-logoutlink" onClick="FB.logout()" >Logout from Facebook</a>
    <?php endif ?>
    <a href="Javascript:void(0)"  onclick="gettoken()" >Post Into Wall</a>
    

    请从fb apps advance设置中禁用“删除offline_access权限:” . 通过选择禁用单选按钮 .

相关问题