如何使用 Facebook 的 API 检查用户是否喜欢我的 Facebook 页面或 URL [英] How to check if a user likes my Facebook Page or URL using Facebook's API

查看:33
本文介绍了如何使用 Facebook 的 API 检查用户是否喜欢我的 Facebook 页面或 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得我快疯了.我无法让它工作.
我只是想检查用户是否在 iFrame 应用程序中使用 javascript 喜欢我的页面.

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 :(');
        }
    }
);

返回:False
但我是我的页面的粉丝,所以它不应该返回 true 吗?!

This returns: False
But I'm a fan of my page so shouldn't it return true?!

推荐答案

我也为这个问题而烦恼.您的代码只有在用户授予了不理想的扩展权限时才有效.

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.

这是另一种方法.

简而言之,如果您为 Canvas 高级选项打开 OAuth 2.0,Facebook 将发送一个 $_REQUEST['signed_request'] 以及其中请求的每个页面你的标签应用程序.如果您解析了 signed_request,您可以获得有关用户的一些信息,包括他们是否喜欢该页面.

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.

function parsePageSignedRequest() {
    if (isset($_REQUEST['signed_request'])) {
      $encoded_sig = null;
      $payload = null;
      list($encoded_sig, $payload) = explode('.', $_REQUEST['signed_request'], 2);
      $sig = base64_decode(strtr($encoded_sig, '-_', '+/'));
      $data = json_decode(base64_decode(strtr($payload, '-_', '+/'), true));
      return $data;
    }
    return false;
  }
  if($signed_request = parsePageSignedRequest()) {
    if($signed_request->page->liked) {
      echo "This content is for Fans only!";
    } else {
      echo "Please click on the Like button to view this tab!";
    }
  }

这篇关于如何使用 Facebook 的 API 检查用户是否喜欢我的 Facebook 页面或 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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