【问题标题】:How can I create a page post using Facebook Javascript SDK?如何使用 Facebook Javascript SDK 创建页面帖子?
【发布时间】:2019-07-05 21:47:36
【问题描述】:

我需要能够使用 Facebook Javascript SDK 发布到用户的业务页面。

我已经成功地将我的用户登录到 facebook,但是当我尝试使用一个函数发布到用户的页面时,没有任何反应。我什至尝试在函数中手动输入我自己的页面 id 以使其工作,但仍然没有任何反应。

我使用从 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>

然后我修改了代码,添加了一个名为 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>

然后我使用一个按钮打开登录对话框并请求适当的权限,如下所示:

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

我通过在文本框中显示用户访问令牌成功测试了它是否实际存储了用户的访问令牌,如下所示:

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

我现在需要能够使用用户访问令牌来获取用户管理的页面列表(包括页面 ID 和页面访问令牌),以便他们可以选择一个发布到。

我试过了:

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

但我不知道如何捕获 api 返回的内容。

我希望能够使用 Facebook Javascript SDK 登录用户并发布到他们的业务页面。

【问题讨论】:

  • “什么都没有发生” - 将尝试的 API 调用的响应记录到控制台,然后您将看到 API 必须报告的错误。现在您在回调函数中只有注释// Insert your code here,没有其他任何内容,这意味着您目前完全忽略了任何响应。

标签: javascript facebook facebook-graph-api facebook-javascript-sdk


【解决方案1】:

为了发布到页面(作为页面,这是唯一的选择),您需要执行以下操作:

  • 使用manage_pagespublish_pages 权限进行授权。
  • 使用/page-id?fields=access_token 获取相关页面的页面令牌
  • 使用页面令牌以/me/feed 发布

带有页面令牌的示例请求:

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

请注意,只有在应用中拥有角色的用户才能看到新帖子,除非您将应用上线并获得 Facebook 审核的所需权限。

【讨论】:

    【解决方案2】:

    在页面供稿上发布需要对特定页面有效的页面令牌,包括页面管理员授予的所有必需权限。因此,您首先需要通过登录来检索 manage_pages 权限,请求该特定页面的页面令牌(例如通过 /me/accounts),然后进行实际的发布提要调用。

    【讨论】:

    • 我对此很陌生,但我的登录弹出窗口没有捕获这些凭据吗?当我登录并授予它们时,它会要求这些权限。我还需要做些什么来捕捉它们。我假设它们存储在 cookie 中并由 SDK 管理。
    • @JamieDixson 页面令牌是您需要通过 API 请求显式获取的东西,它不会自动提供。 developers.facebook.com/docs/facebook-login/…
    • 就像 misorude 所说的,您需要通过将权限添加到您的登录范围来明确请求权限。也就是说,发布帖子的权限需要应用程序审查和批准,否则创建的帖子将不会对公众可见。这意味着您需要将 manage_pages 和 publish_pages 添加到您的登录集成中,如果您希望帖子可见,您还需要提交应用审核。
    • 我遗漏了我所做的一些事情,并且由于您的 cmets,我能够获得用户令牌。我现在需要能够获取用户管理的页面列表,但我无法让 Facebook SDK documentation 中显示的示例 api 调用工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多