创建Facebook AppSecret_Proof HMACSHA256需要C#帮助

 风铃草549天王 发布于 2023-02-10 21:46

Facebook要求我创建appsecret_proof:https: //developers.facebook.com/docs/graph-api/securing-requests

我使用以下代码完成了此操作:

public string FaceBookSecret(string content, string key)
{
        var encoding = new System.Text.ASCIIEncoding();
        byte[] keyByte = encoding.GetBytes(key);
        byte[] messageBytes = encoding.GetBytes(content);
        using (var hmacsha256 = new HMACSHA256(keyByte))
        {
            byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
            return Convert.ToBase64String(hashmessage);
        }
}

一切看起来都不错,但facebook说appsecret_proof无效.我已登录,当我删除密钥时,我可以正常地完成所有操作.所以节省一些时间:

是的我发布到正确的URL

是的我传递了有效的access_token

是的我在证明中使用相同的access_token,就像我在请求中一样

是的,我的appsecret很好,并且有效

使用示例

dynamic results = client.Post("/" + model.PostAsId + "/feed", new { message = model.Message, appsecret_proof = FaceBookSecret(postAs.AuthToken, AppSecret) });

我认为它可能与编码或其他东西有关,但说实话,我只是不知道.

我也在使用Facebook .net SDK,但这在文档中没有多少,并且似乎没有涉及与自动化,服务器端操作等有关的任何事情.

谢谢

1 个回答
  • 我在Facebook上成功使用了以下内容

    using System.Security.Cryptography;
    using System.Text;
    
    internal static string FaceBookSecret(string content, string key)
    {
        byte[] keyBytes = Encoding.UTF8.GetBytes(key);
        byte[] messageBytes = Encoding.UTF8.GetBytes(content);
        byte[] hash;
        using (HMACSHA256 hmacsha256 = new HMACSHA256(keyBytes))
        {
            hash = hmacsha256.ComputeHash(messageBytes);
        }
    
        StringBuilder sbHash = new StringBuilder();
        for (int i = 0; i < hash.Length; i++)
        {
            sbHash.Append(hash[i].ToString("x2"));
        }
        return sbHash.ToString();
    }
    

    2023-02-10 21:53 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有