新闻.阅读Facebook Opengraph

Waw*_*nde 1 wordpress facebook opengraph

我在这里遇到麻烦,你知道雅虎!新闻?我想创建一个这样的小部件(当你阅读它在Facebook上发布的文章时).

我刚刚创建了Facebook应用程序和Open Graph,但我不明白如何将其插入我的网站,我使用的是Wordpress,我在谷歌上搜索但我仍然不明白,希望你能帮忙我.


我仍然不明白,我只是创建应用程序和opengraph,也在我的标题和功能上插入opengraph标签,但当我尝试打开帖子页面时,我得到"错误发布"如何?你能帮助我吗?

phw*_*hwd 5

您需要通过阅读文档来了解这些概念的工作原理

  1. 用户在应用程序中执行操作,例如阅读文章
  2. App调用HTTP POST到Graph API端点/ me/action:object = Object_URL
  3. Facebook将抓取对象网页(您的WordPress页面),读取其元标记并通过操作将对象连接到用户.

你的行动是news.reads,所以你打电话如下

POST https://graph.facebook.com/me/news.reads?article=[article object URL in your Wordpress Blog]

为了完成这项工作,您需要read在应用设置中设置构建的操作类型:https://developers.facebook.com/apps/YOUR_APP_ID/opengraph

应用设置中的news.read

然后,您必须具有article对象集的类型.

文章对象

然后,您必须在WordPress博客上设置一个对象URL,这些是通过插入元标记完成的

<html>
    <head prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb# 
                  article: http://ogp.me/ns/article#">
     <meta property="fb:app_id"               content="YOUR_APP_ID"> 
     <meta property="og:type"                 content="article"> 
     <meta property="og:url"                  content="URL of this object">
     <meta property="og:site_name"            content="Name of site hosting article">
     <meta property="og:image"                content="URL to an image">
     <meta property="og:title"                content="Name of article">
     <meta property="og:description"          content="Description of object"> 
     <meta property="article:published_time"  content="DateTime"> 
     <meta property="article:modified_time"   content="DateTime"> 
     <meta property="article:expiration_time" content="DateTime">
     <meta property="article:author"          content="URL to Author object">
     <meta property="article:section"         content="Section of article">
     <meta property="article:tag"             content="Keyword">
    </head>
<body>
    <!-- main article body -->
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

因此,您可以根据主题设置将这些文件放在header.php或index.php文件中.然后你必须根据你的主题设置插入函数和if语句,例如

帖子的网址

<meta property="og:url" content="<?php the_permalink() ?>"/>
Run Code Online (Sandbox Code Playgroud)

单个帖子标题

<meta property="og:title" content="<?php single_post_title(''); ?>" />
Run Code Online (Sandbox Code Playgroud)

然后你需要lint这个url以确保你正确设置,你可以通过

http://developers.facebook.com/tools/debug
Run Code Online (Sandbox Code Playgroud)

一旦确定元标记设置正确,您需要测试前面提到的操作

POST https://graph.facebook.com/me/news.reads?article=[article object URL in your Wordpress Blog]

如果设置了所有内容,则应返回操作的ID.

然后,您必须使用JS SDK实现身份验证的逻辑

<div id="fb-root"></div>
  <script>
    window.fbAsyncInit = function() {
      FB.init({
        appId      : '[YOUR_APP_ID]', // 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>
Run Code Online (Sandbox Code Playgroud)

和登录按钮插件

<fb:login-button show-faces="true" width="200" max-rows="1" scope="publish_actions"> </fb:login-button>

在functions.php文件中或直接在single.php和index.php页面中

从那里你必须创建一个函数来在页面加载时调用下面的操作

<script type="text/javascript">
  function readArticle()
  {
      FB.api(
        '/me/news.reads',
        'post',
        { article: 'http://yourwordpress.com/site/' },
        function(response) {
           if (!response || response.error) {
              alert('Error occured');
           } else {
              alert('Article read was successful! Action ID: ' + response.id);
           }
        });
  }
  </script>
Run Code Online (Sandbox Code Playgroud)