M.Abulsoud
M.Abulsoud

Reputation: 999

Twilio cURL result in Authentication Error - No credentials provided

I'm struggling with sending a msg from your twilio api, I tested your demos that exists on the twilio website on my local server with the following features: host : (i686-pc-linux-gnu) libcurl php version: 7.35.0 ssl version: OpenSSL/1.0.1f.

It works my local server but on this server with the following features : host : x86_64-redhat-linux-gnu libcurl php version : 7.19.7 ssl version: NSS/3.19.1 Basic ECC it didn't work. Here's the output of the curl request to the twilio api:

{"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}1

the code:

<?php
//my trial account sid, and token
$sid = "XXXXXXXXXXXX"; // Your Account SID from www.twilio.com/user/account
$token = "XXXXXXXXXXXXX"; // Your Auth Token from www.twilio.com/user/account

function send_sms( $sid, $token, $to, $from, $body ) {
    // resource url & authentication
    $uri = 'https://api.twilio.com/2010-04-01/Accounts/' . $sid . '/SMS/Messages.json';
    $auth = $sid . ':' . $token;

    // post string (phone number format= +15554443333 ), case matters
    $fields = 
        '&To=' .  urlencode( $to ) . 
        '&From=' . urlencode( $from ) . 
        '&Body=' . urlencode( $body );

    // start cURL
    $res = curl_init($uri);

    // set cURL options
    curl_setopt( $res, CURLOPT_POST, TRUE ); 
    curl_setopt( $res, CURLOPT_RETURNTRANSFER, TRUE ); // don't echo
    curl_setopt( $res, CURLOPT_SSL_VERIFYPEER, FALSE );
    curl_setopt( $res, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
    curl_setopt( $res, CURLOPT_USERPWD, $auth ); // authenticate
    curl_setopt( $res, CURLOPT_POSTFIELDS, $fields );


    // send cURL
    $result = curl_exec( $res );
    curl_close($res);
    return $result;
}
echo send_sms($sid,$token,"+XXXXXXXX","+XXXXXXXXXXXXXX","TESTING");
?>

I hope you know what is the problem but I think it's related to the ssl version, that included with the php curl extention on the server.

PS: I have no access to the second server so that I cannot upgrade the libcurl extention to the latest version, which I think the reason of the problem.

Upvotes: 2

Views: 2102

Answers (1)

John Hascall
John Hascall

Reputation: 9416

The & separates fields in the POST-DATA, so you do not want the initial one. Change:

$fields = 
    '&To=' .  urlencode( $to ) . 

to:

$fields = 
    'To=' .  urlencode( $to ) . 

[EDIT] One additional difference I see between what you are doing at what I am doing, is I am including the sid and auth directly in the URL:

$uri = "https://" . $sid . ":" + $auth . "@api.twilio.com/2010...";

rather than using:

curl_setopt( $res, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
curl_setopt( $res, CURLOPT_USERPWD, $auth );

but I don't know if that is important or not.

Upvotes: 2

Related Questions