javascript - 如何使用自定义属性创建/发布 Open Graph 对象实例/记录?

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

var hashid = 'abc123'; 
var title = 'Awesome Widget';
FB.api( // creating the widget object instance/record
  'me/objects/myapp:widget',
  'post',
  {
    object: JSON.stringify({
      'app_id': <obfuscated>,
      'url': 'http://example.com/' + hashid, // maps to og:url
      'title': 'widget',  // maps to og:title
      'myapp:real_title': title,  // maps to nothing right now!  No bueno!
      'image': { // maps to og:image
        'url': 'http://example.com/images/' + hashid  
      },
      'description': 'Man, this widget is awesome!' // maps to og:description
    })
  },
  function(response) {
    // handle the response
  }
);

是的,我已经创建了自定义“widget”对象和自定义“real_title”属性。正在创建对象,但不包括“real_title”属性:

no bueno

我是否需要在对象实例/记录中为每个属性指定特殊语法(og:title 等)?

附言

我希望 og:title 只是“小部件”,因为我想以特定方式创建用户故事。因此,还需要指定 real_title

附言

我实际上是在创建对象实例、对象记录还是其他东西?

最佳答案

来自 the documentation :

Standard object properties are added to the top-level of the JSON object that you pass into your call to create an object.

Any property that's not a standard object property should be included as a data: {...} element on the JSON object you pass in when creating an object. Here's an example of a custom mountain type that includes an elevation custom property;

{
  title: 'Mt. Rainier', 
  type: 'myapp:mountain',
  image: 'http://en.wikipedia.org/wiki/File:Mount_Rainier_5917s.JPG', 
  url: 'https://url.to.your.app/example/mountains/Mt-Rainier',
  description: 'Classic cold war technothriller',
  data: {
    elevation: 14411
  }
}

This format is the same as what an object looks like when it's read back from the database via the Graph API.

简而言之,您需要执行以下操作:

var hashid = 'abc123'; 
var title = 'Awesome Widget';
FB.api( // creating the widget object instance/record
  'me/objects/myapp:widget',
  'post',
  {
    object: JSON.stringify({
      'app_id': <obfuscated>,
      'url': 'http://example.com/' + hashid, // maps to og:url
      'title': 'widget',  // maps to og:title
      'image': { // maps to og:image
        'url': 'http://example.com/images/' + hashid  
      },
      'description': 'Man, this widget is awesome!', // maps to og:description
      'data': {
        'real_title': title // maps to myapp:real_title
      }
    })
  },
  function(response) {
    // handle the response
  }
);

关于javascript - 如何使用自定义属性创建/发布 Open Graph 对象实例/记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19388297/

相关文章:

facebook-graph-api - Instagram OEmbed 问题 - OAuthException 200 提供有效的应用程序 ID

javascript - Iframe 应用程序 JavaScript SDK 发布对话框错误(代码 102)

javascript - 通过 Graph API 更新 Facebook 应用程序设置

javascript - Facebook 分享按钮不显示

Javascript 在流程之外放大图像

ios - Facebook SDK iOS : Add post to feed with image

javascript - 如何在 AngularJS 中有条件地用标签包围文本?

ios - 在 Facebook 上发布多张照片,并在单个墙贴中发布状态消息

javascript - 未定义 htmlLink Google 日历插入

javascript - 从外部文件调用 jQuery 日期选择器