当前位置: 首页 > 工具软件 > quora-api > 使用案例 >

使用Facebook Graph API可以做的2件事

赫连宏伯
2023-12-01

In December 2011, Facebook officially deprecated its REST API and launched the Graph API for public use. Since then, all new features have been added to the Graph API and Facebook has enabled users to perform loads of new things, some of which we will discuss today. In this post, we will be making a lot of requests to the Graph API, receiving JSON responses, and thereby manipulating them to get our desired results.

2011年12月, Facebook正式弃用了REST API,并发布了供公众使用的Graph API 。 从那时起,所有新功能都已添加到Graph API中,Facebook使用户能够执行许多新事物,其中一些我们今天将进行讨论。 在本文中,我们将向Graph API发出大量请求,接收JSON响应,从而对其进行处理以获取所需的结果。

访问令牌 (The Access Token)

Most requests to the Graph API need an access token as a parameter. An access token is unique to the combination of a logged in user or page and the Facebook App that makes the request.

对Graph API的大多数请求都需要访问令牌作为参数。 访问令牌对于已登录的用户或页面以及发出请求的Facebook App而言是唯一的。

A token is associated with a Facebook app to handle the permissions that the user has granted to the app. This defines the resources that are accessible through the access token. Thus, a token provides temporary and secure access to Facebook. You can get an access token from the Graph Explorer. A token may or may not have an exipry time depending on whether they are short-term or long-term tokens. After the expiry of a short-term token, you need to re-authenticate the user and get a new token.

令牌与Facebook应用程序关联,以处理用户已授予该应用程序的权限。 这定义了可通过访问令牌访问的资源。 因此,令牌可提供对Facebook的临时安全访问。 您可以从Graph Explorer获得访问令牌。 令牌可以有或没有激励时间,这取决于它们是短期令牌还是长期令牌。 短期令牌到期后,您需要重新验证用户身份并获取新令牌。

通过Facebook页面比赛 (Contests Through Facebook Pages)

In recent times, small and up-and-coming organizations have used Facebook pages effectively to promote their content. Yet, getting ‘likes’ and thus, increasing your reach, is a slow and steady process. Many turn to Facebook ads for this purpose. Many others, though, take a cheaper alternative — by organizing contests through their page.

最近,新兴的小型组织有效地使用了Facebook页面来推广其内容。 但是,获得“喜欢”并因此增加覆盖面是一个缓慢而稳定的过程。 为此,许多人转向Facebook广告。 但是,许多其他公司则采取了更便宜的选择-通过在其页面上组织比赛。

The usual contest involves posting a photo or a paragraph on the page about an experience. The contest is judged on the basis of the number of ‘likes’ on the post or the photo. There’s also an extra condition for participating in the contest. For a ‘like’ to be valid, the user needs to like the page too. Facebook doesn’t have any built-in feature that tells you how many likes are common to your post and page. That makes judging the contests difficult.

通常的比赛包括在页面上发布有关体验的照片或段落。 比赛是根据帖子或照片上的“喜欢”次数来判断的。 参加比赛还有一个额外的条件。 为了使“喜欢”有效,用户也需要喜欢该页面。 Facebook没有任何内置功能可以告诉您您的帖子和页面共有多少个喜欢。 这使得评判比赛变得困难。

Non-programmers would feel that the only way to judge the contest is by cross checking the number of likes manually. Fortunately, the Graph API helps us perform this action without much hassle.

非程序员会认为,判断比赛的唯一方法是通过人工交叉检查喜欢的次数。 幸运的是,Graph API可以帮助我们轻松执行此操作。

Although I am going to perform the action through Python, the process remains the same for other languages. The important part is the target URLs that we send the requests to and the data we get from the received JSON.

尽管我将通过Python执行操作,但其他语言的过程仍然相同。 重要的部分是我们将请求发送到的目标URL和从接收到的JSON获得的数据。

A conceptually easy way to do this would be to get the list of likes on a post and the list of likes on a page and then compare them. However, there is no functionality in Facebook that gets the list of likes on a page as of now. We will use the reverse process to check whether each like on a post likes the page as well.

从概念上讲,做到这一点的简单方法是获取帖子上的喜欢列表和页面上的喜欢列表,然后进行比较。 但是, 到目前为止,Facebook中没有任何功能可以在页面上获取喜欢列表 。 我们将使用相反的过程来检查帖子中的每个喜欢的人是否也喜欢该页面。

The following call checks whether a user likes a page or not. The detailed documentation is available here.

以下呼叫检查用户是否喜欢页面。 详细文档可在此处获得

GET /{user-id}/likes/{page-id}

If the user likes the page, the JSON response contains data about the page, but if the user doesn’t like the page, an empty data is received. We use the following function to determine if a user likes a page or not.

如果用户喜欢该页面,则JSON响应包含有关该页面的data ,但是如果用户不喜欢该页面,则将接收到空data 。 我们使用以下功能来确定用户是否喜欢某个页面。

