迁移Facebook的API的网址股数从V1.0到V2.0 + [英] Migrating Facebook API for URL share count from v1.0 to v2.0+

查看:198
本文介绍了迁移Facebook的API的网址股数从V1.0到V2.0 +的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我保持字preSS社会指标跟踪插件。该插件使GET请求到各种社交网络的API,包括Facebook,检索的次数网址已被分享的次数。

此插件目前使用Facebook的API,它的v1.0是德precated和意志在二○一五年四月三十〇日被删除。我需要迁移到Facebook的API的更新版本,但它似乎Facebook已经锁定了他们的API需要的访问令牌。

从Facebook以下数据是必需的:


  • 股份为给定的URL号

  • 喜欢的一个给定的URL号

  • 注释对于给定的URL号

问题:

什么是检索从Facebook这个数据需要验证从插件的最终用户的满级的最佳方式,并至少使用API​​ 2.0版本?

或者,如果需要身份验证,可我避免要求用户登录到Facebook来得到它?

注意事项:

由于这是一个开源的Word preSS插件,被分发到用户服务器,应用程序没有秘密或其他敏感数据可以在任何地方居住在插件的源$ C ​​$ C。

该插件的用户目前并不需要登录或注册 - 插件只是工作开箱。这将是伟大的,让事情变得简单,但是如果要求用户登录到Facebook的为检索来自Facebook的API数据,那么就这样吧的唯一途径。

如果我们确实需要检索身份验证令牌,它需要保留在服务器上的后台运行,而不必提示用户重新进行身份验证。

现有API请求的例子:

一个GET请求,这个Facebook的API端点的伟大工程,并返回数据:

<一个href=\"https://graph.facebook.com/v1.0/fql?q=SELECT%20url,%20share_count,%20like_count,%20comment_count,%20total_count,%20click_count%20FROM%20link_stat%20where%20url=%27http://www.wikipedia.org%27\"相对=nofollow> API v1.0的链接例子(作品 - 目前使用的插件)

然而,当我们的版本切换到2.0版,我们被告知的访问令牌是必需的资源:

<一个href=\"https://graph.facebook.com/v2.0/fql?q=SELECT%20url,%20share_count,%20like_count,%20comment_count,%20total_count,%20click_count%20FROM%20link_stat%20where%20url=%27http://www.wikipedia.org%27\"相对=nofollow> API 2.0版链接例子(碎)

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

<一个href=\"https://graph.facebook.com/v2.3/fql?q=SELECT%20url,%20share_count,%20like_count,%20comment_count,%20total_count,%20click_count%20FROM%20link_stat%20where%20url=%27http://www.wikipedia.org%27\"相对=nofollow> API v2.3的链接例子(碎)


解决方案

您正在寻找的终点是 /?ID = {URL} 。该图形API V2.3至少需要一个App访问令牌这一点,这将意味着你的插件的用户首先必须能够使用此端点之前创建一个Facebook应用。这可以通过 https://developers.facebook.com/apps/

完成

一个示例调用是

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

这导致

  {
   og_object:{
      ID:382267859091,
      头衔:维基百科,
      类型:网站,
      updated_time:2015-04-14T23:28:48 + 0000,
      URL:http://www.wikipedia.org/
   },
   分享:{
      COMMENT_COUNT:0,
      SHARE_COUNT:195105
   },
   ID:http://www.wikipedia.org
}

当你注意到了,喜欢丢失。

您可以通过使用返回 issueing第二个电话让这些og_object.id 是这样的:

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

这导致

  {
  喜欢:{
    数据:
    ]
    摘要:{
      TOTAL_COUNT:1298
    }
  },
  ID:382267859091
}

您也可以做到这一点在一个批处理请求:

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

与现场批量设置为

  [
    {
        方法:GET,
        名:得-URL-统计
        relative_url:V2.3 / ID = HTTP://www.wikipedia.org
        omit_response_on_success:假的
    },
    {
        方法:GET,
        名:喜欢,
        relative_url:V2.3 / {结果=获得-URL-统计:$ og_object.id}域= likes.summary(真).limit(0)?
    }
]

和领域的access_token 您的应用程序访问令牌。这将返回这样一个漫长的响应(头不再赘述)

  [
  {
    code:200,
    头:
      ...
    ]
    身:{\\ n \\og_object \\:{\\ n \\ID \\:\\382267859091 \\,\\ n \\称号\\:\\维基百科\\,\\ n \\类型\\ :\\网站\\,\\ n \\updated_time \\:\\2015-04-14T23:28:48 + 0000 \\,\\ n \\URL \\:\\http://www.wikipedia.org / \\\\ n} \\ n \\共享\\:{\\ n \\COMMENT_COUNT \\:0 \\ n \\SHARE_COUNT \\:195105 \\ n} \\ n \\ID \\:\\HTTP ://www.wikipedia.org \\\\ n}
  },
  {
    code:200,
    头:
      ...
    ]
    身:{\\ n \\喜欢\\:{\\ n \\数据\\:[\\ n \\ n] \\ n \\总结\\:{\\ n \\TOTAL_COUNT \\:1298 \\ n } \\ n} \\ n \\ID \\:\\382267859091 \\\\ n}
  }
]

您必须解析每个属性作为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

这篇关于迁移Facebook的API的网址股数从V1.0到V2.0 +的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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