简体   繁体   English

如何使用Facebook Javascript SDK创建页面帖子?

[英]How can I create a page post using Facebook Javascript SDK?

I need to be able to post to a user's business page using the Facebook Javascript SDK. 我需要能够使用Facebook Javascript SDK发布到用户的业务页面。

I have successfully logged my user's into facebook but when I try to use a function to post to the user's page, nothing happens. 我已经成功地将用户登录到Facebook,但是当我尝试使用将功能发布到用户页面时,什么也没有发生。 I have even tried to manually enter my own page id into the function to make it work but still nothing happens. 我什至尝试将自己的页面ID手动输入到该函数中以使其正常工作,但仍然没有任何反应。

I log my user in using the following javascript which I got from the Facebook documentation and it works. 我使用从Facebook文档中获得的以下JavaScript登录我的用户,并且可以正常运行。

<script>

  // This is called with the results from from FB.getLoginStatus().
  function statusChangeCallback(response) {
    console.log('statusChangeCallback');
    console.log(response);
    // The response object is returned with a status field that lets the
    // app know the current login status of the person.
    // Full docs on the response object can be found in the documentation
    // for FB.getLoginStatus().
    if (response.status === 'connected') {
      // Logged into your app and Facebook.
      testAPI();
    } else {
      // The person is not logged into your app or we are unable to tell.
      document.getElementById('status').innerHTML = 'Please log ' +
        'into this app.';
    }
  }

  // This function is called when someone finishes with the Login
  // Button.  See the onlogin handler attached to it in the sample
  // code below.
  function checkLoginState() {
    FB.getLoginStatus(function(response) {
      statusChangeCallback(response);
    });
  }

  window.fbAsyncInit = function() {
    FB.init({
      appId      : 'my app id',
      cookie     : true,  // enable cookies to allow the server to access 
                          // the session
      xfbml      : true,  // parse social plugins on this page
      version    : 'v3.3' // The Graph API version to use for the call
    });

    // Now that we've initialized the JavaScript SDK, we call 
    // FB.getLoginStatus().  This function gets the state of the
    // person visiting this page and can return one of three states to
    // the callback you provide.  They can be:
    //
    // 1. Logged into your app ('connected')
    // 2. Logged into Facebook, but not your app ('not_authorized')
    // 3. Not logged into Facebook and can't tell if they are logged into
    //    your app or not.
    //
    // These three cases are handled in the callback function.

    FB.getLoginStatus(function(response) {
      statusChangeCallback(response);
    });

  };

  // Load the SDK asynchronously
  (function(d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s); js.id = id;
    js.src = "https://connect.facebook.net/en_US/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
  }(document, 'script', 'facebook-jssdk'));

  // Here we run a very simple test of the Graph API after login is
  // successful.  See statusChangeCallback() for when this call is made.
  function testAPI() {
    console.log('Welcome!  Fetching your information.... ');
    FB.api('/me', function(response) {
      console.log('Successful login for: ' + response.name);
      document.getElementById('status').innerHTML =
        'Thanks for logging in, ' + response.name + '!';
    });
        }

    </script>

I then altered the code to add a function called postapi() to test a post to the logged in user's page as shown below: 然后,我更改了代码,以添加一个名为postapi()的函数来测试已登录用户页面上的帖子,如下所示:

<script>
        function postapi() {
            FB.api(
                '/{your-page-id}/feed',
                'POST',
                { "message": "Awesome!" },
                function (response) {
                    // Insert your code here
                });
        }
  // This is called with the results from from FB.getLoginStatus().
  function statusChangeCallback(response) {
    console.log('statusChangeCallback');
    console.log(response);
    // The response object is returned with a status field that lets the
    // app know the current login status of the person.
    // Full docs on the response object can be found in the documentation
    // for FB.getLoginStatus().
    if (response.status === 'connected') {
      // Logged into your app and Facebook.
      testAPI();
    } else {
      // The person is not logged into your app or we are unable to tell.
      document.getElementById('status').innerHTML = 'Please log ' +
        'into this app.';
    }
  }

  // This function is called when someone finishes with the Login
  // Button.  See the onlogin handler attached to it in the sample
  // code below.
  function checkLoginState() {
    FB.getLoginStatus(function(response) {
      statusChangeCallback(response);
    });
  }

  window.fbAsyncInit = function() {
    FB.init({
      appId      : 'my app id',
      cookie     : true,  // enable cookies to allow the server to access 
                          // the session
      xfbml      : true,  // parse social plugins on this page
      version    : 'v3.3' // The Graph API version to use for the call
    });

    // Now that we've initialized the JavaScript SDK, we call 
    // FB.getLoginStatus().  This function gets the state of the
    // person visiting this page and can return one of three states to
    // the callback you provide.  They can be:
    //
    // 1. Logged into your app ('connected')
    // 2. Logged into Facebook, but not your app ('not_authorized')
    // 3. Not logged into Facebook and can't tell if they are logged into
    //    your app or not.
    //
    // These three cases are handled in the callback function.

    FB.getLoginStatus(function(response) {
      statusChangeCallback(response);
    });

  };

  // Load the SDK asynchronously
  (function(d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s); js.id = id;
    js.src = "https://connect.facebook.net/en_US/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
  }(document, 'script', 'facebook-jssdk'));

  // Here we run a very simple test of the Graph API after login is
  // successful.  See statusChangeCallback() for when this call is made.
  function testAPI() {
    console.log('Welcome!  Fetching your information.... ');
    FB.api('/me', function(response) {
      console.log('Successful login for: ' + response.name);
      document.getElementById('status').innerHTML =
        'Thanks for logging in, ' + response.name + '!';
    });
        }

    </script>

