javascript - Facebook 使用 javascript sdk 在我的时间线上分享自定义故事

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

这是一个非常新手的问题,我找不到解决方案,我花了很多时间阅读他们的官方文档,但这一切都是徒劳的,所以我来到这里作为最后的手段。
我想做什么
我想在我的时间轴上发布我的自定义故事,

1)我创建了一个自定义故事
2)尝试通过共享对话框共享它[没有成功],并且也尝试过API调用。

<div id="fb-root"></div>

<!-- facebook code starts here -->

<script>
  function statusChangeCallback(response) {
    console.log('statusChangeCallback');
    console.log(response);


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

  window.fbAsyncInit = function() {
  FB.init({
    appId      : xxxxx[correctly used app id],
    cookie     : true,  // enable cookies to allow the server to access 
    status     : true,           
    xfbml      : true,  // parse social plugins on this page
    version    : 'v2.0' // use version 2.0
  });



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




  FB.api(
          'me/objects/restaurant.restaurant',
          'post',
          {
            app_id: xxxxx[correctly used app id],
            type: "restaurant.restaurant",
            url: "http://samples.ogp.me/440002909390231",
            title: "Sample Restaurant",
           image:"https://static.ak.fbcdn.net/images/devsite/attachment_blank.png",
            description: "This place rocks",
            contact_info: "http://www.facebook.com",
            location: "-122.148283"
          },
          function(response) {
            // handle the response
          }
    );


  FB.api(
          'me/testxdev:eat',
          'post',
          {
            restaurant: "http://samples.ogp.me/440002909390231"
          },
          function(response) {
            // handle the response
          }
        );


  };//********END OF FUNCTION ENCLOSING INIT()***********

  // 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 = "//connect.facebook.net/en_US/sdk/debug.js";
    fjs.parentNode.insertBefore(js, fjs);
  }(document, 'script', 'facebook-jssdk'));


</script>  

 <div class="fb-like" data-send="true" data-width="450" data-show-faces="true"></div>


<!--  End of facebook code-->

最佳答案

我能够解决我自己的问题,我也发布了解决方案,这样其他人就不会那么困难了。

<div id="fb-root"></div>
<!-- facebook code starts here -->

<script>

  function statusChangeCallback(response) {


    if (response.status === 'connected') {



     FB.api('/me', function(response) {
        console.log("here");
            console.log(JSON.stringify(response));
});  

  FB.api(
         '/me/objects/restaurant.restaurant',
         'post',
         {
           app_id: "[your app id]",
           type: "restaurant.restaurant",
           url: "http://samples.ogp.me/440002909390231",
           title: "Sample Restaurant",
           image: "https://s-static.ak.fbcdn.net/images/devsite/attachment_blank.png",
           description: "This place rocks",
           contact_info: "http://www.facebook.com",
           location: "-122.148283"
         },
         function(response) {
           // handle the response[this helped a lot]
           console.log(JSON.stringify(response));

         }
        ); 

          FB.api(
              '/me/testxdev:eat',
              'post',
              {
            restaurant: "http://samples.ogp.me/440002909390231"
              },
              function(response) {
            // handle the response
              console.log("the action response "+response);
               console.log(JSON.stringify(response));

              }
           );

    } else if (response.status === 'not_authorized') {
      document.getElementById('status').innerHTML = 'Please log ' +
        'into this app.';
    } else {
      document.getElementById('status').innerHTML = 'Please log ' +
        'into Facebook.';
    }
  }


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

  window.fbAsyncInit = function() {
  FB.init({
    appId      : "[your app id]",
    cookie     : true,  
    status     : true,     
    xfbml      : true,  
    version    : 'v2.0' 
  });

  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 = "//connect.facebook.net/en_US/sdk/debug.js";
    fjs.parentNode.insertBefore(js, fjs);
  }(document, 'script', 'facebook-jssdk'));


</script>


<fb:login-button scope="public_profile,email,publish_actions",
    auth_type: 'rerequest' onlogin="checkLoginState();">
</fb:login-button>
<div id="status"></div>
 <div class="fb-like" data-send="true" data-width="450" data-show-faces="true"></div>


<!--  End of facebook code-->

上面的代码工作正常[代码来自自定义创建的故事],主要问题是默认情况下共享帖子时不会出现在时间线上[因为应用程序处于开发模式],因此要查看帖子,请查看最近的事件日志。它必须在那里可见。 如果您希望帖子出现在时间轴上,请将应用程序仪表板中的状态和审核下的应用程序可见性设置为"is"

截至 2014 年,如果您想向用户发布任何内容[除了应用程序注册开发者],您需要 Facebook 许可,即应用程序需要提交审批请参阅以下链接 https://developers.facebook.com/docs/facebook-login/permissions/v2.0

您可以检查它是否位于概述部分的正下方。

关于javascript - Facebook 使用 javascript sdk 在我的时间线上分享自定义故事,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23561193/

相关文章:

json - Three20 和 Facebook Graph API 问题

java - 使用图形 API 发布到 Facebook 用户的留言墙上

javascript - 经常使用 $scope.$apply() 是一种好习惯吗?

javascript - Facebook 登录仅显示按钮

Javascript代码依赖分析工具

html - HTML+CSS 中的时间线示例,如 Facebook 的时间线

facebook - 带有 Facebook 的实时应用程序

android - 为什么使用 Graph API 获取 FB 头像需要访问 token ?

javascript - 检测图像在 JavaScript 中是最暗还是最亮?

javascript - 平滑 jQuery 转换