How to send sms using Textlocal API?

12,345

Solution 1

The documentation for the API states that you should pass your parameter values either in the header for POST requests or in the url for GET requests. WebClient.UploadValue does a POST per default, but you don't set the header accordingly. So no credentials are found.

You could try to use the WebClient.UploadValues(name, method, values) overload and specify GET as method.

NameValueCollection values = ...;
byte[] response = wb.UploadValues("https://api.txtlocal.com/send/", "GET", values);

Solution 2

I believe you should either send the API Key OR the username and password.

Remove the username from your request and just leave the API key, sender, numbers and message. All should work OK then.

Solution 3

This is what worked for me:

[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);
    }
}
Share:
12,345
Admin
Author by

Admin

Updated on June 22, 2022

Comments

  • Admin
    Admin almost 2 years

    I am trying to implement the send message functionality using third party api. API-https://api.txtlocal.com/send/

    But when we are testing the implementation, we are facing an issue with error code 3 and giving a message as "invalid user details."

    C# code:

    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;
    

    Error Details:

     {
        "errors": [{
            "code": 3,
            "message": "Invalid login details"
        }],
        "status": "failure"
    }
    

    Even though I am passing valid credentials.

    Please assist me and let me know in case you require any more details.

    Thanks and appreciate your help in advance.