Web Development with Visual FoxPro
wwHTTP Authorization Header for Twillio
Gravatar is a globally recognized avatar based on your email address. wwHTTP Authorization Header for Twillio
  Carl Chambers
  All
  Nov 18, 2016 @ 12:42pm

Hi All,

I'm trying to implement Twillio in my WebConnection app without using LibCurl. I've implemented Stripe with plain wwHTTP calls but I'm having trouble with the Authorization Header for Twillio.

Here's the sample Curl code from Twillio's API Reference.

curl -X POST 'https://api.twilio.com/2010-04-01/Accounts/your_account_sid/Messages.json' \
--data-urlencode 'To=+15558675309'  \
--data-urlencode 'From=+15017250604'  \
--data-urlencode 'Body=This is the ship that made the Kessel Run in fourteen parsecs?'  \
-d 'MediaUrl=https://c1.staticflickr.com/3/2899/14341091933_1e92e62d12_b.jpg' \
-u your_account_sid:your_auth_token

So I've tried making the Authorization Header by using...

loHTTP.AddHeader("Authorization",lcAccountSID + ":" + lcAuthToken)

without success. It produced the following error.

{"code": 20003, "detail": "Your AccountSid or AuthToken was incorrect.", "message": "Authentication Error - No credentials provided", "more_info": "https://www.twilio.com/docs/errors/20003", "status": 401}

I copied/pasted the credentials from my dashboard so I'm confident that they are correct. There's something wrong with the way I'm creating the Authorization Header. Does anyone have any experience with this?

Thanks, Carl

Gravatar is a globally recognized avatar based on your email address. re: wwHTTP Authorization Header for Twillio
  Rick Strahl
  Carl Chambers
  Nov 18, 2016 @ 12:53pm

Best thing as always is to use a Proxy and capture the HTTP response. It's not always clear how exactly data is supposed to be encoded in a CURL trace so I would just go to the source of an HTTP header. I have no idea what

Run the CURL request with the correct data, and run Fiddler (or other HTTP proxy) to capture the request to see what actually goes over the wire - that'll be much easier to duplicate with wwHttp. I'm pretty sure the authentication isn't supposed to go over the wire in clear text - most likely it's basic auth. You should use username and password properties to set those most likley. But again a REAL trace will verify that.

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: wwHTTP Authorization Header for Twillio
  Carl Chambers
  Rick Strahl
  Nov 18, 2016 @ 09:24pm

Hi Rick,

Unfortunately, I don't know how to do any of what you suggested.
However, I did find the solution on StackOverflow.

In case anyone is interested, here's my little test program that works for sending SMS.

*- Twilio Test SMS
SET PATH TO "C:\WCONNECT\CLASSES\" additive

*- Credentials
lcAccountSID = <insert_your_account_sid>
lcAuthToken = <insert_your_auth_token>

*- Authorization Header String
*- Converted to Base64
lcHeader = "Basic " + STRCONV(lcAccountSID + ":" + lcAuthToken, 13)

lcTo = <insert_dest_phone>
lcFrom = <insert_your_twilio_phone>
lcBody = "West-Wind + Twilio = Cool Stuff !"

lcURL = "https://api.twilio.com/2010-04-01/Accounts/" ;
      + ALLTRIM(lcAccountSID) + '/SMS/Messages.json'
   
DO wwHTTP.prg
loHTTP = CREATEOBJECT("wwHTTP")
loHTTP.AddHeader("Authorization",lcHeader)
loHTTP.AddPostKey("To",lcTo)
loHTTP.AddPostKey("From",lcFrom)
loHTTP.AddPostKey("Body",lcBody)
lcResponse = loHTTP.HTTPGet(lcURL)

MESSAGEBOX(lcResponse)

Carl

Gravatar is a globally recognized avatar based on your email address. re: wwHTTP Authorization Header for Twillio
  VFPJCOOK
  Carl Chambers
  Nov 22, 2021 @ 02:03pm

Rick,

I have been using Twilio to send text messages to myself from my VFP9 app when my interfaces go down. It uses a TwilioX.prg and libcurl developed by a VFPX project many years ago. Twilio recently informed me it will not work after Jan 31, 2022 because it is using too old a version of TLS. Many many hours of research led me to this thread which ends with a program written by Carl Chambers in 2016 to use Twilio to send text messages. I thought I would give it a try as I do not believe I will be able to modify the TwilioX program to meet newer TLS standards.

When I try using Carl's program I get to the line:

loHTTP.AddHeader("Authorization",lcHeader)

where I get an error "property addheader is not found" I have West Wind Web Connection and I see in the documentation for wwHTTP the class member addheader. Can you tell me why it is giving the error?

Thanks, John/Bulletproof Software

Gravatar is a globally recognized avatar based on your email address. re: wwHTTP Authorization Header for Twillio
  Bob Roenigk
  VFPJCOOK
  Nov 22, 2021 @ 08:01pm

Carl,

I use the following code for Twilio SMS messages.

~bob

    * Send message via Twilio
    oHTTP    = CREATEOBJECT("wwHttp")
    oHTTP.nconnecttimeout    = 2
    oHTTP.AddPostKey("ACCOUNT_SID","ACd02xxxxxxxxxxxxxxxxxxxxxxxxxxx")
    oHTTP.AddPostKey("AUTH_TOKEN","e527f9xxxxxxxxxxxxxxxxxxxxxxxxxxx")
    oHTTP.AddPostKey("To",lcPhone)
    oHTTP.AddPostKey("From","+12125551212")
    oHTTP.AddPostKey("Body",lcName + ", " + lcMessage)

    * Call Twilio API
    lcStatus    = oHTTP.HTTPGet("https://api.twilio.com/2010-04-01/Accounts/ACd02xxxxxxxxxxxxxxxxxxxxxxxxxxxx/Messages.json","ACd02xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","e527f9xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","")
    IF oHTTP.cResultCode <> "201"
        * Query Error.
    ENDIF

    oHTTP = .NULL.

Gravatar is a globally recognized avatar based on your email address. re: wwHTTP Authorization Header for Twillio
  VFPJCOOK
  Bob Roenigk
  Nov 23, 2021 @ 03:35am

Bob, That works and I thank you sooooo much. John

© 1996-2024