Twilio example app works but gets unexpected return codes

X2025 R2.1 on Win 10 Pro 22H2

I’m investigating the example Twilio app and have got everything running OK with account details from Twilio. It works, in that the text I input arrives as an SMS on my phone, but I always see an authentication error 20003 status 401 returned from Twilio.

Twilio’s console shows the messages as accepted and sent, and reports no errors.

Any suggestions as to why I’m seeing this code, and anything I can do about it would be welcome.

Thanks

Steve

I don’t use Twilio or have test credentials, so I’m making some educated guesses.

The approach for authentication used in the example is wrong. The API uses HTTP basic authorization, so requests should include an Authorization request header. The Xojo example does not send the authorization with the initial request—there is no mention of the credentials in constructing the request—so the server returns an error message with HTTP status code 401 Unauthorized. This fires the AuthenticationRequested event, at which point the example supplies the credentials to the URLConnection which then uses those to make the request a second time with the correct header.

I’m guessing URLConnections don’t fire the ContentReceived event for content received after making a second request after initially failing from a 401. I’m also guessing the Twilio API originally did not return a JSON body with the 401 result, which probably allowed the Xojo example to appear to be working correctly and show the eventual result body.

Try removing the AuthenticationRequired event handler from the socket. In SendButton.Pressed, manually add the correct credentials to the initial request with something like this before the sending the request:

// Set Authorization header (username is AccountSID, password is AuthToken)
TwilioSocket.RequestHeader("Authorization", "Basic " + EncodeBase64(AccountIDField.Text + ":" + AuthTokenField.Text, 0))

EDIT: Forgot to add “Basic” to the header.

1 Like

Sorry, the Assigns syntax slipped my mind when typing it out. Should be:

// Set Authorization header (username is AccountSID, password is AuthToken)
TwilioSocket.RequestHeader("Authorization") = "Basic " + EncodeBase64(AccountIDField.Text + ":" + AuthTokenField.Text, 0)

This shouldn’t be… it should fire every time content has been received. Can you make a sample project that shows this behavior?

Thanks Travis.

Steve

Yes, I’ll log this issue with an example project at some point over the next few days.

In short, there is a difference in behavior between macOS and Windows for cases where the server returns 401, AuthenticationRequested fires and returns the correct credentials, and then the URLConnection makes the request again with the credentials. On macOS ContentReceived only fires for the second (successful) request, not for the 401. On Windows ContentReceived only fires for the 401 response even though the URLConnection does make a successful connection returning 200.

1 Like

https://tracker.xojo.com/xojoinc/xojo/-/issues/79843

1 Like