2
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":"Invalid or missing Integration API Key.-"}

但我的 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

唯一的区别是我将我的令牌指定为不记名令牌。没有它,我会收到格式异常。

感谢任何可能对我的问题有所了解的人。

4

3 回答 3

2

手动测试 API

您可以使用谷歌浏览器的 Postman APP 查看

脚步 :-

  1. 安装 POSTMAN(去谷歌浏览器应用部分搜索邮递员)
  2. 创建一个 API
  3. 选择方法作为 POST
  4. 添加此 URL https://platform.clickatell.com/messages
  5. 转到标题部分并添加标题
  6. 添加 Key = Content-Type , value = application/json
  7. 添加键=接受,值=应用程序/json
  8. 添加 Key = Authorization , value =你的 API Key
  9. 转到正文部分
  10. 选择为原始
  11. 添加此内容

    {“内容”:“测试消息。”,“到”:[“+919999999999”]}

  12. 点击发送

  13. 你会找到回应。

按照上述过程,您还可以手动测试您的 API

于 2017-02-27T10:13:52.143 回答
1

我想我已经解决了。看来我需要使用无效的授权标头。

httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", "xxxxxxxxxxxxxxxxxxxxxx==");
于 2016-11-22T12:06:57.583 回答
0

要解决此问题,请按照以下步骤操作:

  1. 登录到您的 clickatell 帐户。
  2. 转到 SMS 集成(左侧菜单)。
  3. 选择(单击)您的集成名称。
  4. 您将找到更新按钮,然后是 API 密钥。
  5. 单击更新按钮,它将更新您的 API 密钥。
  6. 现在保存或发布您的集成。
  7. 确保您的 SMS 集成应处于活动模式。(您可以从 SMS 集成(左侧菜单)状态选项卡更改状态)

创建一个实体(名称: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 响应

于 2017-02-27T09:54:40.283 回答