using (var httpClient = new HttpClient())
{
httpClient.BaseAddress = new Uri("https://platform.clickatell.com/");
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "wyOtm7-eRq-dSHWORr6hfw==");
var stringContent = new StringContent("{\"content\":\"Test Message\",\"to\":[\"61400000000\"]}");
stringContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var httpResponseMessage = AsyncHelper.RunSync(() => httpClient.PostAsync("messages", stringContent));
var result = httpResponseMessage.Content.ReadAsStringAsync().Result;
}
结果是: {“messages”:[],“error”:“Integration API密钥无效或缺失。 - ”}}
但我的API密钥与Clickatell完全一致。
其他信息: Clickatell站点显示以下Json curl调用(注意:我无法使curl工作以进行测试)。
CURL CALL:
==========
curl -i \
-X POST \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "Authorization: wyOtm7-eRq-dSHWORr6hfw==" \
-d '{"content": "Test Message Text", "to": ["61400000000"], "from": "+1234567890"}' \
-s https://platform.clickatell.com/messages
唯一的区别是我将我的令牌指定为不记名令牌。没有它我会得到格式异常。
感谢任何能够阐明我的问题的人。
答案 0 :(得分:2)
手动FOR TEST API
您可以使用Google Chrome的Postman APP进行检查
步骤: -
在“
”中添加此内容{ "内容":"测试消息", "到":[" + 919999999999"] }
点击发送
你会找到答案。
通过以上流程,您还可以手动测试API 。
答案 1 :(得分:0)
我想我已经解决了。看起来我需要使用无效的授权标头。
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", "z2zbgb4bRWeaImcjrhmWww==");
答案 2 :(得分:0)
要解决此问题,请按以下步骤操作:
创建一个实体(名称:OTPRequestEntity)
public class OTPRequestEntity
{
[JsonProperty("content")]
public string content { get; set; }
[JsonProperty("to")]
public List<string> to { get; set; }
}
转到您的代码并添加标题: -
OTPResponseEntity entOTPResponseEntity = new OTPResponseEntity();
List<string> numbers = new List<string>();
numbers.Add("Your Mobile Number");
string apiBaseAddress = "https://platform.clickatell.com";
var objHttpClient = new HttpClient();
objHttpClient.BaseAddress = new Uri(apiBaseAddress);
objHttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
objHttpClient.DefaultRequestHeaders.Add(HttpRequestHeader.ContentType.ToString(), "application/json");
objHttpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", "Your API Key");
var vendRequest = new OTPRequestEntity
{
content = "Your Message.",
to = numbers
};
HttpResponseMessage apioresponse = await objHttpClient.PostAsJsonAsync("/messages", vendRequest).ConfigureAwait(false);
apioresponse.EnsureSuccessStatusCode();
if (apioresponse.StatusCode == HttpStatusCode.Accepted)
{
string responseString = await apioresponse.Content.ReadAsStringAsync().ConfigureAwait(false);
entOTPResponseEntity = JsonConvert.DeserializeObject<OTPResponseEntity>(responseString);
}
在 entOTPResponseEntity 对象中,您将获得 API响应。