【问题标题】:Twilio SMS messaging not working in Console ApplicationTwilio SMS 消息在控制台应用程序中不起作用
【发布时间】:2014-04-06 11:52:05
【问题描述】:

我为我的 Twilio 帐户提供了资金,并且正在使用控制台应用程序。何时访问文档(此处:https://www.twilio.com/user/account/developer-tools/api-explorer/message-create)并输入我的电话号码,请求有效。但是,当我将代码复制到本地控制台应用程序时,什么也没有发生。我逐行复制代码行,确保 SID、令牌和数字正确无误,控制台应用程序运行到执行结束。

string AccountSid = "MySID";
string AuthToken = "MyAuthToken";
var twilio = new TwilioRestClient(AccountSid, AuthToken);
var message = twilio.SendSmsMessage("+12222222222", "+13333333333","Hello World");
Console.WriteLine(message.Sid);

我运行 Fiddler 并为原始数据包获取此信息。 Fiddler 还说结果是 401 状态码。

POST https://api.twilio.com/2010-04-01/Accounts/MySID/SMS/Messages.json HTTP/1.1 授权:基本 {TonsOfRandomCharactersThatLookLikeISHouldHide} 接受:application/json、application/xml、text/json、text/x-json、text/javascript、text/xml 接受字符集:utf-8 用户代理:twilio-csharp/3.4.1.0 (.NET 4.0.30319.17929) 内容类型:application/x-www-form-urlencoded 主办方:api.twilio.com 内容长度:56 接受编码:gzip,放气 连接:保持活动状态

From=%2B14697891380&To=%2B12146630105&Body=New%20Message

对可能发生的事情有任何想法吗?我知道其他人也有这个问题,我看到它在其他地方发布,但我还没有看到回复。

这里还有一个链接到另一个有这个问题的人。我会发表评论,但我没有发表评论的声誉,因此我制作了另一个帖子 (Why is Twilio not sending sms?)

【问题讨论】:

  • 我在 //Build Conference 中找到了这篇博文。我实现了它,发现我从 HTTP 请求中收到 401 Not Authorized 错误。不知道发生了什么,SID 和 Auth Token 与文档中基于浏览器的示例一起使用,但当我绕过他们使用 Nuget 分发的库并使用此博客文章中的示例时不起作用。 link

标签: c# twilio


【解决方案1】:

我无法让 Twilio 工作,这不是技术问题的答案(我认为出于某种原因,Twilio 只是没有授权我的帐户),但是对于你们中的原型设计和需要的东西,我最终使用了Plive 并在一小时内接听电话和发短信。这是我的示例代码,它实际上比 Twilio 便宜。我真的很喜欢 Twilio 并且过去曾使用过它,但从未使用过 C#。所以也许我仍然可以尽快解决问题。

using System;
using System.Collections.Generic;
using System.Reflection;
using RestSharp;
using Plivo.API;

namespace Plivo2
{
    class Program
    {
        static void Main(string[] args)
        {

            string auth_id = "MyAuthID";  // obtained from Plivo account dashboard
            string auth_token = "MyAuthTokey";  // obtained from Plivo account dashboard

            // Making a Call
            string from_number = "MyPliveNumber";
            string to_number = "TheNumberYouWantToContact";

            SendMessage(auth_id, auth_token,  from_number,  to_number,"Hello World!");

        }

        private static void CallPhone(string auth_id,string auth_token, string fromNumber, string toNumber){

            // Creating the Plivo Client
            RestAPI plivo = new RestAPI(auth_id, auth_token);

            IRestResponse<Call> response = plivo.make_call(new Dictionary<string, string>() {
                { "from", fromNumber },
                { "to", toNumber }, 
                { "answer_url", "http://some.domain.com/answer/" }, 
                { "answer_method", "GET" }
            });

            // The "Outbound call" API response has four properties -
            // message, request_uuid, error, and api_id.
            // error - contains the error response sent back from the server.
            if (response.Data != null)
            {
                PropertyInfo[] proplist = response.Data.GetType().GetProperties();
                foreach (PropertyInfo property in proplist)
                    Console.WriteLine("{0}: {1}", property.Name, property.GetValue(response.Data, null));
            }
            else
                Console.WriteLine(response.ErrorMessage);

        }

        private static void SendMessage(string auth_id,string auth_token, string fromNumber, string toNumber, string message) { 

            RestAPI plivo = new RestAPI(auth_id, auth_token);

            IRestResponse<MessageResponse> resp = plivo.send_message(new Dictionary<string, string>() 
            {
                { "src", fromNumber },
                { "dst", toNumber },
                { "text", message },
                { "url", "http://some.domain/receivestatus/" },
                { "method", "GET" }
            });
            if (resp.Data != null)
            {
                PropertyInfo[] proplist = resp.Data.GetType().GetProperties();
                foreach (PropertyInfo property in proplist)
                    Console.WriteLine("{0}: {1}", property.Name, property.GetValue(resp.Data, null));
            }
            else
                Console.WriteLine(resp.ErrorMessage);
        }


    }
}

【讨论】:

    【解决方案2】:

    Twilio 布道者在这里。

    您发布的代码在我看来是正确的,但根据 Fiddler 的输出,您似乎遇到了身份验证错误,因此我会仔细检查您是否已从 Twilio 帐户仪表板正确复制并粘贴了您的帐户 sid 和 auth 令牌.

    希望对您有所帮助。

    【讨论】:

    • 是的,我已经正确复制和粘贴了。我在想也许我的帐户尚未激活或其他什么。几天前我做到了,但 SID 和 AuthToken 仍然在基于浏览器的演示中工作,而不是当我在本地使用它们时。我实际上希望从您的@DevinRader 的回复中得到回复,感谢您至少向我保证代码有效。现在我知道要关注 SID 和 Token。
    • 作为验证点,我想使用我的示例中提供的或此页面上提供的 AccountSID 和 AuthToken 是否正确? twilio.com/user/account/settings
    • 您应该使用来自此处的 AccountSid 和 AuthToken:twilio.com/user/account 或设置页面:twilio.com/user/account/settings。注册后,您无需执行任何其他操作即可开始使用您的帐户。我们不会手动授权帐户。如果您仍然遇到问题,请随时向我发送电子邮件 devin [at] twilio [dot] com,我可以查看您的帐户发生了什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    • 1970-01-01
    • 2022-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多