使用Delphi的Twilio或Plivo SMS
我想知道如何使用Delphi 10西雅图和印地向Plivo或Twilio发送发送短信的POST请求。当我将此代码用于Twilio工作时,我会得到一条未经授权的消息作为回报(请注意,我已经编辑了我的用户名和身份验证代码): 我有类似的Plivo代码。这两家公司都没有任何德尔福支持。有人能告诉我上面我遗漏了什么吗?非常感谢 Mic上的REST API文档表示使用了基本身份验证: 对RESTAPI的HTTP请求受HTTP Basic保护 认证 TIdHTTP内置了对基本身份验证的支持。只需将使用Delphi的Twilio或Plivo SMS,delphi,sms,twilio,indy,plivo,Delphi,Sms,Twilio,Indy,Plivo,我想知道如何使用Delphi 10西雅图和印地向Plivo或Twilio发送发送短信的POST请求。当我将此代码用于Twilio工作时,我会得到一条未经授权的消息作为回报(请注意,我已经编辑了我的用户名和身份验证代码): 我有类似的Plivo代码。这两家公司都没有任何德尔福支持。有人能告诉我上面我遗漏了什么吗?非常感谢 Mic上的REST API文档表示使用了基本身份验证: 对RESTAPI的HTTP请求受HTTP Basic保护 认证 TIdHTTP内置了对基本身份验证的支持。只需将TIdHT
TIdHTTP.Request.BasicAuthentication
属性设置为true,然后根据需要设置IdHTTP.Request.Username
和TIdHTTP.Request.Password
属性
其他提示:
可以缩短为TIdHTTP.Create(nil)
TIdHTTP.Create
- 响应的var修饰符可以更改为const
TIdHTTP.Request.BasicAuthentication
属性设置为true,然后根据需要设置IdHTTP.Request.Username
和TIdHTTP.Request.Password
属性
其他提示:
可以缩短为TIdHTTP.Create(nil)
TIdHTTP.Create
- 响应的var修饰符可以更改为const
您的代码也会泄漏内存,因为Indy组件没有被释放。在Delphi 10中,TRestClient中有一些REST组件
TMS在其TMS云包中为Twillio制作了组件,在Delphi 10西雅图,有一些REST组件与TRestClient
TMS在他们的TMS云包中为Twillio制造组件 除了上面@mjn提出的基本Auth建议外,我相信您的样本中还有两个问题会给您带来问题: 首先,在上面的代码示例中,您的URL将是错误的,因为
accountsid
变量将您的sid和身份验证令牌连接在一起
accountsid := 'AC2f7cda1e6a4e74376***************:2b521b60208af4c*****************';
虽然您确实希望这样做以使用基本身份验证,但不希望将身份验证令牌作为URL的一部分。创建url
属性时,只需将SID作为参数放入url中,如下所示:
/Accounts/ACXXXXXXX/
其次,我还建议不要使用/SMS
资源作为其弃用资源。相反,请使用更新且具有更多功能的:
/Accounts/ACXXXXXXX/Messages
这里是Twilio福音传道者
除了上面@mjn提出的基本Auth建议外,我相信您的样本中还有两个问题会给您带来问题:
首先,在上面的代码示例中,您的URL将是错误的,因为accountsid
变量将您的sid和身份验证令牌连接在一起
accountsid := 'AC2f7cda1e6a4e74376***************:2b521b60208af4c*****************';
虽然您确实希望这样做以使用基本身份验证,但不希望将身份验证令牌作为URL的一部分。创建url
属性时,只需将SID作为参数放入url中,如下所示:
/Accounts/ACXXXXXXX/
其次,我还建议不要使用/SMS
资源作为其弃用资源。相反,请使用更新且具有更多功能的:
/Accounts/ACXXXXXXX/Messages
这很有趣,但我正要发布关于plivo的相同问题。我也一直在努力让twilio和plivo都能工作。我终于设法让twilio开始工作了。另一方面,Plivo不能使用相同的代码,即使它们实际上是相同的
我在delphi中使用了REST函数。以下代码用于twilio和plivo
procedure TForm1.FormCreate(Sender: TObject);
begin
// TypeCo
// = T = Twilio
// = P = Plivo
TypeCo := 'T';
if TypeCo='T' then // Twillio
begin
AccountSid := 'ACd27xxxxxxxxxxxxxxxxxxxxxxb106e38'; // x's were replaced to hide ID
AuthToken := '24fxxxxxxxxxxxxxxxxxxxxxxxxf08ed'; // x's were replaced to hide Token
BaseURL := 'https://api.twilio.com';
Resource := '/2010-04-01/Accounts/'+accountSid+'/Messages';
end
else if TypeCO='P' then // Plivo
begin
AccountSid := 'MANTxxxxxxxxxxxxxXYM';
AuthToken := 'ZDg0OxxxxxxxxxxxxxxxxxxxxxxxxxxxxjM5Njhh';
BaseURL := 'https://api.plivo.com';
Resource := '/v1/Account/'+accountSid+'/Message/';
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
RESTClient := TRESTClient.Create(BaseURL);
try
RESTRequest := TRESTRequest.Create(RESTClient);
try
RESTResponse := TRESTResponse.Create(RESTClient);
try
HTTPBasicAuthenticator := THTTPBasicAuthenticator.Create('AC', 'c1234');
try
RESTRequest.ResetToDefaults;
RESTClient.BaseURL := BaseURL;
RESTRequest.Resource := Resource;
HTTPBasicAuthenticator.UserName := AccountSid;
HTTPBasicAuthenticator.Password := AuthToken;
RESTClient.Authenticator := HTTPBasicAuthenticator;
RESTRequest.Client := RESTClient;
RESTRequest.Response := RESTResponse;
// Tried this to fix plivo error with no luck!
// RESTClient.Params.AddHeader('Content-Type', 'application/json');
// "From" number is the send # setup in the twilio or plivo account. The "To" number is a verified number in your twilio or plivo account
if TypeCo='T' then // Twilio
SendSMS( '+1602xxxxx55','+1602xxxxxx7', 'This is a test text from Twilio') // x's were replaced to hide telephone numbers
else if TypeCo='P' then // Plivo
SendSMS( '1602xxxxx66','1602xxxxxx7', 'This is a test text from Plivo'); // x's were replaced to hide telephone numbers
finally
HTTPBasicAuthenticator.Free;
end;
finally
RESTResponse.Free;
end;
finally
RESTRequest.Free;
end;
finally
RESTClient.Free;
end;
end;
function TForm1.SendSMS(aFrom, aTo, aText: string): boolean;
begin
result := True;
RESTRequest.ResetToDefaults;
RESTClient.BaseURL := BaseURL;
RESTRequest.Resource := Resource;
if TypeCo='T' then // Twilio
begin
RESTRequest.Params.AddUrlSegment('AccountSid', accountSid);
RESTRequest.Params.AddItem('From', aFrom);
RESTRequest.Params.AddItem('To', aTo);
RESTRequest.Params.AddItem('Body', aText);
end
else if TypeCo='P' then // Plivo
begin
RESTRequest.Params.AddUrlSegment('AccountSid', accountSid);
RESTRequest.Params.AddItem('src', aFrom);
RESTRequest.Params.AddItem('dst', aTo);
RESTRequest.Params.AddItem('text', aText);
end;
RESTRequest.Method := rmPOST;
RESTRequest.Execute;
// Show Success or Error Message
ErrorMsg.Clear;
ErrorMsg.Lines.Text := RESTResponse.Content;
end;
如前所述,上面的代码对Twilio很有效。但是,对于Plivo,我得到以下错误:
{
"api_id": "b124d512-b8b6-11e5-9861-22000ac69cc8",
"error": "use 'application/json' Content-Type and raw POST with json data"
}
我一直在努力确定如何解决这个问题。我联系了Plivo支持部门,收到了以下回复:
The error "use 'application/json' Content-Type and raw POST with json data" is generated when the Header "Content-Type" is not set the value "application/json"
Please add the Header Content-Type under Items in the Request Tab and set the Description as application/json.
我尝试在按钮过程中添加代码:
RESTClient.Params.AddHeader('Content-Type', 'application/json');
但它仍然给出了相同的错误。我认为这段代码非常接近于为Plivo工作。我是一个新手在休息功能,所以我不知道还有什么可以尝试。我尝试将“application/json”分配给几乎所有接受它的对象,但仍然得到相同的错误。希望其他人能知道是什么让Plivo起作用。这很有趣,但我只是想发布关于Plivo的相同问题。我也一直在努力让twilio和plivo都能工作。我终于设法让twilio开始工作了。另一方面,Plivo不能使用相同的代码,即使它们实际上是相同的 我在delphi中使用了REST函数。以下代码用于twilio和plivo
procedure TForm1.FormCreate(Sender: TObject);
begin
// TypeCo
// = T = Twilio
// = P = Plivo
TypeCo := 'T';
if TypeCo='T' then // Twillio
begin
AccountSid := 'ACd27xxxxxxxxxxxxxxxxxxxxxxb106e38'; // x's were replaced to hide ID
AuthToken := '24fxxxxxxxxxxxxxxxxxxxxxxxxf08ed'; // x's were replaced to hide Token
BaseURL := 'https://api.twilio.com';
Resource := '/2010-04-01/Accounts/'+accountSid+'/Messages';
end
else if TypeCO='P' then // Plivo
begin
AccountSid := 'MANTxxxxxxxxxxxxxXYM';
AuthToken := 'ZDg0OxxxxxxxxxxxxxxxxxxxxxxxxxxxxjM5Njhh';
BaseURL := 'https://api.plivo.com';
Resource := '/v1/Account/'+accountSid+'/Message/';
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
RESTClient := TRESTClient.Create(BaseURL);
try
RESTRequest := TRESTRequest.Create(RESTClient);
try
RESTResponse := TRESTResponse.Create(RESTClient);
try
HTTPBasicAuthenticator := THTTPBasicAuthenticator.Create('AC', 'c1234');
try
RESTRequest.ResetToDefaults;
RESTClient.BaseURL := BaseURL;
RESTRequest.Resource := Resource;
HTTPBasicAuthenticator.UserName := AccountSid;
HTTPBasicAuthenticator.Password := AuthToken;
RESTClient.Authenticator := HTTPBasicAuthenticator;
RESTRequest.Client := RESTClient;
RESTRequest.Response := RESTResponse;
// Tried this to fix plivo error with no luck!
// RESTClient.Params.AddHeader('Content-Type', 'application/json');
// "From" number is the send # setup in the twilio or plivo account. The "To" number is a verified number in your twilio or plivo account
if TypeCo='T' then // Twilio
SendSMS( '+1602xxxxx55','+1602xxxxxx7', 'This is a test text from Twilio') // x's were replaced to hide telephone numbers
else if TypeCo='P' then // Plivo
SendSMS( '1602xxxxx66','1602xxxxxx7', 'This is a test text from Plivo'); // x's were replaced to hide telephone numbers
finally
HTTPBasicAuthenticator.Free;
end;
finally
RESTResponse.Free;
end;
finally
RESTRequest.Free;
end;
finally
RESTClient.Free;
end;
end;
function TForm1.SendSMS(aFrom, aTo, aText: string): boolean;
begin
result := True;
RESTRequest.ResetToDefaults;
RESTClient.BaseURL := BaseURL;
RESTRequest.Resource := Resource;
if TypeCo='T' then // Twilio
begin
RESTRequest.Params.AddUrlSegment('AccountSid', accountSid);
RESTRequest.Params.AddItem('From', aFrom);
RESTRequest.Params.AddItem('To', aTo);
RESTRequest.Params.AddItem('Body', aText);
end
else if TypeCo='P' then // Plivo
begin
RESTRequest.Params.AddUrlSegment('AccountSid', accountSid);
RESTRequest.Params.AddItem('src', aFrom);
RESTRequest.Params.AddItem('dst', aTo);
RESTRequest.Params.AddItem('text', aText);
end;
RESTRequest.Method := rmPOST;
RESTRequest.Execute;
// Show Success or Error Message
ErrorMsg.Clear;
ErrorMsg.Lines.Text := RESTResponse.Content;
end;
如前所述,上面的代码对Twilio很有效。但是,对于Plivo,我得到以下错误:
{
"api_id": "b124d512-b8b6-11e5-9861-22000ac69cc8",
"error": "use 'application/json' Content-Type and raw POST with json data"
}
我一直在努力确定如何解决这个问题。我联系了Plivo支持部门,收到了以下回复:
The error "use 'application/json' Content-Type and raw POST with json data" is generated when the Header "Content-Type" is not set the value "application/json"
Please add the Header Content-Type under Items in the Request Tab and set the Description as application/json.
我尝试在按钮过程中添加代码:
RESTClient.Params.AddHeader('Content-Type', 'application/json');
但它仍然给出了相同的错误。我认为这段代码非常接近于为Plivo工作。我是一个新手在休息功能,所以我不知道还有什么可以尝试。我尝试将“application/json”分配给几乎所有接受它的对象,但仍然得到相同的错误。希望其他人能知道Plivo的工作原理。最后,我能够让Twilio和Plivo发送短信。在DelphiREST调试器和注释的帮助下