Jui Shah
Jui Shah

Reputation: 108

Twilio send sms api request using http post mentod in asp.net

I am learning twilio send sms tool.

I want to make post url request for send message in asp.net mvc application.

I tired some code but could not get safisfied o/p.

There is an error that is authentication sid or auth token is invalid.

Can anyone help me how to make post request for twilio send sms api?

try{
            const string accountSid = "AC255e61580d73904b2a5e5a5e39c715f0";
            const string authToken = "AUTH_TOKEN";
            const string url = "https://api.twilio.com/2010-04-01/Accounts/" + accountSid + "/Messages.json";


            TwilioRestClient client = new TwilioRestClient(accountSid, authToken);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            var authInfo = accountSid +":"+ authToken;
            authInfo = Convert.ToBase64String(Encoding.UTF8.GetBytes(authInfo));
            request.Method = "POST";
            var postData = "{\"To\":" + "\"" + sms.ToNumber + "\"" + ", \"From\":" + "\"" + fromNumber + "\"" + ", \"Body\":" + "\"" + sms.Body + "\"}";

            var data = Encoding.UTF8.GetBytes(postData);
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = data.Length;
            Stream writer = null;
            writer = request.GetRequestStream();
            writer.Write(data, 0, data.Length);
            writer.Close();
            request.Headers["Authorization"] = "Basic " + authInfo;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream());
            string content = reader.ReadToEnd();
            msg = client.SendMessage("+12012317746 ", sms.ToNumber,sms.Body);
        }
        catch (WebException ex)
        {
            using (var stream = ex.Response.GetResponseStream())
            using (var reader = new StreamReader(stream))
            {
                string err =reader.ReadToEnd();
            }
        }

I have an error :

{"code": 20003, "detail": "Your AccountSid or AuthToken was incorrect.", "message": "Authentication Error - No credentials provided", "more_info": "https://www.twilio.com/docs/errors/20003", "status": 401}

Upvotes: 0

Views: 1313

Answers (1)

January Mmako
January Mmako

Reputation: 330

Please check if you are using the Live credentials(accountSid,authToken) on your Twilio Account.

The API does the post already:

https://www.twilio.com/docs/api/twiml/sms/twilio_request

You don't need all this code below.

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        var authInfo = accountSid +":"+ authToken;
        authInfo = Convert.ToBase64String(Encoding.UTF8.GetBytes(authInfo));
        request.Method = "POST";
        var postData = "{\"To\":" + "\"" + sms.ToNumber + "\"" + ", \"From\":" + "\"" + fromNumber + "\"" + ", \"Body\":" + "\"" + sms.Body + "\"}";

        var data = Encoding.UTF8.GetBytes(postData);
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = data.Length;
        Stream writer = null;
        writer = request.GetRequestStream();
        writer.Write(data, 0, data.Length);
        writer.Close();
        request.Headers["Authorization"] = "Basic " + authInfo;
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        StreamReader reader = new StreamReader(response.GetResponseStream());
        string content = reader.ReadToEnd();

Upvotes: 4

Related Questions