Facebook API - 发布到时间轴

时间:2013-01-12 23:32:03

标签: javascript html facebook facebook-graph-api facebook-timeline

我知道这不会那么困难;也许我会以错误的方式解决这个问题。希望你能帮助我。

我有一个网站需要能够将字符串发布到某人的Facebook时间线。

我已经让他们正确登录,但我没有找到任何有效的代码,以便在他们获得许可后向他们的时间表提交信息。

我跟着示例here没有运气。我已经

  • 创建了一个应用
  • 登录Facebook
  • 使用配方对象定义Cook操作
  • 使用我们提供的代码在我的网站上创建了一个页面,将[your_app_id]替换为我的应用ID
  • 尝试发布烹饪动作

但是当我点击我网站上的示例页面上的烹饪按钮时,它会给我一个错误:

No callback passed to the ApiClient for https://graph.facebook.com/me/fblaevents:cook 

并弹出error occurred浏览器提醒。

以下是我正在使用的示例代码:

<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US"
  xmlns:fb="https://www.facebook.com/2008/fbml"> 
<head prefix="og: http://ogp.me/ns# fblaevents: 
              http://ogp.me/ns/apps/fblaevents#">
  <title>OG Tutorial App</title>
  <meta property="fb:app_id" content="542182755801091" /> 
  <meta property="og:type" content="fblaevents:recipe" /> 
  <meta property="og:title" content="Stuffed Cookies" /> 
  <meta property="og:image" content="http://fbwerks.com:8000/zhen/cookie.jpg" /> 
  <meta property="og:description" content="The Turducken of Cookies" /> 
  <meta property="og:url" content="http://fbwerks.com:8000/zhen/cookie.html">

  <script type="text/javascript">
  function postCook()
  {
      FB.api(
        '/me/fblaevents:cook',
        'post',
        { recipe: 'http://fbwerks.com:8000/zhen/cookie.html' },
        function(response) {
           if (!response || response.error) {
              alert('Error occured');
           } else {
              alert('Cook was successful! Action ID: ' + response.id);
           }
       });
  }
  </script>
</head>
<body>
  <div id="fb-root"></div>
  <script>
    window.fbAsyncInit = function() {
      FB.init({
        appId      : '542182755801091', // App ID
        status     : true, // check login status
        cookie     : true, // enable cookies to allow the server to access the session
        xfbml      : true  // parse XFBML
      });
    };

    // Load the SDK Asynchronously
    (function(d){
      var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
      js = d.createElement('script'); js.id = id; js.async = true;
      js.src = "//connect.facebook.net/en_US/all.js";
      d.getElementsByTagName('head')[0].appendChild(js);
    }(document));
  </script>

  <h3>Stuffed Cookies</h3>
  <p>
    <img title="Stuffed Cookies" 
         src="http://fbwerks.com:8000/zhen/cookie.jpg" 
         width="550"/>
  </p>

  <br>
  <form>
    <input type="button" value="Cook" onclick="postCook()" />
  </form>
</body>
</html>

如果有更简单的方法,请指出给我。我不知道我在做什么。

1 个答案:

答案 0 :(得分:0)

您应该用自己的代码替换JS代码中的OG元标记和配方URL。由于上面的代码只是一个起始代码,而不是一个工作演示(或者可能是现在但链接已经死了)。

此外,由于您获得了Error occured,因此很可能response.error中有一些内容,因此您应该通过调用控制台方法来检查其值,例如:

if (!response || response.error) {
   console.log('Error occured: ', response.error);
} else {

这会给你:

  

发生错误:对象{type:“异常”,消息:“无法   从URL检索数据。“,代码:1660002}