def user_likes_page(user_id, page_id):
    """
        Returns whether a user likes a page
    """
    url = 'https://graph.facebook.com/%d/likes/%d/' % (user_id, page_id)
    parameters = {'access_token': TOKEN}
    r = requests.get(url, params = parameters)
    result = json.loads(r.text)
    if result['data']:
        return True
    else:
        return False

The next step is to get the list of likes for a particular post and find out whether the users likes the page too. The following call gives us the list of likes for a post, provided we have proper access.

下一步是获取特定帖子的喜欢列表,并找出用户是否也喜欢该页面。 只要我们有适当的访问权限,以下电话就会给我们提供帖子的喜欢列表。

GET /{post-id}/likes/

Combining the two ideas, we make the following function to check how many likes in a post are common to the page too.

结合这两个想法,我们执行以下功能来检查帖子中有多少个喜欢的页面也是如此。

def get_common_likes(post_id, page_id):
    """
        Returns the number of likes common to a post and the page
    """
    count_likes = 0
    url = 'https://graph.facebook.com/%d/likes/' % post_id
    parameters = {'access_token': TOKEN}
    r = requests.get(url, params = parameters)
    result = json.loads(r.text)
    for like in result['data']:
        if user_likes_page(int(like['id']), page_id):
            count_likes += 1
            print 1
    return count_likes

大量响应您时间轴上的帖子 (Mass Responding to Posts on Your Timeline)

On your birthday, I am sure you get hundreds (if not thousands) of posts. Replying to each of them is tedious! Many people put up a status thanking everyone for their wishes, while others prefer to thank each one personally. Let us see how we can choose the personal option and do it in a short amount of time.

在您的生日那天,我相信您会收到数百(即使不是数千)的帖子。 回复每个人都很乏味! 许多人都建立了感谢每个人的愿望的状态,而另一些人则更愿意亲自感谢每个人。 让我们看看如何选择个人选项并在短时间内完成。

The call to get the feed for users or pages is as follows.

获取用户或页面供稿的调用如下。

GET /{user-id}/feed

In case you want to get the posts on your timeline, you can replace {user-id} with ‘me’, which makes the process look easier. To manipulate hundreds and thousands of posts, you would not be able to get them in a single page. You would need to go a step ahead and check the next url in the JSON response.

如果您想在时间轴上获取帖子,可以将{user-id}替换为“ me”,这使过程看起来更容易。 要操纵成千上万的帖子,您将无法在单个页面中获得它们。 您将需要进一步检查JSON响应中的next URL。

The function that gets all the posts on your timeline is as follows.

获取您时间轴上所有帖子的功能如下。

def get_posts():
    """
        Returns the list of posts on my timeline
    """

    parameters = {'access_token': TOKEN}
    r = requests.get('https://graph.facebook.com/me/feed', params=parameters)
    result = json.loads(r.text)
    return result['data']

The next step is to publish comments on your timeline. The call that is used to perform this action is as follows.

下一步是在时间轴上发布评论。 用于执行此操作的调用如下。

POST /{object-id}/comments

The comment should be sent as a message in the POST request above. So, the function that we use to comment on posts is as follows.

注释应在上面的POST请求中作为message发送。 因此,我们用来评论帖子的功能如下。

def comment_on_posts(posts):
    """Comments on all posts"""
    for post in posts:
        url = 'https://graph.facebook.com/%s/comments' % post['post_id']
        message = 'Commenting through the Graph API'
        parameters = {'access_token': TOKEN, 'message': message}
        s = requests.post(url, data = parameters)

The scripts that I have used for both of these can be found on GitHub. In addition, you can take it one step further by making multiple API requests at the same time.

我用于这两个脚本的脚本可以在GitHub上找到 。 此外,您可以通过同时发出多个API请求来将其进一步发展。

另一种方法 (An Alternative Approach)

Akshit Khurana, on Quora, discusses another approach to this through the use of Facebook Query Language (FQL). FQL is an SQL-like language that lets you query the data that you receive through the Graph API. There is a list of tables, each with its own list of columns that can be queried, thus helping you filter your data.

Quora上的Akshit Khurana 讨论了使用Facebook查询语言(FQL)的 另一种方法 。 FQL是一种类似于SQL的语言,可让您查询通过Graph API接收的数据。 有一个表列表 ,每个都有自己的列列表,可以查询这些列,从而帮助您过滤数据。

结论 (Conclusion)

Facebook has been working hard since the launch of the Graph API and new features are being added to it frequently. If you plan to work on mobile or web applications that are linked to Facebook, the use of the Graph API is a must. Facebook also maintains extensive documentation, which explains in detail the features and variety of uses of the Graph API.

自从Graph API推出以来,Facebook一直在努力工作,并且经常向其添加新功能。 如果您打算在链接到Facebook的移动或Web应用程序上工作,则必须使用Graph API。 Facebook还维护着广泛的文档 ,其中详细说明了Graph API的功能和用途。

翻译自: https://www.sitepoint.com/2-cool-things-can-facebook-graph-api/

 类似资料: