【问题标题】:I get an error when i make post request on Twilio sending sms api in Asp.net当我在 Twilio 上发出在 Asp.net 中发送短信 api 的发布请求时出现错误
【发布时间】:2016-07-07 21:35:21
【问题描述】:

我正在学习 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());
            }
        }

我仍然遇到同样的错误。

【问题讨论】:

  • Read the documentation。您没有在请求 URI 中使用令牌。
  • @CodeCaster 不需要那样。 Jui 只是在学习。
  • @philnash 一个人不读书怎么能学?我也不觉得我之前的评论特别冒犯、敌对或“那样”。
  • 不是特别友好。我希望 StackOverflow 对新开发人员更加欢迎。
  • 我认为您在下面的实际答案要好得多,感谢您的跟进:)

标签: asp.net twilio


【解决方案1】:

这里是 Twilio 开发者宣传员。

这里有几个问题。正如 CodeCaster 指出的那样,您不需要 Auth Token 作为 URL 的一部分,只需要 Account SID。

您的 Authorization 标头似乎设置不正确,“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 请求的基本设置,只需继续构建您的应用程序即可。

【讨论】:

    【解决方案2】:

    这段代码有很多问题。错误本身已经很清楚了;此请求提供了一个您应该阅读的响应:

    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。您的代码存在的问题:

    1. 您只需在请求 URI 中提供帐户 ID,因此请丢失 + ":" + authenticationToken
    2. 您没有正确编写基本身份验证标头。您在“基本”和 base64 编码凭据之间缺少一个空格。
    3. 借用刚刚发布的其他答案:请求的内容类型也不正确。
    4. 如上面“更多信息”和文档链接中所述,请使用客户端库。您不想重新发明轮子。

    特别是如果您查看最后一点,所有这些代码都可以替换为以下内容:

    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!", ...);
    

    【讨论】:

    • 我正在这样做@CodeCaster,但我只想通过发布请求来做到这一点。
    猜你喜欢
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-09
    • 2021-05-01
    • 2019-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多