Mryqu's Notes


  • 首页

  • 搜索
close

Facebook Graph API合集

时间: 2016-01-04   |   分类: DataBuilder     |   阅读: 411 字 ~2分钟

常用URL笔记

获取Facebook主页Id

https://graph.facebook.com/v2.5/SasSoftware?access_token={accessToken}&format=json 常用URL笔记上面示例是通过主页名SasSoftware获取其主页Id。

获取Facebook主页帖子

  • https://graph.facebook.com/v2.5/{pageId}/feed?limit=100&format=json&include_hidden=true&access_token={accessToken}&since=2015-01-01&util=2015-12-31
  • https://graph.facebook.com/{pageId}/feed?limit=100&format=json&include_hidden=true&access_token={accessToken}&since=1420041660&util=1422634320
  • https://graph.facebook.com/v2.0/{pageId}/feed?limit=100&format=json&include_hidden=true&access_token={accessToken}&since=1420041660&util=1422634320

通过Facebook Graph API 2.5或不带版本的API仅能获取帖子的Id、创建时间和帖子内容,而FacebookGraph API 2.0则可以获得更多内容。

获取Facebook帖子的评论信息 https://graph.facebook.com/{postId}/comments?limit=100&format=json&include_hidden=true&access_token={accessToken}

获取Facebook帖子的点赞信息 https://graph.facebook.com/{postId}/likes?limit=100&format=json&include_hidden=true&summary=true&access_token={accessToken}

获取帖子订阅信息之limit参数

不同版本Facebook Graph API对获取帖子订阅信息 中limit参数要求不同:

  • v2.0及以下版本没有说明
  • v2.1、v2.2和v2.3版本上限为250
  • v2.4和v2.5版本上限为100Facebook Graph API获取帖子订阅信息之limit参数

处理Facebook API访问速率超限错误

对于下列Facebook通用错误,我个人觉的#2、#4、#9、#17、#18和#32错误都可以向客户端报告FacebookAPI访问速率超限,至于#5不确定。

Error numberPHP Constant nameError descriptionGenerated by methods
2API_EC_SERVICEService temporarily unavailable(all)
4API_EC_TOO_MANY_CALLSApplication request limit reached(all)
5API_EC_BAD_IPUnauthorized source IP address(all)
9API_EC_RATEUser is performing too many actions
17API_EC_USER_TOO_MANY_CALLSUser request limit reached
18API_EC_REQUEST_RESOURCES_EXCEEDEDThis API call could not be completed due to resourcelimits
32Page request limit reached

参考

  • Rate Limiting on the Facebook Graph API
  • Facebook API Error Codes for Developers

fields参数

使用Facebook Graph API进行查询,v2.3和v2.4版返回结果截然不同。使用v2.3之前的FacebookGraph API获得的响应信息很详细,而使用v2.4及之后的Facebook Graph API获得的响应基本没什么信息! 使用Facebook Graph API中的fields参数 使用Facebook Graph API中的fields参数 这可是相同的AccessToken呀。卖萌的话,可以说句“吓死宝宝了”。

Using the Graph API 提到可以使用fields参数选择所需字段,照着v2.3的加上了id、name、about、awards…… 使用Facebook Graph API中的fields参数 失而复得,虚惊一场!!!

Using the Graph API 里不仅提到了选择参数,还提到可以使用字段表达式进行嵌套查询。 使用Facebook Graph API中的fields参数 likes字段使用limit(1)限定其仅返回一个点赞数据,反正我还要使用GraphAPI对帖子点赞数据进行请求,返回一个点赞知道需不需要请求就好了。

comments字段还制定了二级字段attachment、id和from。这样comments字段既不会漏了我需要的子字段,也不会多出来我不需要的字段。在上图GraphAPI Explorer中,左侧comments字段下面的”Search for afield“链接可以提示那些子字段可选,很方便。

message_tags

message_tag是Facebook帖子和评论中消息标记的设置档,包括标记ID、文本、类型、偏移和长度。 今天才注意到Facebook帖子(Post )中message_tag是一个JSON对象,而评论(Comment )中message_tag是一个JSON数组。

帖子中的message_tag是这个样子的:

"message_tags": {
  "88": [
    {
      "id": "168597536563870",
      "name": "IBM",
      "type": "page",
      "offset": 88,
      "length": 3
    }
  ],
  "93": [
    {
      "id": "241760048297",
      "name": "Fidelity Investments",
      "type": "page",
      "offset": 93,
      "length": 20
    }
  ],
  "115": [
    {
      "id": "145619362306025",
      "name": "ABB",
      "type": "page",
      "offset": 115,
      "length": 3
    }
  ],
  "120": [
    {
      "id": "252467906271",
      "name": "Quintiles",
      "type": "page",
      "offset": 120,
      "length": 9
    }
  ],
  "131": [
    {
      "id": "193453547355388",
      "name": "SAS Software",
      "type": "page",
      "offset": 131,
      "length": 12
    }
  ],
  "161": [
    {
      "id": "702317053131576",
      "name": "Duke Energy",
      "type": "page",
      "offset": 161,
      "length": 11
    }
  ],
  "174": [
    {
      "id": "313176732094295",
      "name": "Toshiba Global Commerce Solutions",
      "type": "page",
      "offset": 174,
      "length": 33
    }
  ],
  "209": [
    {
      "id": "20531316728",
      "name": "Facebook",
      "type": "page",
      "offset": 209,
      "length": 8
    }
  ]
}

评论中的message_tag是这个样子的:

"message_tags": [
  {
    "id": "10101999889432459",
    "length": 16,
    "name": "Crystal Sullivan",
    "offset": 10,
    "type": "user"
  },
  {
    "id": "10152592598848593",
    "length": 16,
    "name": "Chris Hemedinger",
    "offset": 27,
    "type": "user"
  }
]

主页名

今天Alisa同学说搜中国湖南卫视的主页报错,我一直都只是用SASsoftware和我的YquTest做的开发测试。赶紧赶过去看了一眼,实验证明搜hntvchina好使,搜中国湖南卫视就会出错。 Facebook Graph API之主页名 感觉中国湖南卫视就像是hntvchina的显示名,而SASsoftware的显示名其实是SAS software。 又和小伙伴一起创建了一个主页玩玩,让我们输入的名字最后成了显示名,而真正生成的名字将空格用"-“代替然后又加了一串数字,感觉像防止主键冲突。

嵌套请求

今天又玩了一把Facebook Graph API。当我们抓取Page上的帖子后,之后会发起API请求获取帖子的评论及回复。

获取Page SASsoftware(ID为193453547355388)下的帖子

https://graph.facebook.com/193453547355388/feed?fields=id,XXXX,likes.limit(0).summary(1),comments,XXXX,with_tags&format=json&include_hidden=true&limit=100&since=XXXX&until=XXXX&access_token={YOUR_TOKEN}

FB:SASSoftware feed

获取帖子193453547355388_951786161522119的评论

https://graph.facebook.com/193453547355388_951786161522119/comments?fields=id,from,message,created_time,like_count&format=json&include_hidden=true&limit=100&access_token={YOUR_TOKEN}

FB:SASSoftware pageItemComment

获取评论951786161522119_951787458188656的回复

https://graph.facebook.com/951786161522119_951787458188656/comments?fields=id,from,message,created_time,like_count&format=json&include_hidden=true&limit=100&access_token={YOUR_TOKEN}

FB:SASSoftware pageItemCommentReply

试用嵌套请求

https://graph.facebook.com/193453547355388/feed?fields=id,XXXX,likes.limit(0).summary(1),comments{id,from,message,type,created_time,like_count,comments{id,from,message,type,created_time,like_count}},XXXX,with_tags&format=json&include_hidden=true&limit=100&since=XXXX&until=XXXX&access_token={YOUR_TOKEN}

这里的请求使用了两级嵌套请求,第一级获取帖子的评论,第二季获取评论的回复,那结果如何? FB:SASSoftware restedRequest 一个API请求就能够获得了帖子、评论及回复的信息。但是,考虑到一个帖子的评论或一个评论的回复都可能很多,返回结果是第一个分页结果,还是需要通过/{object-id}/comments API 请求获取,考虑到设计复杂性和性价比,决定放弃这种方案。

获取评论数量

通过fields参数获取评论/回复个数

通过预判评论/回复个数,以决定是否发起/{object-id}/comments API请求,可以显著减少API请求个数。

获取帖子的评论数

