我在Asp.net中对Twilio发送sms api发出post请求时出错
我正在学习Twilio发送短信工具。我创建了一个试用帐户,并且有一个经过验证的电话号码 我发出post请求通过我的aspx页面发送短信,但我得到了一个身份验证错误 这是代码。谁能告诉我是什么问题吗我在Asp.net中对Twilio发送sms api发出post请求时出错,asp.net,twilio,Asp.net,Twilio,我正在学习Twilio发送短信工具。我创建了一个试用帐户,并且有一个经过验证的电话号码 我发出post请求通过我的aspx页面发送短信,但我得到了一个身份验证错误 这是代码。谁能告诉我是什么问题吗 string url ="https://api.twilio.com/2010-04-01/Accounts/"+accontSid+ ":" + authenticationToken +"/SMS/Messages.json"; HttpWebRequest request = (HttpWeb
string url ="https://api.twilio.com/2010-04-01/Accounts/"+accontSid+ ":" + authenticationToken +"/SMS/Messages.json";
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
var authInfo = accSid + ":" + authToken;
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
request.Method = "POST";
var postData = "{\"To\":" +"\"" + ToNumber + "\"" + ", \"From\":" + "\"" + FromNumber + "\"" + ", \"Body\":" + "\"" + Body + "\"}";
var data = Encoding.UTF8.GetBytes(postData);
//request.ContentType = "application/json; charset=utf-8";
request.ContentType = "application/json";
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();
运行此代码时,会出现以下错误:
{"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}
我应该做什么改变
编辑更改
try
{
string url ="https://api.twilio.com/2010-04-01/Accounts/"+accountSid+"/SMS/Messages.json";
string FromNumber = "+12012317746";
string ToNumber = "+91xxxxxxxxxx";
string Body = "Hello This is my test account.";
TwilioRestClient client = new TwilioRestClient(accSid, authToken);
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
var authInfo = accSid + ":" + authToken;
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
request.Method = "POST";
var postData = "{\"To\":" +"\"" + ToNumber + "\"" + ", \"From\":" + "\"" + FromNumber + "\"" + ", \"Body\":" + "\"" + 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();
client.SendMessage("+12012317746 ", txtToNo.Text, txtMsg.Text);
}
catch (WebException ex)
{
using (var stream = ex.Response.GetResponseStream())
using (var reader = new StreamReader(stream))
{
Response.Write(reader.ReadToEnd());
}
}
我仍然会遇到同样的错误。这段代码有很多问题。错误本身已经相当清楚了;此请求提供了一个响应,您应该阅读:
detail: Your AccountSid or AuthToken was incorrect.
message: Authentication Error - No credentials provided
more_info: https://www.twilio.com/docs/errors/20003
阅读错误,访问提到的“更多信息”页面,然后单击。代码中的问题:
+:“+authenticationToken
string AccountSid = "AC5ef872f6da5a21de157d80997a64bd33";
string AuthToken = "[AuthToken]";
var twilio = new TwilioRestClient(AccountSid, AuthToken);
var message = twilio.SendMessage(
"+14158141829", "+16518675309",
"Hey Jenny! Good luck on the bar exam!", ...);
这里是Twilio开发者福音传道者 这里有几个问题。正如CodeCaster指出的,您不需要Auth令牌作为URL的一部分,只需要帐户SID 您的授权标头似乎设置不正确,在“Basic”和
authInfo
之间没有空格
您还将请求类型设置为“application/json”
Twilio实际上提供了一个可用于在Asp.net中更轻松地发送消息的工具。您不必担心HTTP请求的基本设置,只需继续构建应用程序即可。。您没有在请求URI中使用令牌。@CodeCaster不需要这样做。Jui只是在学习。@philnash一个人不读书怎么能学习?我也不觉得我之前的评论特别具有攻击性、敌意或“像那样”。它不是特别友好。我希望StackOverflow会更欢迎新开发人员。我认为您下面的实际答案要好得多,感谢您的跟进:)我像@CodeCaster那样做,但我只想通过post请求来做。