ruby “'og:type' 属性是必需的,但不存在。” 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25576259/
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-09-06 06:34:01  来源:igfitidea点击:

"The 'og:type' property is required, but not present." error

rubyfacebookfb-graph

提问by kitdesai

I'm having an issue with Facebook's graph API. I'm getting a "The 'og:type' property is required, but not present." error on this URL: http://thatnewjoint.com/posts/logic-driving-ms-daisy-feat-childish-gambino(and every other URL on my blog).

我遇到了 Facebook 图形 API 的问题。我收到“'og:type' 属性是必需的,但不存在。” 此 URL 上的错误:http: //thatnewjoint.com/posts/logic-driving-ms-daisy-feat-childish-gambino(以及我博客上的所有其他 URL)。

The og:type meta tag is in the HTML so i'm not sure why Facebook isn't recognizing it. Here's what their graph api call is returning:

og:type 元标记在 HTML 中,所以我不确定 Facebook 为何无法识别它。这是他们的图形 API 调用返回的内容:

{
   "id": "561280430667026",
   "created_time": "2014-08-28T20:35:18+0000",
   "is_scraped": false,
   "type": "website",
   "updated_time": "2014-08-28T20:35:18+0000",
   "url": "http://thatnewjoint.com/posts/eminem-superman-throwback-thursdays"
}

seems like when i'm posting using their api (via the koala ruby gem), the link doesn't get scraped? i would assume that the og:type error is causing this but here are the meta tags from that page:

似乎当我使用他们的 api(通过 koala ruby​​ gem)发布时,链接没有被刮掉?我会假设 og:type 错误导致了这一点,但这里是该页面的元标记:

<meta property="og:url" content="http://thatnewjoint.com/posts/eminem-superman-throwback-thursdays" />
<meta property="og:title" content="Eminem - &quot;Superman&quot; [Throwback Thursdays] | ThatNewJoint | Premier Hip Hop Blog" />
<meta property="og:description" content="Classic from The Eminem Show. I&#39;ll be honest - the video is very strange, but the song is great."
<meta property="og:type" content="video">
<meta property="og:image" content="http://i3.ytimg.com/vi/8kYkciD9VjU/hqdefault.jpg" />
<meta property="og:video" content="http://www.youtube.com/v/8kYkciD9VjU?version=3&amp;autohide=1">
<meta property="og:video:type" content="application/x-shockwave-flash">
<meta property="og:video:width" content="1920">
<meta property="og:video:height" content="1080">

EDITING WITH MORE INFO:

使用更多信息进行编辑:

Seems as though if I put the URL into the FB Debugger, I initially get this "The 'og:type' property is required, but not present." error BUT if I then click the "Fetch new scrape information" button, it will find all the correct information like it should. Is it possible that FB is attempting to scrape the URL too early? Seems like on their first pass, they just aren't scraping the URL properly, but I can't figure out why.

似乎如果我将 URL 放入FB Debugger,我最初得到这个“'og:type' 属性是必需的,但不存在。” 错误但是如果我然后单击“获取新的抓取信息”按钮,它将找到所有正确的信息。FB 是否可能过早地尝试抓取 URL?似乎在他们第一次通过时,他们只是没有正确抓取 URL,但我不知道为什么。

回答by totallytotallyamazing

You will need to add this HTML meta tag to your HEAD, with your-app-id (unique Facebook App ID). Just be sure to swap out "your-app-id":

您需要将此 HTML 元标记添加到您的 HEAD 中,并使用 your-app-id(唯一的 Facebook 应用 ID)。一定要换掉“你的应用程序ID”:

<meta property="fb:app_id" content="your-app-id">

To create a FB App ID go here: https://developers.facebook.com/apps

要创建 FB App ID,请访问:https: //developers.facebook.com/apps

And add your same unique Facebook App ID to the Facebook JS and place it as close to the top of your HTML body tag as you can. Just be sure to swap out "your-app-id":

并将您相同的唯一 Facebook 应用 ID 添加到 Facebook JS,并将其尽可能靠近 HTML 正文标签的顶部。一定要换掉“你的应用程序ID”:

<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId      : '{your-app-id}',
      xfbml      : true,
      version    : 'v2.0'
    });
  };

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

Also to place your website Facebook button add this code as well. Just be sure to swap out "your-unique-page-to-be-liked":

还要放置您的网站 Facebook 按钮,请添加此代码。一定要换掉“你要喜欢的独特页面”:

<div class="fb-like" data-href="your-unique-page-to-be-liked" data-layout="button_count" data-action="like" data-show-faces="true" data-share="false"></div>

Finally in Facebook Developers Dashboard take your FB Application out of "Developers Mode" and put it in "Public" Mode.

最后,在 Facebook Developers Dashboard 中,将您的 FB 应用程序从“开发者模式”中取出,并将其置于“公共”模式中。

For more info about FB Open Graph and meta tags here are 2 helpful links: 1. https://developers.facebook.com/docs/opengraph/using-objects2. https://developers.facebook.com/docs/sharing/best-practices

有关 FB Open Graph 和元标记的更多信息,这里有 2 个有用的链接: 1. https://developers.facebook.com/docs/opengraph/using-objects2. https://developers.facebook.com/docs/sharing /最佳实践

That should do the trick. Let me know if you need any more help.

这应该够了吧。如果您需要更多帮助,请告诉我。

I updated this answer, thanks to kitdesai for pointing out that Facebook has new requirements for their Graph API Version which is currently up to v2.1

我更新了这个答案,感谢 kitdesai 指出 Facebook 对其 Graph API 版本有新的要求,该版本目前最高为 v2.1

回答by jeffjv

I had this same issue and my problem was that the URL I had set for og:url had a '/' at the end and url of page I posted on facebook did not.

我遇到了同样的问题,我的问题是我为 og:url 设置的 URL 在末尾有一个“/”,而我在 facebook 上发布的页面的 url 没有。

The debugger just told me the og:type was not set yet it was. I believe the issue was that facebook's parser treated the mismatch as a redirect and got confused.

调试器只是告诉我 og:type 尚未设置。我相信问题在于 facebook 的解析器将不匹配视为重定向并感到困惑。

Anyway, making them match fixed my issue.

无论如何,让它们匹配解决了我的问题。