新的v6 Facebook C#SDK中的FacebookSignedRequest

本文关键字:中的 FacebookSignedRequest C#SDK Facebook v6 新的 | 更新日期: 2023-09-27 18:29:15

我曾经使用过旧的v5 facebook c#sdk,但在FB这个快速变化的世界里,一切似乎都变了:)

我曾经使用一个简单的代码来帮助从签名请求中获取facebook页面ID,请参阅下文。但这已经不起作用了。

有人能帮忙吗?

谢谢!!

FacebookSignedRequest fbSr = FacebookSignedRequest.Parse(appSecret, HttpContext.Current.Request.Form["signed_request"]);
                    if (fbSr != null)
                    {
                        JObject fbSrObj = JObject.Parse(fbSr.Data.ToString());
                        if (fbSrObj["page"] != null && fbSrObj["page"]["id"] != null)
                            return fbSrObj["page"]["id"].ToString();
                    }

新的v6 Facebook C#SDK中的FacebookSignedRequest

我将其与v6.10:一起使用

            string test = "";
        var client = new FacebookClient();
        if (Request.Params["signed_request"] != null)
        {
            string payload = Request.Params["signed_request"].Split('.')[1];
            var encoding = new UTF8Encoding();
            var decodedJson = payload.Replace("=", string.Empty).Replace('-', '+').Replace('_', '/');
            var base64JsonArray = Convert.FromBase64String(decodedJson.PadRight(decodedJson.Length + (4 - decodedJson.Length % 4) % 4, '='));
            var json = encoding.GetString(base64JsonArray);
            var o = JObject.Parse(json);
            var lPid = Convert.ToString(o.SelectToken("page.id")).Replace("'"", "");
            var lLiked = Convert.ToString(o.SelectToken("page.liked")).Replace("'"", "");
            var lUserId = Convert.ToString(o.SelectToken("user_id")).Replace("'"", "");
            var lAdmin = Convert.ToString(o.SelectToken("admin")).Replace("'"", "");
            //test += lPid;    //current page id (where app is installed)
            //test += lLiked;  //page is liked by current visitor
            //test += lUserId; //current visitor
            //test += lAdmin;  //is current visitor an admin (does not work for me)
            if (lPid == "IDOFTHEPAGETOWORKWITH")
            {
                //test += (lAdmin == "True" || lUserId == "ADMIN ID HERE") ? " is admin" : "not admin";
                if (lLiked == "False")
                {
                    test = "like it please";
                }
                else
                {
                    test = "thank you";
                }
            }
        }
        ViewBag.Test = test;