如何使用 Facebook 的 API 检查用户是否喜欢我的 Facebook 页面或 URL
我想我快要疯了。我无法让它工作。
我只想检查用户是否喜欢在 iFrame
应用程序中使用 javascript 的我的页面。
FB.api({
method: "pages.isFan",
page_id: my_page_id,
}, function(response) {
console.log(response);
if(response){
alert('You Likey');
} else {
alert('You not Likey :(');
}
}
);
返回:False
但我是我的页面的粉丝,所以它不应该返回 true 吗?!
I think I'm going crazy. I can't get it to work.
I simply want to check if a user has liked my page with javascript in an iFrame
app.
FB.api({
method: "pages.isFan",
page_id: my_page_id,
}, function(response) {
console.log(response);
if(response){
alert('You Likey');
} else {
alert('You not Likey :(');
}
}
);
This returns: False
But I'm a fan of my page so shouldn't it return true?!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我也为这件事撕扯了我的头发。仅当用户授予了不理想的扩展权限时,您的代码才有效。
这里是 简而言之
,如果您打开 Canvas 高级选项的
OAuth 2.0
,Facebook 将发送$_REQUEST['signed_request']
以及选项卡应用程序中请求的每个页面。如果您解析该signed_request,您可以获得有关用户的一些信息,包括他们是否喜欢该页面。I tore my hair out over this one too. Your code only works if the user has granted an extended permission for that which is not ideal.
Here's another approach.
In a nutshell, if you turn on the
OAuth 2.0
for Canvas advanced option, Facebook will send a$_REQUEST['signed_request']
along with every page requested within your tab app. If you parse that signed_request you can get some info about the user including if they've liked the page or not.您可以使用 (PHP)
这将返回以下三个之一:
我猜唯一不使用 token 的方法来实现这一点是使用刚刚发布的signed_request Jason Siffring 。我使用 PHP SDK 的助手:
You can use (PHP)
That will return one of three:
I guess the only not-using-token way to achieve this is with the signed_request Jason Siffring just posted. My helper using PHP SDK:
您可以像这样在 JavaScript 中执行此操作(基于 @dwarfy 对类似问题的回答):
服务器上的channel.html 文件仅包含以下行:
那里有一些代码重复,但您明白了。当用户第一次访问该页面时,这将弹出一个登录对话框(这并不完全理想,但可行)。但在随后的访问中,应该不会出现任何内容。
You can do it in JavaScript like so (Building off of @dwarfy's response to a similar question):
Where the channel.html file on your server just contains the line:
There is a little code duplication in there, but you get the idea. This will pop up a login dialog the first time the user visits the page (which isn't exactly ideal, but works). On subsequent visits nothing should pop up though.
虽然这篇文章已经发布了一段时间,但解决方案并不是纯 JS。尽管 Jason 指出请求权限并不理想,但我认为这是一件好事,因为用户可以明确拒绝它。我仍然发布这段代码,尽管(几乎)同样的事情也可以在 ifaour 的另一篇文章中看到。将此视为纯 JS 版本,无需过多关注细节。
基本代码相当简单:
或者,将
me
替换为其他人的正确 UserID(您可能需要更改下面的权限才能执行此操作,例如friends_likes
) ,您需要的不仅仅是基本权限:Though this post has been here for quite a while, the solutions are not pure JS. Though Jason noted that requesting permissions is not ideal, I consider it a good thing since the user can reject it explicitly. I still post this code, though (almost) the same thing can also be seen in another post by ifaour. Consider this the JS only version without too much attention to detail.
The basic code is rather simple:
ALternatively, replace
me
with the proper UserID of someone else (you might need to alter the permissions below to do this, likefriends_likes
) As noted, you need more than the basic permission:当用户按下“喜欢”按钮时,我使用 jquery 发送数据。
注意:您可以使用一些隐藏的输入文本来获取按钮的 id。在我的例子中,我从“var fbl_id=h_fbl[4];”中的 url 本身获取它。因为有 id 示例:
网址:
http://mywebsite.com/post/22/some-tittle
所以我解析url 来获取 id,然后将其插入到我的数据库中的 like.php 文件中。
这样你不需要请求权限来知道是否有人按下了“喜欢”按钮,但如果你想知道谁按下了它,则需要权限。
i use jquery to send the data when the user press the like button.
Note:you can use some hidden input text to get the id of your button.in my case i take it from the url itself in "var fbl_id=h_fbl[4];" becasue there is the id example:
url:
http://mywebsite.com/post/22/some-tittle
so i parse the url to get the id and then insert it to my databse in the like.php file.
in this way you dont need to ask for permissions to know if some one press the like button, but if you whant to know who press it, permissions are needed.