[英]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.