将用于 URL 共享计数的 Facebook API 从 v1.0 迁移到 v2.0+ [英] Migrating Facebook API for URL share count from v1.0 to v2.0+

查看:34
本文介绍了将用于 URL 共享计数的 Facebook API 从 v1.0 迁移到 v2.0+的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在维护 WordPress Social Metrics Tracker 插件.该插件向各种社交网络 API(包括 Facebook)发出 GET 请求,以检索共享 URL 的次数.

此插件目前使用 Facebook API v1.0,该 API 已弃用,将于 4 月删除2015 年 30 月.我需要迁移到更新版本的 Facebook API,但似乎 Facebook 已锁定他们的 API 以要求访问令牌.

需要来自 Facebook 的以下数据:

  • 给定网址的分享次数
  • 给定网址的点赞数
  • 给定 URL 的评论数量

问题:

从 Facebook 检索这些数据的最佳方法是什么,它需要插件最终用户的最低级别的身份验证,并且至少使用 API 版本 2.0?

或者,如果需要身份验证,我可以避免要求用户登录 Facebook 以获取身份吗?

注意事项:

因为这是一个开源的 WordPress 插件,它被分发到用户服务器,任何应用程序机密或其他敏感数据都不能存在于插件源代码中的任何地方.

插件的用户目前不需要登录"或注册"——插件开箱即用.保持事情如此简单会很棒,但是如果要求用户登录 Facebook"是从 Facebook API 检索数据的唯一方法,那么就这样吧.

如果我们确实需要检索身份验证令牌,它需要在服务器的后台继续工作,而不必提示用户重新进行身份验证.

现有 API 请求示例:

对此 Facebook API 端点的 GET 请求效果很好并返回数据:

API v1.0 链接示例(有效 - 当前由插件使用)

但是,当我们将版本切换到 v2.0 时,我们被告知资源需要访问令牌:

API v2.0 链接示例(已损坏)

当我们切换到最新版本 v2.3 时,我们了解到 FQL 查询已从 API 中删除:

API v2.3 链接示例(已损坏)

解决方案

您要查找的端点是 /?id={url}.Graph API v2.3 为此至少需要一个应用程序访问令牌,这意味着您的插件用户必须先创建 Facebook 应用程序才能使用此端点.这可以通过 https://developers.facebook.com/apps/

完成

示例调用是

https://graph.facebook.com/v2.3/?access_token={app_access_token}&id=http://www.wikipedia.org

导致

<代码>{og_object":{"id": "382267859091","title": "维基百科","类型": "网站","updated_time": "2015-04-14T23:28:48+0000","url": "http://www.wikipedia.org/"},分享": {评论计数":0,share_count":195105},"id": "http://www.wikipedia.org"}

正如您所注意到的,缺少点赞.

您可以通过使用返回的 og_object.id 发出第二次调用来获得这些:

https://graph.facebook.com/v2.3/382267859091?fields=likes.summary(true).limit(0)&access_token={app_access_token}

导致

<代码>{喜欢":{数据": [],概括": {total_count":1298}},id":382267859091"}

您也可以在一个 Batch 请求中执行此操作:

POST https://graph.facebook.com/v2.3

字段 batch 设置为

