Search code examples
delphitwilio

Send message with Twilio and Delphi


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);

Solution

  • 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.