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 无效。我已登录,当我移除 key 时,我可以正常执行所有操作。所以为了节省一些时间:
- 是的,我发布到正确的 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,但是它没有太多文档,而且似乎没有涉及任何与自动化、服务器端操作等相关的内容。
谢谢
最佳答案
我已经在 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();
}
关于创建 Facebook AppSecret_Proof HMACSHA256 需要 C# 帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20572523/