I then use a button to open the login dialog and request the proper permissions as follows: 然后,我使用按钮打开登录对话框,并请求适当的权限,如下所示:

<fb:login-button scope="manage_pages,publish_pages,pages_show_list" onlogin="checkLoginState();">
</fb:login-button>

I successfully tested to see if this actually stored the user's access token by displaying the user access token in a textbox as follows: 通过在文本框中显示用户访问令牌,我成功测试了一下是否确实存储了用户的访问令牌:

function placetoken() {
            var user_access_token = FB.getAuthResponse()['accessToken'];
            document.getElementById("<%=TextBox1.ClientID%>").value=user_access_token;
        }

I now need to be able to use the user access token to get a list of the pages (including page id and page access token) that the user manages so they can choose one to post to. 现在,我需要能够使用用户访问令牌来获取用户管理的页面列表(包括页面ID和页面访问令牌),以便他们可以选择要发布到的页面。

I have tried: 我努力了:

var user_pages=FB.api('/me/accounts', 'GET');

But I cannot figure out how to capture what the api returns. 但我不知道如何捕获api返回的内容。

I would like to be able to use the Facebook Javascript SDK to log user's in and post to their business pages. 我希望能够使用Facebook Javascript SDK登录用户并发布到其业务页面。

Publishing on a page feed requires a page token valid for that specific page incl all required permissions granted by a page admin. 在页面供稿上进行发布需要对特定页面有效的页面令牌,包括页面管理员授予的所有必需权限。 Therefore you first need to retrieve a manage_pages permission with login, request a page token (via /me/accounts for example) for that specific page and then make the actual publish feed call. 因此,您首先需要获取具有登录名的manage_pages权限,为该特定页面请求页面令牌(例如,通过/ me / accounts),然后进行实际的发布feed调用。

In order to publish to a Page (as a Page, which is the only option), you need to to the following: 为了发布到页面(作为页面,这是唯一的选择),您需要执行以下操作:

  • Authorize with the manage_pages and publish_pages permissions. 使用manage_pagespublish_pages权限进行授权。
  • Get a Page Token of the Page in question with /page-id?fields=access_token 使用/page-id?fields=access_token获取相关页面的页面令牌
  • Use the Page Token to post with /me/feed 使用页面令牌通过/me/feed

Example Request with Page Token: 带页面令牌的示例请求:

FB.api(
    '/me/feed',
    'POST',
    {message: 'Awesome!', access_token: 'the-page-token'},
    function (response) {
        // Insert your code here
    }
);

Be aware that only users with a role in the App would see the new post, unless you put your App live and get the required permissions reviewed by Facebook. 请注意,除非您将应用程序投入使用并获得Facebook审阅的所需权限,否则只有在该应用程序中具有角色的用户才能看到该新帖子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Facebook JavaScript SDK将其发布到墙上,如页面管理员? - How to post to the wall like the admin of a page using the Facebook JavaScript SDK? 我可以在Facebook SDK中使用JavaScript创建Android应用吗? - Can i create android apps using javascript with facebook sdk? 如何使用facebook javascript sdk在“访客帖子”上发布? - how to post on 'visitor post' using facebook javascript sdk? 如何创建相册并向其发布图像,facebook javascript sdk - How to create album and post image to it,facebook javascript sdk 使用facebook javascript sdk将评论发布到某人的个人资料帖子或页面帖子或群组帖子 - Post comments to someones profile post or page post or group post using facebook javascript sdk 如何使用Javascript SDK在Facebook墙上发布HTML消息? - How to post HTML message in Facebook wall using Javascript SDK? 如何使用JavaScript SDK附加位置附加到Facebook - How to post to Facebook with location attached using JavaScript SDK 如何使用 FB.api Javascript ZF20E3C5E54C0AB3D96FAZ5D660B3 将视频发布到 Facebook - How to post a video to Facebook using FB.api Javascript SDK? 如何在Ajax调用中使用Facebook Javascript SDK添加Facebook分享按钮? - How can I add a Facebook share button using the Facebook Javascript SDK in an Ajax call? 使用 Javascript sdk 提取 Facebook 帖子 - Facebook post extraction using Javascript sdk
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM