facebook graph api从2.6转到2.8的问题处理

之前一直用facebook2.6的api,现在想升级到2.8.但是2.8的api做了一些调整。有些api的参数已经不一样了。

各个版本的api的停用时间表见:

https://developers.facebook.com/docs/apps/changelog

版本路径推出日期停用日期

v2.8

/v2.8/{object}

2016年10月5日

至少到2018年10月

v2.7

/v2.7/{object}

2016年7月13日

2018年10月5日

v2.6

/v2.6/{object}

2016年4月12日

2018年7月13日

v2.5

/v2.5/{object}

2015年10月7日

2018年4月12日

v2.4

/v2.4/{object}

2015年7月8日

2017年10月7日

v2.3

/v2.3/{object}

2015年3月25日

2017年7月8日

v2.2

/v2.2/{object}

2014年10月30日

2017年3月25日

v2.1

/v2.1/{object}

2014年8月7日

2016年10月30日

v2.0

/v2.0/{object}

2014年4月30日

2016年8月7日起停用

v1.0

/{object}

2010年4月21日

2015年4月30日起停用

例如之前获取postinsight的api

https://graph.facebook.com/v2.6/page_id/insights?debug=all&access_token=xxx&format=json&method=get

是可以获取到postinsight的所有数据的。而改成2.8则会报错:

{
  "error": {
    "message": "Invalid query",
    "type": "OAuthException",
    "code": 3001,
    "error_subcode": 1504028,
    "is_transient": false,
    "error_user_title": "No Metric Specified",
    "error_user_msg": "No metric was specified to be fetched. Please specify one or more metrics to be fetched and try again.",
    "fbtrace_id": "AnenpDXtC4N"
  },
  "__debug__": {
  }
}

必须传递metric参数。

我们可以在图谱explore中试下

https://developers.facebook.com/tools/explorer/?method=GET&path=xxx%2Finsights%2F%3Fmethod%3Dget%26format%3Djson%26access_token%xxx%26debug%3Dall&version=v2.8

metriclist:

https://developers.facebook.com/docs/graph-api/reference/v2.8/insights#metrics

例如我要获取post_impressions_organic和post_reactions_by_type_total。则url加上

https://graph.facebook.com/v2.8/page_id/insights/["post_impressions_organic","post_reactions_by_type_total"]?debug=all&format=json&method=get

这样就能返回数据了..

需要注意的是,since和until的时间戳应该用UTC时间。建议时间差异最大为6个月。

https://developers.facebook.com/docs/graph-api/using-graph-api/v2.1

{
  "data": [
    {
      "name": "post_impressions_by_paid_non_paid",
      "period": "lifetime",
      "values": [
        {
          "value": {
            "total": 123609,
            "unpaid": 123609,
            "paid": 0
          }
        }
      ],
      "title": "Lifetime Post impressions by paid and non-paid",
      "description": "Lifetime: The number of impressions of your Page post broken down by paid and non-paid. (Total Count)",
      "id": "xxx/insights/post_impressions_by_paid_non_paid/lifetime"
    },
    {
      "name": "post_story_adds_by_action_type",
      "period": "lifetime",
      "values": [
        {
          "value": {
            "like": 822,
            "comment": 66,
            "share": 39
          }
        }
      ],
      "title": "Lifetime Post Stories by action type",
      "description": "Lifetime: The number of stories created about your Page post, by action type. (Total Count)",
      "id": "xxx/insights/post_story_adds_by_action_type/lifetime"
    },
    {
      "name": "post_video_views",
      "period": "day",
      "values": [
        {
          "value": 0,
          "end_time": "2017-02-11T08:00:00+0000"
        },
        {
          "value": 0,
          "end_time": "2017-02-12T08:00:00+0000"
        },
        {
          "value": 0,
          "end_time": "2017-02-13T08:00:00+0000"
        }
      ],
      "title": "Daily Total Video Views",
      "description": "Daily: Total number of times your video was viewed for more than 3 seconds. (Total Count)",
      "id": "xxx/insights/post_video_views/day"
    },
    {
      "name": "post_video_views",
      "period": "lifetime",
      "values": [
        {
          "value": 36
        }
      ],
      "title": "Lifetime Total Video Views",
      "description": "Lifetime: Total number of times your video was viewed for more than 3 seconds. (Total Count)",
      "id": "xxx/insights/post_video_views/lifetime"
    },
    {
      "name": "post_consumptions_by_type",
      "period": "lifetime",
      "values": [
        {
          "value": {
            "other clicks": 6326,
            "link clicks": 8833
          }
        }
      ],
      "title": "Lifetime Post Consumptions by type",
      "description": "Lifetime: The number of clicks anywhere in your post, by type. (Total Count)",
      "id": "xxx/insights/post_consumptions_by_type/lifetime"
    },
    {
      "name": "post_reactions_by_type_total",
      "period": "lifetime",
      "values": [
        {
          "value": {
            "like": 770,
            "love": 21,
            "wow": 0,
            "haha": 29,
            "sorry": 1,
            "anger": 1
          }
        }
      ],
      "title": "Lifetime Total post Reactions by Type.",
      "description": "Lifetime: Total post reactions by type.",
      "id": "xxx/insights/post_reactions_by_type_total/lifetime"
    }
  ],
  "paging": {
    "previous": "https://graph.facebook.com/v2.8/xxx/insights/['post_impressions_by_paid_non_paid','post_story_adds_by_action_type','post_video_views','post_consumptions_by_type','post_reactions_by_type_total']/?access_token=xxx&debug=all&format=json&method=get&pretty=0&suppress_http_code=1&since=1486458350&until=1486717550",
    "next": "https://graph.facebook.com/v2.8/xxx/insights/['post_impressions_by_paid_non_paid','post_story_adds_by_action_type','post_video_views','post_consumptions_by_type','post_reactions_by_type_total']/?access_token=xxx&debug=all&format=json&method=get&pretty=0&suppress_http_code=1&since=1486976750&until=1487235950"
  },
  "__debug__": {
  }
}

相关推荐