Twilio使用asp.net中的http post mentod发送sms api请求

时间:2016-03-22 07:53:20

标签: asp.net-mvc twilio

我正在学习twilio发送短信工具。

我想在asp.net mvc应用程序中发送发送消息的url请求。

我厌倦了一些代码,但无法满足o / p。

有一个错误,即身份验证sid或身份验证令牌无效。

任何人都可以帮我解决twilio发送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();
            }
        }

我有一个错误:

{"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 个答案:

答案 0 :(得分:4)

请检查您的Twilio帐户是否使用了真实凭证(accountSid,authToken)。

API已经发布了帖子:

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

您不需要以下所有代码。

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