0
votes

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}
1
I can't understand how to pass accSid as username and authToken as password. - Jui Shah
I'm not an expert on Twilio but it looks like you are mixing the "raw" REST request with using the .Net REST API Helper Library. Maybe try just using the TwilioRestClient. - Filburt
You mean to say only : TwilioRestClient client = new TwilioRestClient(accountSid, authToken); const string fromNumber = "+12012317746 "; var result = client.SendMessage(fromNumber, sms.ToNumber, sms.Body); - Jui Shah
Yes, that's all it should take using the Helper Lib. You may want to trim the trailing space in your fromNumber value. - Filburt

1 Answers

4
votes

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