https://graph.facebook.com/{YOUR_PAGE_ID}/feed?fields=id,XXXX,likes.limit(0).summary(1),comments.limit(0).summary(1),XXXX,with_tags&format=json&include_hidden=true&limit=100&since=XXXX&until=XXXX&include_hidden=true&access_token={YOUR_ACCESS_TOKEN}

Get comments count using Facebook Graph API

获取评论的回复数

https://graph.facebook.com/{YOUR_POST_ID}/comments?fields=id,from,message,created_time,like_count,comments.limit(0).summary(1)&format=json&include_hidden=true&limit=100&since=XXXX&until=XXXX&include_hidden=true&access_token={YOUR_ACCESS_TOKEN}

Get comments count using Facebook Graph API

comments.summary.total_count解读

在Facebook Graph指南中/{object-id}/comments 提到了total_count数值是随filter变动的。

  • filter为stream,total_count为该节点下所有评论及其回复的总数;
  • filter为toplevel,total_count为该节点下顶层评论/回复的总数。

由于在我的使用场景中为设置filter,而其默认值为toplevel,则total_count为该节点下顶层评论/回复的总数。

通过fields参数获取点赞数量

原来获得Facebook帖子/评论的点赞数,需要额外单独发送一次API请求: Get likes count using fields parameters in Facebook Graph API Get likes count using fields parameters in Facebook Graph API

通过对Facebook GraphAPI中fields参数添加likes.limit(0).summary(1),仅需一次API请求就可获得帖子/评论的所有信息: Get likes count using fields parameters in Facebook Graph API

点赞数

现在Facebook帖子上有一个心情计数,下面这个湖南卫视的帖子可以看到总共有293个。 Facebook Graph API之点赞数 通过Facebook Graph API获得的点赞数为275个。 Facebook Graph API之点赞数 示例里的帖子心情计数293包括点赞数275、大爱数13、笑趴数4、【未知表情计数】1。 Facebook Graph API之点赞数 这里可以看到点赞可以细分为点赞、大爱、笑趴、哇、心碎和怒这六种表情。 Facebook Graph API之点赞数

分享数

现在Facebook帖子上有一个分享计数,下面这个湖南卫视的帖子可以看到总共有3个。 Facebook Graph API之分享数 点进去可以查看这3个分享的具体情况: Facebook Graph API之分享数 使用Facebook Graph Explorer却可以得到分享数为7: Facebook Graph API之分享数 差4个分享,有点醉! 其实我刚才刚刚用消息给朋友共享了这个帖子,由于是不是公开的,所以Facebook网页显示上是不计入的,但是API却是统计的。 Facebook Graph API之分享数

获取表情

Facebook Enhances Everyone’s Like With Love, Haha, Wow, Sad, Angry Buttons. Fb Reaction For example, there are two reactions in the post 778979332230227: Fb Reaction Post Sample Your can use Facebook Graph API - Reading Reactions to get the reactions data. Note: The API version must be 2.6 or higher. FbApi25 on Reaction FbApi26 on Reaction You also can get all reactions count of one post in single Graph API request: FbApi getReactionCount

Facebook根据请求API版本返回不同内容?

今天跟测试组同事研究Facebook返回内容时,发现一个奇怪现象:抓取湖南卫视的Page,当API版本为2.3至2.8时,返回结果内容为空;而API版本为2.9时,返回有内容的响应。 FB API 2.7 Response FB API 2.9 Response

标题:Facebook Graph API合集
作者:mryqu
声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 CN 许可协议。转载请注明出处!

#facebook# #graph# #api#
为cURL库设置HTTP代理的代码片段
[C++]用正则表达式检查日期格式yyyy-MM-dd
  • 文章目录
  • 站点概览

Programmer & Architect

662 日志
27 分类
1472 标签
GitHub Twitter FB Page
    • 常用URL笔记
    • 获取帖子订阅信息之limit参数
    • 处理Facebook API访问速率超限错误
    • fields参数
    • message_tags
    • 主页名
    • 嵌套请求
    • 获取评论数量
    • 通过fields参数获取点赞数量
    • 点赞数
    • 分享数
    • 获取表情
    • Facebook根据请求API版本返回不同内容?
© 2009 - 2023 Mryqu's Notes
Powered by - Hugo v0.120.4
Theme by - NexT
0%