Facebook Graph API没有返回特定帖子的所有字段

时间:2016-02-11 16:51:53

标签: python facebook-graph-api

我正在下载公共页面的所有帖子。例如,来自Facebook的Google页面的最新帖子如下所示:

{
        "created_time": "2016-02-04T02:55:11+0000",
        "message": "We hope you\u2019re just as excited about the Year of the Monkey as we are!  So excited, we\u2019ve been hunting around Street View for monkey sightings. Here are a few we found so far...want to help us find more 'Monkeys of StreetView'? #monkeyview goo.gl/8LhqMe",
        "story": "Google added 4 new photos to the album: Monkeys of StreetView.",
        "id": "104958162837_10153902425172838"
    }

这个json对象是以下代码返回的帖子之一:(代码来自其他堆栈溢出页面,但发布链接有限制)

from facepy import GraphAPI
import json
page_id = "Google"

access_token = "AccessToken"

graph = GraphAPI(access_token)
data = graph.get(page_id + "/feed", page=False, retry=3, limit=100)

with open('content.json', 'w') as outfile:
     json.dump(data, outfile, indent = 4)

这篇文章应该包含更多字段。例如,使用与此帖子相关联的ID,我使用此链接并查看其他字段。例如,此帖子中有多张图片,“here”将返回:

{    “picture”:“https://graph.facebook.com/v2.4/104958162837_10153902425172838?fields=picture&access_token=AccessToken”,    “id”:“104958162837_10153902425172838” }

同样,它还有其他字段未在代码结果中显示。它应该例如也显示这篇文章的喜欢。 所以我有两个问题:

  1. 为什么结果没有显示所有字段,尽管这些字段存在并且可以通过查询该帖子的ID找到

  2. 这篇文章有多张图片,但我们只收到其中一张图片,当我查询喜欢和评论的数量而不是回复帖子的评论或评论时,会将其返回给某张图片。< / p>

  3. 谢谢。

1 个答案:

答案 0 :(得分:0)

  1. 您的第一个问题的答案是Facebook Graph API默认只返回字段的子集。如果您需要不同的字段,则需要指定所需的字段。 the Graph API docs
  2. 中提到了这一点
      

    默认情况下,并非您返回节点或边缘中的所有字段   进行查询。您可以选择要返回的字段(或边)   使用“fields”查询参数。这对制作非常有用   您的API调用更加高效和快速。

    在您的情况下,您可以将呼叫更改为以下内容以检索(例如)消息和图片字段

    graph.get(page_id + "/feed", page=False, retry=3, limit=100, fields="message,picture")
    
    1. 帖子的图片字段只会返回单张图片(The picture scraped from any link included with the post.)。要检索所有图片,请使用attachments edge,它将返回所有附加图像(或视频等)的列表。