简体   繁体   English

Facebook Like Button-是否选中?

[英]Facebook Like Button - Is It Checked?

I'm having trouble finding good documentation about how to use javascript in order to find out if a like button is clicked or not. 我在查找有关如何使用javascript的良好文档方面遇到麻烦,以便找出是否单击了喜欢的按钮。 I can't use an on-click event because the button might already be checked when a user comes onto my page but that seems to be all I can find regarding tracking likes externally (not counting other methods that are no longer supported). 我无法使用on-click事件,因为当用户进入我的页面时,该按钮可能已经被选中,但这似乎是我从外部跟踪喜欢的全部内容(不包括不再支持的其他方法)。 Anyone have any experience with such issues? 任何人都有处理此类问题的经验吗? Thanks. 谢谢。

It depends on the Like button. 这取决于“赞”按钮。 If it's for a Facebook object that has an ID, you can query the like table (but you'll need user_likes permission) 如果用于具有ID的Facebook对象,则可以查询like表(但需要user_likes权限)

If it's for a URL, it's not really possible: facebook graph api determine if user likes url [stackoverflow] 如果用于URL,则实际上是不可能的: facebook graph api确定用户是否喜欢url [stackoverflow]

It it's for a built-in like you could use a cookie to remember the click, or save the click in your own database. 它是内置的,就像您可以使用Cookie来记住点击或将点击保存在自己的数据库中一样。

Just to expand on Gil's answer. 只是为了扩展Gil的答案。 For built-in likes you can use a batch request consisting of two request: 1) Try to like the object in question 2) If there was no error, delete the like connection 对于内置的喜欢,您可以使用包含两个请求的批处理请求:1)尝试喜欢有问题的对象2)如果没有错误,请删除喜欢的连接

Example batch: 批处理示例:

[{"method":"POST", "relative_url":"me/og.likes", "body":"object=<SOME_URL>", "name":"like-attempt", "omit_response_on_success": false},
{"method":"DELETE", "relative_url":"{result=like-attempt:$.id}"}]

If an object was already liked, batch response would be: 如果某个对象已经被喜欢,则批处理响应将是:

[
    {
        "code": 400,
        "headers": [
            ...
            {
                "name": "WWW-Authenticate",
                "value": "OAuth \"Facebook Platform\" \"invalid_request\" \"(#3501) User is already associated to the object object on a unique action type Like. Original Action ID: 143539809123515\""
            }
        ],
        "body": "{\n \"error\": {\n \"message\": \"(#3501) User is already associated to the object object on a unique action type Like. Original Action ID: 143539809123515\",\n \"type\": \"OAuthException\",\n \"code\": 3501\n }\n}"
    },
    null
]

So to check if object was liked, JSON.parse the first response body and look for an error with error code 3501 . 因此,要检查是否喜欢该对象,请JSON.parse第一个响应主体,并查找错误代码为3501错误。 Keep in mind that the actual error code is not documented anywhere so it could change, although I don't think it is likely. 请记住,实际的错误代码没有记录在任何地方,因此它可能会更改,尽管我认为这不太可能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM