twilio sipendpoint call is failed - twilio

I have used following code make a call to twilio sipendpoint. But i'm getting call failed status.
The call flow is like this when agent click on customer number, first i'm connecting to agent to his sip url like this sip:name#sip.twilio.com on request call back i'll connect to customer number using dial verb.
The sip log gives following error
X-Twilio-Error: 400: Bad value for header: X-Twilio-OutboundRouteCount
$client = new Services_Twilio($sid, $token);
$client->account->calls->create(
'name',
'sip:name#sip.twilio.com',
'http://twiliocallback,
array(
'Record' => true,
'Method' => "GET",
'StatusCallback' => 'http://url,
'StatusCallbackMethod' => 'GET',
'StatusCallbackEvent' => array("initiated", "ringing", "answered", "completed"),
'SipAuthPassword' => '****',
'SipAuthUsername' => '****'
));

Related

How to fix getFederationToken returns 403 not authorized error after upgrading from SDK version 1 to 3

We have existing code that uses AWS PHP SDK version 1 AmazonSTS()->get_federation_token(). After upgrading to SDK version 3, the same call using the same credentials, resource and policy returns a 403 not authorized error.
Version and region appear to be the same. The policy json is the same. The user credentials used to make the call are the same (and if I switch back to the SDK v1 they still work). I have the debug option set but it doesn't appear to provide any additional information as to why the same user it not authorized to perform the same function getFederationToken on the same federated user.
Old code that works:
$client = new AmazonSTS();
$policy = new stdClass();
$policy->Statement = [
'Sid' => 'randomstatementid' . time(),
'Action' => ['s3:*'],
'Effect' => 'Allow',
'Resource' => 'aws:s3:::' . $AWS_BUCKET . '*'
];
// Fetch the session credentials.
$response = $client->get_federation_token('User1',array(
'Policy' => json_encode($policy),
'DurationSeconds' => $NUMSECS
));
New code that returns 403 error:
$client = new Aws\Sts\StsClient([
'region' => 'us-east-1',
'version' => '2011-06-15',
]);
$policy = new stdClass();
$policy->Statement = [
'Sid' => 'randomstatementid' . time(),
'Action' => ['s3:*'],
'Effect' => 'Allow',
'Resource' => 'aws:s3:::' . $AWS_BUCKET . '*'
];
try {
$response = $client->getFederationToken([
'Name' => 'User1',
'Policy' => json_encode($policy),
'DurationSeconds' => $NUMSECS,
]);
} catch (Exception $e) {
var_dump($e);
die();
}
The first example returns temporary credentials for the federated user User1.
The second example returns 403 forbidden error (I'm hiding the actual account id):
<Error>
<Type>Sender</Type>
<Code>AccessDenied</Code>
<Message>User: arn:aws:iam::[account id]:user/portal is not authorized to perform: sts:GetFederationToken on resource: arn:aws:sts::[account id]:federated-user/User1</Message>
</Error>
Turns out I was looking at the wrong credentials. I found the correct credentials hard-coded in the script :(

Twilio Browser Call Sends Wrong Call Sid in POST

I have setup browser calls and in my TwiML App , setup the Status CallBack url . Twilio posts the status in the format
array (
'ApiVersion' => '2010-04-01',
'Called' => NULL,
'CallStatus' => 'completed',
'Duration' => '1',
'From' => 'client:browser',
'CallDuration' => '13',
'Direction' => 'inbound',
'Timestamp' => 'Mon, 16 Apr 2018 19:35:26 +0000',
'AccountSid' => 'asdasdasdadasdasdasdasdasdasdas',
'CallbackSource' => 'call-progress-events',
'ApplicationSid' => 'rwerwerwerwewewewewwrwe',
'Caller' => 'client:browser',
'SequenceNumber' => '0',
'To' => NULL,
'CallSid' => 'CA9c6408108969fsf6f1c53396003d931',
)
I am trying to get the call to ,from and other details. I did was
$callsid = $_POST['CallSid'];
$call = $client->calls($callsid)->fetch();
But it failed everytime. When I checked my dashboard for the CallSod , it is different from what they sent in the webhook.
Is there something I am missing ?
Resolved it. I need to get the Child Call SID to get that information .
$call = $client->calls->read(
array("ParentCallSid" => $callsid )
);

Authorization Required on Twitter Stream Api using Guzzle PHP

I want to use the Twitter Streaming Api with the filter query.
And following the GuzzleHttp documentation I'm accessing like this:
<?php
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Subscriber\Oauth\Oauth1;
$stack = HandlerStack::create();
$middleware = new Oauth1([
'consumer_key' => 'my_key',
'consumer_secret' => 'my_secret',
'token' => 'my_token',
'token_secret' => 'my_token_secret'
]);
$stack->push($middleware);
$client = new Client([
'base_uri' => 'https://stream.twitter.com/1.1/',
'handler' => $stack,
'auth' => 'oauth'
]);
$client->post('statuses/filter.json', [
'form_params' => [
'track' => 'some_word_to_track',
],
]);
But when executing this code, I'm getting the following exception:
PHP Fatal error: Uncaught GuzzleHttp\Exception\ClientException:
Client error: 'POST https://stream.twitter.com/statuses/filter.json'
resulted in a '401 Authorization Required' response
in /Users/.../vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:111
Conclusion:
Is it because I cannot use GuzzleHttp's Oauth1 Subscriber with
Twitter streaming API?
Or is it because I missing something on the
authentication?

Connecting to Twilio SMS via a Proxy in PHP

I'm testing Twilio to use as our SMS solution however I'm having issues getting it to work behind our proxy server.
I've tried:
$twiliohttp = new Services_Twilio_TinyHttp(
"https://api.twilio.com",
array("curlopts" => array(
CURLOPT_USERAGENT => self::USER_AGENT,
CURLOPT_HTTPHEADER => array('Accept-Charset: utf-8'),
CURLOPT_CAINFO => dirname(__FILE__) . '/cacert.pem',
CURLOPT_PROXY => '3.X.X.X:9400',
))
);
$client = new Services_Twilio($account_sid, $auth_token, null, $twiliohttp );
$message = $client->account->messages->sendMessage(
'+441432XXXX31', // From a Twilio number in your account
'+44776XXXX712', // Text any number
"Hello monkey!"
);
I then get the error: Fatal error: Cannot access self:: when no class scope is active in /var/www/twiliosms.php on line 16
So I modified the Twilio.php file modifying the curlopts array to add:
CURLOPT_PROXY => '3.X.X.X:9400',
and calling Twilio with:
$client = new Services_Twilio($account_sid, $auth_token );
$message = $client->account->messages->sendMessage(
'+4414XXXXXXX1', // From a Twilio number in your account
'+4477XXXXXXX2', // Text any number
"Hello monkey!"
);
But I then get the error:
Fatal error: Uncaught exception 'Services_Twilio_RestException' with message 'Could not decode response body as JSON. This likely indicates a 500 server error' in /var/www/GE/includes/SMS/Twilio.php:288
Stack trace:
#0 /var/www/GE/includes/SMS/Twilio.php(181): Base_Services_Twilio->_processResponse(Array)
#1 /var/www/GE/includes/SMS/Twilio/ListResource.php(92): Base_Services_Twilio->createData('/2010-04-01/Acc...', Array)
#2 /var/www/GE/includes/SMS/Twilio/Rest/Messages.php(24): Services_Twilio_ListResource->_create(Array)
#3 /var/www/GE/includes/SMS/Twilio/Rest/Messages.php(71): Services_Twilio_Rest_Messages->create(Array)
#4 /var/www/GE/twiliosms.php(35): Services_Twilio_Rest_Messages->sendMessage('+441432233131', '+447766205712', 'Hello monkey!')
#5 {main}
thrown in /var/www/GE/includes/SMS/Twilio.php on line 288
Any ideas how to make this solution work through a proxy server that doesn't allow inbound connections?
Thanks in advance.
Ricky from Twilio here.
Although I couldn't test with your exact proxy setup I think the first solution you tried will work if you hardcode the user agent. For example:
$twiliohttp = new Services_Twilio_TinyHttp(
"https://api.twilio.com",
array("curlopts" => array(
CURLOPT_USERAGENT => "Twilio Proxy/1.0",
CURLOPT_HTTPHEADER => array('Accept-Charset: utf-8'),
CURLOPT_CAINFO => dirname(__FILE__) . '/cacert.pem',
CURLOPT_PROXY => '3.X.X.X:9400',
))
);
You also may need to make the modification to the TinyHttp library shown here.
Many thanks to RickyRobinett this is how to resolve the issue, I post this answer with the full solution so people don't have to trove through the badly formatted comments.
Update TinyHttp in line with: https://github.com/camuthig/twilio-php/commit/20d4f3c4479c93894866f498e89a0f13cf16d6bf
$twiliohttp = new Services_Twilio_TinyHttp(
"https://api.twilio.com",
array("curlopts" => array(
CURLOPT_USERAGENT => "Twilio Proxy/1.0",
CURLOPT_HTTPHEADER => array('Accept-Charset: utf-8'),
CURLOPT_CAINFO => 'includes/SMS/cacert.pem',
CURLOPT_PROXY => 'X.X.X.X:9400',
))
);
$client = new Services_Twilio($account_sid, $auth_token, null, $twiliohttp );
If you don't have the cacert.pem then checkout this post https://stackoverflow.com/a/31297747/1697288

Failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request - OAuth 2.0 POST

I am working with YouTube APIs for my college project, and I keep getting an error. Here I send them to the authorisation page to log in, when they allow access it sends the $_GET['code'] string back. Then I send this along with some other data and it should send back a JSON object. Instead I am just getting
Warning: file_get_contents(https://accounts.google.com/o/oauth2/token)
[function.file-get-contents]: failed to open stream: HTTP request
failed! HTTP/1.0 400 Bad Request in http://www.example.net/callback.php on line 27
I have replaced my domain with example.net just for security
urlencode($_GET['code']),
'client_id' => urlencode('111522767640.apps.googleusercontent.com '),
'client_secret' => urlencode('secret'),
'redirect_uri' => urlencode('http://example.net/callback.php'),
'grant_type' => urlencode('authorization_code')
)
);
$params =
array('http' =>
array(
'method' => 'POST /o/oauth2/token HTTP/1.1',
'header' => 'Host: accounts.google.com\r\n'.
'Content-Type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($params);
$result = file_get_contents('https://accounts.google.com/o/oauth2/token', false,$context);
var_dump($_SESSION);
var_dump($result);
}
else //If code isnt set, user must have come here erroniously or has denied access to this program
{
//header( 'Location: www.example.net/error.php' ) ;
}
?>
file_get_contents is going to make a GET request to the url specified, but oauth2/token needs a POST request.
See reference Google OAuth2, PHP HTTP.
if you are using oauth2, goto libraries/oauth2/provider.php and uncomment the code line 182 shows
$ci = get_instance();
$ci->load->spark('curl/1.2.1');
$ci->curl
->create($url)
->post($params, array('failonerror' => false));
$response = $ci->curl->execute();
Here's how to do the Google oAuth properly:
$config = (object) array(
'CLIENT_ID' => 'AAAAA',
'CLIENT_SECRET' => 'BBBBB',
'REFRESH_TOKEN' => 'CCCCC',
);
$sJSON = file_get_contents('https://www.googleapis.com/oauth2/v4/token',FALSE,
stream_context_create(array('http'=>array(
'ignore_errors' => TRUE, // see errors in response instead of empty on error
'method' => 'POST',
'header' => array(
'Content-Type: application/x-www-form-urlencoded'
),
'content' => http_build_query(array(
'grant_type' => 'refresh_token',
'client_id' => $config->CLIENT_ID,
'client_secret' => $config->CLIENT_SECRET,
'refresh_token' => $config->REFRESH_TOKEN
))
)))
);
You can then use json_decode() to parse $sJSON into an object and then retrieve the access_token property.
For those who are wondering how to get the CLIENT_ID, CLIENT_SECRET, and REFRESH_TOKEN, watch this video. It's not easy. In my case, I needed to do this for Google Adwords API. So, I had to get my Developer Token and Login Customer ID from https://ads.google.com/home/tools/manager-accounts. Then, I had to go to https://console.developers.google.com/apis/credentials with this guide to generate a Client ID and Client Secret. Then, I followed this guide to learn how to get my Refresh Token.

Resources