发送消息使用Twilio和Delphi。

huangapple go评论74阅读模式
英文:

Send message with Twilio and Delphi

问题

I've tried to send a message with Twilio, but I got an authentication error:

{ "code": 20003, "message": "Authentication Error - No credentials provided", "more_info": "https://www.twilio.com/docs/errors/20003", "status": 401 }

The Account SID and Auth Token changed as required.

The Code:

// Create environment variables (named below) with your Twilio credentials
client := TTwilioClient.Create(
  GetEnvironmentVariable('YourAccountSID'),
  GetEnvironmentVariable('YourAuthToken'));
// Your Twilio phone number
fromPhoneNumber := '+1234567890';
// Your destination number (for trials, this needs to be your mobile #)
toPhoneNumber := '+9876543210';
// Make a phone call
Writeln('----- Phone Call -----');

allParams := TStringList.Create;
allParams.Add('From=' + fromPhoneNumber);
allParams.Add('To=' + toPhoneNumber);
allParams.Add('Url=http://demo.twilio.com/docs/voice.xml');
// POST to the Calls resource
response := client.Post('Calls', allParams);
if response.Success then
  Writeln('Call SID: ' + response.ResponseData.GetValue<string>('sid'))
else if response.ResponseData <> nil then
  Writeln(response.ResponseData.ToString)
else
  Writeln('HTTP status: ' + response.HTTPResponse.StatusCode.ToString);

(Note: Replace 'YourAccountSID' and 'YourAuthToken' with your actual Twilio credentials, and adjust phone numbers accordingly.)

英文:

I've tried to send message with Twilio

But I got authentication error :

{&quot;code&quot;:20003,&quot;message&quot;:&quot;Authentication Error - No credentials provided&quot;,&quot;more_info&quot;:&quot;https:\/\/www.twilio.com\/docs\/errors\/20003&quot;,&quot;status&quot;:401}

The Account SID and Auth Token changed as required
The Code:

    // Create environment variables (named below) with your Twilio credentials
client := TTwilioClient.Create(
  GetEnvironmentVariable(&#39;*************************a271d22b1&#39;),
  GetEnvironmentVariable(&#39;*************************3647817&#39;));
// Your Twilio phone number
fromPhoneNumber := &#39;+12*******40&#39;;
// Your destination number (for trials, this needs to be your mobile #)
toPhoneNumber := &#39;+**********20&#39;;
// Make a phone call
Writeln(&#39;----- Phone Call -----&#39;);

allParams := TStringList.Create;
allParams.Add(&#39;From=&#39; + fromPhoneNumber);
allParams.Add(&#39;To=&#39; + toPhoneNumber);
allParams.Add(&#39;Url=http://demo.twilio.com/docs/voice.xml&#39;);
// POST to the Calls resource
response := client.Post(&#39;Calls&#39;, allParams);
if response.Success then
  Writeln(&#39;Call SID: &#39; + response.ResponseData.GetValue&lt;string&gt;(&#39;sid&#39;))
else if response.ResponseData &lt;&gt; nil then
  Writeln(response.ResponseData.ToString)
else
  Writeln(&#39;HTTP status: &#39; + response.HTTPResponse.StatusCode.ToString);

答案1

得分: 2

GetEnvironmentVariable 检索一个值,当你传递键时。

假设你有一个键/值对,TWILIO_ACCOUNT_SID=1234356789。要访问值,你需要传递键:GetEnvironmentVariable('TWILIO_ACCOUNT_SID')

在你的代码片段中,你正在传递值本身(即 *************************a271d22b1)。

你应该先设置环境变量。

作为一个快速的解决办法,你可以只传递:

client := TTwilioClient.Create(
  '*************************a271d22b1',
  '*************************3647817');

但你绝对需要隐藏你的凭据。

英文:

GetEnvironmentVariable retrieves a value when you pass in the key.

Let's say you have a key/value pair, TWILIO_ACCOUNT_SID=1234356789. To access the value, you will need to pass in the key: GetEnvironmentVariable(&#39;TWILIO_ACCOUNT_SID&#39;).

In your code snippet, you are passing in the value itself (i.e. *************************a271d22b1)

You should set the environment variables first.

As a quick workaround, you can just pass in:

client := TTwilioClient.Create(
  &#39;*************************a271d22b1&#39;,
  &#39;*************************3647817&#39;);

But you'll definitely want to hide your credentials.

huangapple
  • 本文由 发表于 2023年5月10日 15:41:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76216020.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定