C# 中 Facebook 转换 API 的哈希字段
Hashing fields for Facebook Conversion API in C#
我已使用硬编码示例数据在 C# 中成功调用 Facebook 转化 API。代码如下:
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer",<MyToken>);
Int64 unitTimeStamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
var fbData= new
{
data = new[] {
new {
event_name="Purchase",
event_time = 1616695650,
action_source= "email",
user_data = new
{
em= "7b17fb0bd173f625b58636fb796407c22b3d16fc78302d79f0fd30c2fc2fc068",
ph= ""
},
custom_data = new
{
currency= "USD",
value= "142.52"
}
}
}
};
var postResponse = await httpClient.PostAsJsonAsync("https://graph.facebook.com/v10.0/<MyPixelID/events", fbData);
postResponse.EnsureSuccessStatusCode();
然而,当使用 'real' 数据时,Facebook 要求使用 SHA-256 对某些类型进行哈希处理(例如电子邮件):
我不知道如何在 ASP.NET 中执行此操作。我试过使用以下功能,但这似乎不起作用:
public static string GenerateSHA256(string input)
{
var bytes = Encoding.Unicode.GetBytes(input);
using (var hashEngine = SHA256.Create())
{
var hashedBytes = hashEngine.ComputeHash(bytes, 0, bytes.Length);
var sb = new StringBuilder();
foreach (var b in hashedBytes)
{
var hex = b.ToString("x2");
sb.Append(hex);
}
return sb.ToString();
}
}
附带说明一下,如果我确实使用 SHA-256 对数据进行哈希处理,Facebook 将如何读取数据,因为我认为这是不可逆的?
这一行有问题
var bytes = Encoding.Unicode.GetBytes(input);
这是UTF16。你应该使用 UTF8.
var bytes = Encoding.UTF8.GetBytes(input);
关于你的附带问题,Facebook 是这样说的
https://developers.facebook.com/docs/marketing-api/audiences/guides/custom-audiences#hash
As the owner of your business's data, you are responsible for creating
and managing this data. This includes information from your Customer
Relationship Management (CRM) systems. To create audiences, you must
share your data in a hashed format to maintain privacy. See Hashing
and Normalizing Data. Facebook compares this with our hashed data to
see if we should add someone on Facebook to your ad's audience.
我已使用硬编码示例数据在 C# 中成功调用 Facebook 转化 API。代码如下:
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer",<MyToken>);
Int64 unitTimeStamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
var fbData= new
{
data = new[] {
new {
event_name="Purchase",
event_time = 1616695650,
action_source= "email",
user_data = new
{
em= "7b17fb0bd173f625b58636fb796407c22b3d16fc78302d79f0fd30c2fc2fc068",
ph= ""
},
custom_data = new
{
currency= "USD",
value= "142.52"
}
}
}
};
var postResponse = await httpClient.PostAsJsonAsync("https://graph.facebook.com/v10.0/<MyPixelID/events", fbData);
postResponse.EnsureSuccessStatusCode();
然而,当使用 'real' 数据时,Facebook 要求使用 SHA-256 对某些类型进行哈希处理(例如电子邮件):
我不知道如何在 ASP.NET 中执行此操作。我试过使用以下功能,但这似乎不起作用:
public static string GenerateSHA256(string input)
{
var bytes = Encoding.Unicode.GetBytes(input);
using (var hashEngine = SHA256.Create())
{
var hashedBytes = hashEngine.ComputeHash(bytes, 0, bytes.Length);
var sb = new StringBuilder();
foreach (var b in hashedBytes)
{
var hex = b.ToString("x2");
sb.Append(hex);
}
return sb.ToString();
}
}
附带说明一下,如果我确实使用 SHA-256 对数据进行哈希处理,Facebook 将如何读取数据,因为我认为这是不可逆的?
这一行有问题
var bytes = Encoding.Unicode.GetBytes(input);
这是UTF16。你应该使用 UTF8.
var bytes = Encoding.UTF8.GetBytes(input);
关于你的附带问题,Facebook 是这样说的 https://developers.facebook.com/docs/marketing-api/audiences/guides/custom-audiences#hash
As the owner of your business's data, you are responsible for creating and managing this data. This includes information from your Customer Relationship Management (CRM) systems. To create audiences, you must share your data in a hashed format to maintain privacy. See Hashing and Normalizing Data. Facebook compares this with our hashed data to see if we should add someone on Facebook to your ad's audience.