Vishal Kadam
Vishal Kadam

Reputation: 31

How to send sms using Textlocal API?

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.

Upvotes: 2

Views: 14641

Answers (4)

harishr
harishr

Reputation: 18065

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);
    }
}

Upvotes: 1

Atul
Atul

Reputation: 3423

its bit late ...... Try replacing {"hash" , ""} with {"apikey" , ""}

Upvotes: 0

David Whiteford
David Whiteford

Reputation: 103

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.

Upvotes: 1

derpirscher
derpirscher

Reputation: 17400

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);

Upvotes: 3

Related Questions