英文:
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 :
{"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('*************************a271d22b1'),
GetEnvironmentVariable('*************************3647817'));
// Your Twilio phone number
fromPhoneNumber := '+12*******40';
// Your destination number (for trials, this needs to be your mobile #)
toPhoneNumber := '+**********20';
// 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);
答案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('TWILIO_ACCOUNT_SID')
.
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(
'*************************a271d22b1',
'*************************3647817');
But you'll definitely want to hide your credentials.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论