通过 Graph API 检查用户是否喜欢页面 [英] Check if user likes page via Graph API

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

问题描述

我看过一些类似的问题,但我认为这是新问题...

I have seen a few similar questions but I think this is new...

我正在尝试在博客上添加一个弹出框,其中包含 Facebook 页面的 Facebook 赞按钮,并且仅在用户不喜欢 Facebook 页面时才会显示.

I am trying to add a popover on a blog that will contain a Facebook like button for a Facebook page and wil only show if the user does not already like the Facebook page.

通读文档,这应该可以通过 Graph API pages.isFan 方法实现,但这是否需要用户授予权限?如果是这样,是否有可能没有应用请求权限而是优雅地失败?

Reading through the documentation this should be achievable with the Graph API pages.isFan method but does this require the user to give permissions? If so would it be possible to not have the app request permissions but rather fail gracefully?

非常感谢任何帮助.

推荐答案

您可以从 facebook 的请求中发送的 signed_request 中判断用户是否喜欢某个页面.您不必查询图形 api.

You can tell if a user likes a page from the signed_request which is sent in the request from facebook. You don't have to query the graph api.

我们使用 c# 并像这样得到它:

We use c# and get it like so:

protected void Page_Load(object sender, EventArgs e)
    {
        Result = DecodePayload(Request["signed_request"]);
    }

    public JObject DecodePayload(string payload)
    {
        var encoding = new UTF8Encoding();
        var decodedJson = payload.Split('.')[1].Replace("=", string.Empty).Replace('-', '+').Replace('_', '/');
        var base64JsonArray = Convert.FromBase64String(decodedJson.PadRight(decodedJson.Length + (4 - decodedJson.Length % 4) % 4, '='));
        Json = encoding.GetString(base64JsonArray);
        var result = JObject.Parse(Json);
        return result;
    }

然后在页面中

<% if (Result["page"] == null || Result["page"]["liked"] == null || !(bool) Result["page"]["liked"])
     {%> 
 Content if liked
<%}%>

另一件重要的事情截至 3 月 30 日,页面布局正在更改为时间轴,您应该注意当前有一个错误,该错误在喜欢它时不会刷新页面,请参阅:

One more important thing as of the 30th March the page layout is changing to timeline and you should be aware of a bug currently that does not refresh the page upon liking it see this:

有没有人知道点赞时 Facebook 时间线页面上缺少页面刷新的解决方法?

更新

解码签名请求的php为:

The php for decoding the signed request is:

   function parse_signed_request($signed_request, $secret) {
  list($encoded_sig, $payload) = explode('.', $signed_request, 2); 

  // decode the data
  $sig = base64_url_decode($encoded_sig);
  $data = json_decode(base64_url_decode($payload), true);

  if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') {
    error_log('Unknown algorithm. Expected HMAC-SHA256');
    return null;
  }

  // check sig
  $expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
  if ($sig !== $expected_sig) {
    error_log('Bad Signed JSON signature!');
    return null;
  }

  return $data;
}

function base64_url_decode($input) {
  return base64_decode(strtr($input, '-_', '+/'));
}

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

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