日安
我正在尝试使用C#
云API在WhatsApp中创建一个带有媒体头的模板,如下所述:可恢复上传API。现在,每当我创建模板时,都会返回一个错误:文件类型不支持。
我已经在网上搜索了其他开发人员的例子,他们都经历过同样的事情,但是没有人发现解决了我的问题。我已经遵循了这两篇文章中的建议/解决方案,但仍然很幸运:
My Steps:
代码:
创建会话
// 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"
}
}
帮帮忙,谢谢。
发布于 2022-09-27 06:28:34
我找到了解决办法,希望这能帮助其他人解决同样的问题。
我没有向请求中添加“内容类型”标题,而是在request.Content,上添加了它们,例如:request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
我还将MultipartFormDataContent
更改为带有内容头application/x-www-form-urlencoded
的ByteArrayContent
。
有关对我有用的完整代码示例,请参见下面的内容。
创建会话
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;
}
}
https://stackoverflow.com/questions/73853167
复制相似问题