<预><代码>[{"方法": "获取","name": "get-url-stats","relative_url": "v2.3/?id=http://www.wikipedia.org",omit_response_on_success":假},{"方法": "获取","name": "喜欢","relative_url": "v2.3/{result=get-url-stats:$.og_object.id}?fields=likes.summary(true).limit(0)"}]

以及带有您的应用程序访问令牌的字段 access_token.这将返回一个像这样的冗长响应(为简洁起见省略了标头):

<预><代码>[{代码":200,标题":[...],"body": "{\n \"og_object\": {\n \"id\": \"382267859091\",\n \"title\": \"Wikipedia\",\n \"type\": \"网站\",\n \"updated_time\": \"2015-04-14T23:28:48+0000\",\n \"url\": \"http://www.wikipedia.org/\"\n },\n \"share\": {\n \"comment_count\": 0,\n \"share_count\": 195105\n },\n \"id\": \"http://www.wikipedia.org\"\n}"},{代码":200,标题":[...],"body": "{\n \"likes\": {\n \"data\": [\n \n ],\n \"summary\": {\n \"total_count\": 1298\n}\n },\n \"id\": \"382267859091\"\n}"}]

您必须将每个 body 属性解析为 JSON,然后使用数据来创建/显示您的统计数据.

I am maintaining the WordPress Social Metrics Tracker plugin. The plugin makes GET requests to various social network APIs, including Facebook, to retrieve the number of times a URL has been shared.

This plugin currently uses v1.0 of the Facebook API which is deprecated and will be removed on April 30, 2015. I need to migrate to a newer version of the Facebook API, however it seems that Facebook has locked down their API to require access tokens.

The following data from Facebook is required:

  • Number of Shares for a given URL
  • Number of Likes for a given URL
  • Number of Comments for a given URL

The Question:

What is the best way to retrieve this data from Facebook that requires the least level of authentication from the end-user of the plugin, and that uses at least API version 2.0?

Or, if authentication is required, can I avoid asking the user to sign in to Facebook to get it?

Considerations:

Because this is an open source WordPress plugin which gets distributed to user servers, no app secret or other sensitive data can live in the plugin source code anywhere.

Users of the plugin do not currently need to "sign in" or "sign up" - the plugin just works out of the box. It would be great to keep things that simple, however if asking users to "sign in to Facebook" is the only way to retrieve data from the Facebook API then so be it.

If we do need to retrieve an auth token, it will need to keep working in the background on the server and not have to prompt the user to re-authenticate.

Existing API request examples:

A GET request to this Facebook API endpoint works great and returns data:

API v1.0 link example (works - currently used by plugin)

However, when we switch the version to v2.0 we are informed that an access token is required for the resource:

API v2.0 link example (broken)

And when we switch to v2.3, the latest version, we learn that the FQL queries have been removed from the API:

API v2.3 link example (broken)

解决方案

The endpoint you're looking for is /?id={url}. The Graph API v2.3 requires at least an App Access Token for this, which would mean that your plugins' users must create an Facebook App first before being able to use this endpoint. This can be done via https://developers.facebook.com/apps/

A sample call would be

https://graph.facebook.com/v2.3/?access_token={app_access_token}&id=http://www.wikipedia.org

which results in

{
   "og_object": {
      "id": "382267859091",
      "title": "Wikipedia",
      "type": "website",
      "updated_time": "2015-04-14T23:28:48+0000",
      "url": "http://www.wikipedia.org/"
   },
   "share": {
      "comment_count": 0,
      "share_count": 195105
   },
   "id": "http://www.wikipedia.org"
}

As you noticed, the likes are missing.

You can get these by issueing a second call by using the returned og_object.id like this:

https://graph.facebook.com/v2.3/382267859091?fields=likes.summary(true).limit(0)&access_token={app_access_token}

which results in

{
  "likes": {
    "data": [
    ], 
    "summary": {
      "total_count": 1298
    }
  }, 
  "id": "382267859091"
}

You can also do this in one Batch request:

POST https://graph.facebook.com/v2.3

with the field batch set to

[
    {
        "method": "GET",
        "name": "get-url-stats",
        "relative_url": "v2.3/?id=http://www.wikipedia.org",
        "omit_response_on_success": false
    },
    {
        "method": "GET",
        "name": "likes",
        "relative_url": "v2.3/{result=get-url-stats:$.og_object.id}?fields=likes.summary(true).limit(0)"
    }
]

and the fields access_token with your App Access Token. This returns a lengthy response like this (headers omitted for brevity):

[
  {
    "code": 200, 
    "headers": [
      ...
    ], 
    "body": "{\n   \"og_object\": {\n      \"id\": \"382267859091\",\n      \"title\": \"Wikipedia\",\n      \"type\": \"website\",\n      \"updated_time\": \"2015-04-14T23:28:48+0000\",\n      \"url\": \"http://www.wikipedia.org/\"\n   },\n   \"share\": {\n      \"comment_count\": 0,\n      \"share_count\": 195105\n   },\n   \"id\": \"http://www.wikipedia.org\"\n}"
  }, 
  {
    "code": 200, 
    "headers": [
      ...
    ], 
    "body": "{\n   \"likes\": {\n      \"data\": [\n         \n      ],\n      \"summary\": {\n         \"total_count\": 1298\n      }\n   },\n   \"id\": \"382267859091\"\n}"
  }
]

You have to parse each body property as JSON, and then use the data to create/show your stats.

See

这篇关于将用于 URL 共享计数的 Facebook API 从 v1.0 迁移到 v2.0+的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