我正在尝试使用第三方api实现发送消息功能。API-https://api.txtlocal.com/send/
但是,当我们测试这个实现时,我们遇到了一个错误代码为3的问题,并给出了一条“无效用户详细信息”的消息。
C#代码:
string UserId = "1234";
String message = HttpUtility.UrlEncode("OTP");
using (var wb = new WebClient())
{
byte[] response = wb.UploadValues("https://api.txtlocal.com/send/", new NameValueCollection()
{
{"username" , "<TextLocal UserName>"},
{"hash" , "<API has key>"},
{"sender" , "<Unique sender ID>"},
{"numbers" , "<receiver number>"},
{"message" , "Text message"}
});
string result = System.Text.Encoding.UTF8.GetString(response);
//return result;错误详细信息:
{
"errors": [{
"code": 3,
"message": "Invalid login details"
}],
"status": "failure"
}即使我传递的是有效凭据。
如果您需要更多的详细信息,请帮助我并让我知道。
感谢并提前感谢您的帮助。
发布于 2016-07-17 00:42:07
API的文档指出,您应该在POST请求的头部或GET请求的url中传递参数值。WebClient.UploadValue在默认情况下执行POST,但是您没有相应地设置头部。因此找不到任何凭据。
您可以尝试使用WebClient.UploadValues(name, method, values)重载并指定GET as方法。
NameValueCollection values = ...;
byte[] response = wb.UploadValues("https://api.txtlocal.com/send/", "GET", values);发布于 2017-11-15 18:43:25
我认为您应该发送API密钥或用户名和密码。
从你的请求中删除用户名,只留下API密钥、发送者、号码和消息。那么一切都应该正常工作了。
发布于 2019-06-21 13:41:41
这是对我有效的方法:
[HttpGet]
public async Task<JObject> SendOtp(string number)
{
using (var client = _httpClientFactory.CreateClient())
{
client.BaseAddress = new Uri("https://api.textlocal.in/");
client.DefaultRequestHeaders.Add("accept","application/json");
var query = HttpUtility.ParseQueryString(string.Empty);
query["apikey"] = ".....";
query["numbers"] = ".....";
query["message"] = ".....";
var response = await client.GetAsync("send?"+query);
response.EnsureSuccessStatusCode();
var content = await response.Content.ReadAsStringAsync();
return JObject.Parse(content);
}
}https://stackoverflow.com/questions/38411714
复制相似问题