[英]Add a Template sample image to WhatsApp Cloud API
再会
我正在尝试使用 WhatsApp 云 API 在C#
中创建一个带有媒体 header 的模板,如下所述: Resumable Upload ZDB97427CEDFF84 现在,每当我创建模板时,都会返回一个错误: File type not supported 。
我在网上搜索了其他有相同经历但没有找到解决我的问题的开发人员的示例。 我遵循了这两个帖子中的建议/解决方案,但仍然很幸运:
我的步骤:
代码:
// Create the session
var sessionId = "";
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("POST"), $"https://graph.facebook.com/v14.0/{appId}/uploads"))
{
request.Headers.TryAddWithoutValidation("Authorization", "Bearer " + _accessToken);
request.Headers.TryAddWithoutValidation("Content-Type", "application/json");
request.Content = new StringContent("{\"file_length\":\"16384\",\"file_type\":\"image/png\",\"file_name\":\"test.png\"}");
var response = await httpClient.SendAsync(request);
var responseContent = response.Content.ReadAsStringAsync().Result;
var result = System.Text.Json.JsonSerializer.Deserialize<SessionResponse>(responseContent);
sessionId = result.id;
}
}
var handle = "";
var dataBinary = System.IO.File.ReadAllBytes(@"C:\Temp\IMAGES\test.png");
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("POST"), $"https://graph.facebook.com/v14.0/{sessionId}"))
{
request.Headers.TryAddWithoutValidation("Authorization", "OAuth " + _accessToken);
request.Headers.TryAddWithoutValidation("file_offset", "0");
request.Headers.TryAddWithoutValidation("Content-Type", "multipart/form-data");
var multipartContent = new MultipartFormDataContent();
multipartContent.Add(new ByteArrayContent(dataBinary));
request.Content = multipartContent;
var response = await httpClient.SendAsync(request);
var responseContent = response.Content.ReadAsStringAsync().Result;
var result = System.Text.Json.JsonSerializer.Deserialize<MediaUploadSessionResponse>(responseContent);
handle = result.h;
}
}
jsonData:(本例中未添加完整句柄)
{
"name":"template_media",
"components":[
{
"type":"HEADER",
"format":"IMAGE",
"example":{
"header_handle":[
"4:::ARaVEoRalHjf9hIFnYJb2O9I6BJeHNoonwkB...."
]
}
},
{
"type":"BODY",
"text":"Please find media attached as requested."
}
],
"language":"en_US",
"category":"TRANSACTIONAL"
}
要求:
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("POST"), $"https://graph.facebook.com/v14.0/{_businessAccountID}/message_templates"))
{
request.Headers.TryAddWithoutValidation("Authorization", "Bearer " + _accessToken);
request.Content = new StringContent(jsonData);
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
var response = await httpClient.SendAsync(request);
var responseContent = response.Content.ReadAsStringAsync().Result;
}
}
{
"error": {
"message": "Invalid parameter",
"type": "OAuthException",
"code": 100,
"error_subcode": 2388084,
"is_transient": false,
"error_user_title": "File Type Not Supported",
"error_user_msg": "The type of file is not supported.",
"fbtrace_id": "AZalksSZjALNaBLXiiJzgZw"
}
}
请帮忙,谢谢。
我找到了解决方案,希望这可以帮助其他人解决同样的问题。
我没有将“Content-Type”标头添加到请求中,而是将它们添加到request.Content上,例如: request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
我还将MultipartFormDataContent
更改为ByteArrayContent
,其内容为 header application/x-www-form-urlencoded
。
有关对我有用的完整代码示例,请参见下文。
var sessionId = "";
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("POST"), $"https://graph.facebook.com/v14.0/{appId}/uploads"))
{
request.Headers.TryAddWithoutValidation("Authorization", "Bearer " + _accessToken);
request.Content = new StringContent("{\"file_length\":\"16384\",\"file_type\":\"image/png\",\"file_name\":\"test.png\"}");
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
var response = await httpClient.SendAsync(request);
if (!response.IsSuccessStatusCode)
return null;
var responseContent = response.Content.ReadAsStringAsync().Result;
var result = System.Text.Json.JsonSerializer.Deserialize<SessionResponse>(responseContent);
sessionId = result.id;
}
}
var handle = "";
var dataBinary = System.IO.File.ReadAllBytes(@"C:\Temp\IMAGES\test.png");
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("POST"), $"https://graph.facebook.com/v14.0/{sessionId}"))
{
request.Headers.TryAddWithoutValidation("Authorization", "OAuth " + _accessToken);
request.Headers.TryAddWithoutValidation("file_offset", "0");
request.Content = new ByteArrayContent(dataBinary);
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");
var response = await httpClient.SendAsync(request);
if (!response.IsSuccessStatusCode)
return null;
var responseContent = response.Content.ReadAsStringAsync().Result;
var result = System.Text.Json.JsonSerializer.Deserialize<MediaUploadSessionResponse>(responseContent);
handle = result.h;
}
}
jsonData:(本例中未添加完整句柄)
{
"name":"template_media",
"components":[
{
"type":"HEADER",
"format":"IMAGE",
"example":{
"header_handle":[
"4:::ARaVEoRalHjf9hIFnYJb2O9I6BJeHNoonwkB...."
]
}
},
{
"type":"BODY",
"text":"Please find media attached as requested."
}
],
"language":"en_US",
"category":"TRANSACTIONAL"
}
要求
var responseContent = string.empty;
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("POST"), $"https://graph.facebook.com/v14.0/{_businessAccountID}/message_templates"))
{
request.Headers.TryAddWithoutValidation("Authorization", "Bearer " + _accessToken);
request.Content = new StringContent(jsonData);
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
var response = await httpClient.SendAsync(request);
if (!response.IsSuccessStatusCode)
return null;
responseContent = response.Content.ReadAsStringAsync().Result;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.