【问题标题】:Facebook - How can i post to a company using the javascript sdk?Facebook - 我如何使用 javascript sdk 向公司发布信息?
【发布时间】:2012-04-23 23:04:26
【问题描述】:

我是 facebook 发帖的新手,但在使用用户帐户离线发布时取得了一些成功,但无法使用公司页面离线发布。

我通过我自己的个人 Facebook 帐户创建了自己的“Facebook 应用程序”,称为“尼克海报应用程序”。我已为我的个人页面和公司页面授予应用程序三个权限(offline_access、read_stream、publish_stream)。 我对每个帐户都按照以下步骤操作...

Creating the app...
1.       Login to facebook with the account you want linked to the app
2.       Follow this link http://www.facebook.com/developers/apps.php#!/developers/createapp.php
3.       Create your app and take a note of you App Id and your App secret Id.

Giving the correct rights to the app and getting the access_token..
Method 1:
1.       Get the account in question to login to facebook
2.       However you like, direct the user to this link (replacing <App-Id> with the App Id of the created app) https://graph.facebook.com/oauth/authorize?client_id=<App-Id>&scope=offline_access,read_stream&redirect_uri=http://www.facebook.com/connect/login_success.html
3.       Take a note of the result of the “code” querystring.
4.       Goto this url (replace “<APP-ID>” with you appId and “<APP-SECRET>” with your apps secret id and “<code>” with the copied code)
https://graph.facebook.com/oauth/access_token?client_id=<APP-ID>&redirect_uri=http://www.facebook.com/connect/login_success.html&client_secret=<APP-SECRET>&code=<code>
5.       Copy what you see, minus the expires querystring.  That is your access_token.

在获得两个帐户的访问令牌后,我使用此代码发布帖子。

<!-- FACEBOOK -->        
<div id="fb-root"></div>
<script>
    (function () {
        var e = document.createElement('script');
        // replacing with an older version until FB fixes the cancel-login bug
        e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
        //e.src = 'scripts/all.js';
        e.async = true;
        document.getElementById('fb-root').appendChild(e);
    } ());
</script>
<!-- END-OF-FACEBOOK -->
<script>
    //initialise
    window.fbAsyncInit = function () {
        FB.init({
            appId: '351023398277068',
            status: true, // check login status
            cookie: true, // enable cookies to allow the server to access the session
            xfbml: true, // parse XFBML
            oauth: true // Enable oauth authentication
        });
    };
    function sendPost(inMessage) {
        var opts = {
            message: inMessage,
            access_token: '<SAVED-TOKEN>'
        };

        FB.api('/me/feed', 'post', opts, function (response) {
            if (!response || response.error) {
                alert('Posting error occured');
            }
            else {
                alert('Success - Post ID: ' + response.id);
            }
        });
    }

</script>

当使用参数“测试帖子”执行“sendPost”命令时,它将适用于我的个人帐户(前提是我将 access_token 放在适当的位置)。这不适用于我的公司页面,我不知道为什么(我确实将我的 access_token 放在了适当的位置)。

Facebok 也没有很好地记录这一点,因此很难取得进展,有人明白为什么这对公司页面不起作用吗?

提前谢谢你。

【问题讨论】:

    标签: facebook integration


    【解决方案1】:

    您可以设置“to”参数来定位您希望发布到的页面,“如果您希望将您的页面作为应用程序发布到您的页面,则需要管理页面权限。

    <div id="msg"></div>
    <script>
    // uid is the id of the page or user you wish to post to.
          function feedthis2(uid) {
    
            // calling the API ...
            var obj = {
            method: 'feed',
            to: ''+uid+''    
            };
    
            function callback(response) {
              document.getElementById('msg').innerHTML = "Post ID: " + response['post_id'];
            }
    
            FB.ui(obj, callback);
          }
        feedthis2('AnotherFeed'); // to http://facebook.com/anotherfeed
        //feedthis2('135669679827333');
    </script>
    

    【讨论】:

    • 我不想在其他用户帐户上发帖,只是在自己的新闻提要上发帖,或者如您所说的“作为您的页面发布到您的页面”。什么是“管理页面权限”。它类似于“offline_access”吗?目前我授权的唯一权限是“offline_access,read_stream”。我也使用过“publish_stream”,但没有任何乐趣。
    • 好的,我找到了“manage_pages”权限,现在正在使用我的新 access_token。这仍然不起作用,它仍然适用于我的个人帐户。有什么想法吗?
    • 对不起,如果我让您感到困惑,请作为页面发布到您需要作为页面登录并获取页面访问令牌的页面。 developers.facebook.com/docs/authentication/pages 创建一个附加页面访问令牌,将帖子设置为页面 ID。那么你的帖子应该可以工作了。
    • 不错的一个。您发送到那里的链接将我带到一个帮助我获得“页面访问令牌”的页面。我所做的只是使用“page access_token”而不是“users access_token”并将其成功发布到公司新闻源(使用我上面的代码)。它通过 nicks 海报应用程序制作了一个标准帖子,非常完美。再次感谢您。
    • 完成 :) 我是新用户,所以我不能投票。希望其他人会 :) 再次投票。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-30
    • 1970-01-01
    • 2011-08-18
    • 1970-01-01
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    相关资源
    最近更新 更多