使用Delphi的Twilio或Plivo SMS [英] Twilio or Plivo SMS using Delphi

查看:106
本文介绍了使用Delphi的Twilio或Plivo SMS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何使用Delphi 10 Seattle和Indy向Plivo或Twilio发送POST请求以发送SMS消息.当我将此代码用于Twilio时,会得到一条未授权的消息作为回报(请注意,我已编辑了我的用户名和Auth代码):

I am trying to figure out how to use Delphi 10 Seattle and Indy to send a POST request to either Plivo or Twilio for sending SMS messages. When I use this code for Twilio efforts, I get an Unauthorized message in return (note that I have redacted my user name and Auth code):

procedure TSendTextForm.TwilioSendSms(FromNumber, ToNumber, Sms: string; Var Response: TStrings);

var
  apiurl, apiversion, accountsid, authtoken,
  url: string;
  aParams, aResponse: TStringStream;
  mHTTP : TidHttp;

begin

  mHTTP := TIdHTTP.Create(nil);

  apiurl := 'api.twilio.com';
  apiversion := '2010-04-01';
  accountsid := 'AC2f7cda1e6a4e74376***************:2b521b60208af4c*****************';
  url := Format('https://%s/%s/Accounts/%s/SMS/Messages/', [apiurl, apiversion, accountsid]);
  aParams := TStringStream.Create ;
try
  aParams.WriteString('&From=' + FromNumber);
  aParams.WriteString('&To=' + ToNumber);
  aParams.WriteString('&Body=' + Sms);
  aResponse := TStringStream.Create;
try
  mHTTP.Post(url, aParams, aResponse);
finally
  Response.Text  := aResponse.DataString;
end;
finally
  aParams.Free;
end;
end;

我有类似的Plivo代码.两家公司都没有任何Delphi支持.谁能告诉我我在这里上面想念的东西吗?非常感谢.

I have similar code for Plivo. Neither company has any Delphi support. Can anyone tell me what I'm missing above here? Thanks so much.

麦克风

推荐答案

此处是Twilio的传播者.

Twilio evangelist here.

除了上面@mjn提出的基本身份验证"建议外,您的示例中还有另外两个问题,我相信会导致您遇到问题:

In addition to the Basic Auth suggestion made by @mjn above, there are two other issues in your sample I believe will cause you problems:

首先,在上面的代码示例中,您的URL将是错误的,因为accountsid变量将sid和auth令牌连接在一起.

First, in the code example above, your URL will be wrong because the accountsid variable is concatenating together both your sid and auth token.

accountsid := 'AC2f7cda1e6a4e74376***************:2b521b60208af4c*****************';

要做以便使用基本身份验证时,您不想将auth令牌作为URL的一部分.创建url属性时,您只想将SID作为如下参数放入URL:

While you do want to do this in order to use Basic Authentication, you don't want the auth token as part of the URL. When you create the url property you just want to put the SID into the URL as the parameter like this:

/Accounts/ACXXXXXXX/

第二,我也建议不要使用/SMS资源.而是使用更新的且具有更多功能的 /Messages :

Second, I would also suggest not using the /SMS resources as its deprecated. Instead use /Messages which is newer and has more features:

/Accounts/ACXXXXXXX/Messages

这篇关于使用Delphi的Twilio或Plivo SMS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