Javascript 如何让 FB.api('/me/feed', 'post', ... 工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3932880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:42:08  来源:igfitidea点击:

How to get FB.api('/me/feed', 'post', ... to work?

javascriptfacebook

提问by Jan Deinhard

I've tried to use FB.api to post something to my feed for hours now. I can't get it to work for me. I gave the permissions to the app. I can post to my feed with the PHP SDK but I have to use JavaScript.

几个小时以来,我一直在尝试使用 FB.api 将一些内容发布到我的提要中。我无法让它为我工作。我给了应用程序的权限。我可以使用 PHP SDK 发布到我的提要,但我必须使用 JavaScript。

<button onclick="doPost()">Post to Stream</button>

<script>
window.doPost = function() {
  FB.api(
    '/me/feed',
    'post',
    { body: 'Trying the Graph' },
    Log.info.bind('/me/feed POST callback')
  );
};
</script>

Can someone give me the example of a simple HTML page that uses FB.api to post to a feed?

有人可以给我一个使用 FB.api 发布到提要的简单 HTML 页面的示例吗?

回答by Jan Deinhard

Well, I got it working myself. I'm not sure what was wrong the first time as I started from scratch with a new HTML file. I hope it will help someone:

好吧,我自己让它工作了。当我从头开始使用一个新的 HTML 文件时,我不确定第一次出了什么问题。我希望它会帮助某人:

    <!DOCTYPE html>
    <html xmlns:fb="http://www.facebook.com/2008/fbml">
    <head>
    </head>
    <body>

    <a href="#" onClick="postToFacebook()">Post to Facebook</a>

    <script>
    function postToFacebook() {
        var body = 'Reading Connect JS documentation';

        FB.api('/me/feed', 'post', { body: body, message: 'My message is ...' }, function(response) {
          if (!response || response.error) {
            alert('Error occured');
          } else {
            alert('Post ID: ' + response);
          }
        });
    }
    </script>

    <div id="fb-root"></div>
    <script>
      window.fbAsyncInit = function() {
        FB.init({
          appId  : 'YOUR APP ID GOES HERE',
          status : true, // check login status
          cookie : true, // enable cookies to allow the server to access the session
          xfbml  : true  // parse XFBML
        });
      };

      (function() {
        var e = document.createElement('script');
        e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
        e.async = true;
        document.getElementById('fb-root').appendChild(e);
      }());
    </script>

    </body>
    </html>

回答by Dan N

I use this code on fb game-app and looks like this http://trupa.files.wordpress.com/2012/04/prscreenan.jpg

我在 fb 游戏应用程序上使用此代码,看起来像这样http://trupa.files.wordpress.com/2012/04/prscreenan.jpg

<a href="#" onClick="publishStory();" class="sendFeed"><br><font style="color:#FFF; text-decoration:none;padding-left:27px;">post to wall</font></a><br>
<script>
function publishStory() {
  FB.ui({
    method: 'feed',
    name: 'message name',
    caption: 'message caption ',
    description: 'description goes here',
    link: 'the url current page',
    picture: 'if you want to add an image'
  }, 
  function(response) {
    console.log('publishStory response: ', response);
  });
  return false;
}
</script>

回答by Alex Baumgertner

In first example you forgot "message" property. With out "message" you can post everyone, but not self.

在第一个示例中,您忘记了“消息”属性。没有“消息”,您可以发布每个人,但不能发布自己。