我正在学习Twilio发送短信工具。我创建了一个试用帐户,并且还有一个经过验证的电话号码。
我发出了通过我的aspx页面发送短信的请求,但是我收到了身份验证错误。
这是代码。谁能告诉我这是什么问题?
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());
}
}
我仍然得到同样的错误。
答案 0 :(得分:1)
此代码存在很多问题。错误本身已经很清楚了;此请求提供了您应该阅读的响应:
detail: Your AccountSid or AuthToken was incorrect.
message: Authentication Error - No credentials provided
more_info: https://www.twilio.com/docs/errors/20003
阅读错误,访问提到的“更多信息”页面和read the documentation。您的代码存在问题:
+ ":" + 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!", ...);
答案 1 :(得分:1)
Twilio开发者传道者在这里。
这里有几个问题。正如CodeCaster指出的那样,您不需要将Auth Token作为URL的一部分,只需要帐户SID。
您的授权标题看起来好像设置不正确,您在" Basic"之间没有空格。和authInfo
。
您还将请求类型设置为" application / json"。 Twilio expects your POST request to have form encoded data as the body and the content type "application/x-www-form-urlencoded"
Twilio实际上提供了一个C# helper library,您可以使用它来更轻松地在Asp.net中发送消息。您不必担心HTTP请求的基本设置,并且可以继续构建您的应用程序。