I am trying to send a POST request to Twilio's SMS API from Marketing Cloud's cloudpage using SSJS. I am getting a 401 unauthorized access even though I have added ACCOUNT_SID AND AUTH_TOKEN in the URL.
<script type="text/javascript" runat="server">
Platform.Load("core", "1");
var config = {
endpoint: "https://XXXX:XXXX@api.twilio.com/2010-04-01/Accounts/XXXX/Messages.json",
contentType: "application/x-www-form-urlencoded",
payload : "From=+0000&To=+0000&Body=Test1"
}
try {
var httpResult = HTTP.Post(config.endpoint, config.contentType, config.payload);
var result = JSON.parse(httpResult.response);
Write(httpResult.StatusCode);
Write('result' + result);
} catch(error) { Write(Stringify(error)); }
</script>
I get this error: {"message":"An error occurred when attempting to evaluate a HTTPPost function call. See inner exception for details.","description":"ExactTarget.OMM.FunctionExecutionException: An error occurred when attempting to evaluate a HTTPPost function call. See inner exception for details.\r\n Error Code: OMM_FUNC_EXEC_ERROR\r\n - from Jint --> \r\n\r\n --- inner exception 1---\r\n\r\nSystem.Net.WebException: The remote server returned an error: (401) Unauthorized. - from System\r\n\r\n\r\n\r\n"}
I have tried using client side JavaScript, but I get the same error.
<script>
const url = 'https://XXXX:XXXX@api.twilio.com/2010-04-01/Accounts/XXXX/Messages.json';
var xhr = new XMLHttpRequest();
xhr.open("POST", url);
xhr.setRequestHeader("Accept", "application/x-www-form-urlencoded");
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
var data = 'From=+0000&To=+0000&Body=Test1';
xhr.send(data);
</script>