Ijas Ahamed N
Ijas Ahamed N

Reputation: 6110

Unable to listen to twilio video javascript endpoint

I am using twilio video javascript service. When i try to listen to a endpoint, its giving an error :

{_errorData: Object, name: "LISTEN_FAILED", message: "Gateway responded with: 31201 Authentication failed"}

I am generating access token using below code :

<?php
    require_once('/path/to/twilio-php/Services/Twilio.php');

    $accountSid = "ACC_SID";
    $signingKeySid = SID;
    $signingKeySecret = SECRET;

    $token = new Services_Twilio_AccessToken($signingKeySid, $accountSid, $signingKeySecret);
    $token->addEndpointGrant(ENDPOINT_NAME);
    $token->enableNTS();
    echo $token->toJWT();

?>

When i use this token in my javascript to start listening to endpoint, it is giving above error.

Javascript code is :

endpoint = new Twilio.Endpoint(token);

endpoint.listen().then(init,function (error) {
    console.log('Could not connect to Twilio: ' + error.message);
});

But, when i use token generated from twilio testing-tool, video cal works. I had updated my php twilio video sdk. But no change in result. This was a working code earlier. Dont know what happened later! Does any one have an answer to solve this issue?

Upvotes: 1

Views: 926

Answers (2)

Bogdan Sulima
Bogdan Sulima

Reputation: 427

Just had the same issue with Twilio Video. In my case, server had a wrong date, so the generated token was invalid. After fixing the date, Twilio authentication started to work again.

Upvotes: 0

gogasca
gogasca

Reputation: 10058

This is what I did, which works in my case:

In my PHP file I have:

<?php
require_once('php/Services/Twilio.php'); // Loads the library

// You will need your Account Sid and a SigningKey Sid and Secret
// to generate an Access Token for your SDK endpoint to connect to Twilio.
$accountSid = "XXXX";
$signingKeySid = "YYYY";
$signingKeySecret = "ZZZZ";

$token = new Services_Twilio_AccessToken($signingKeySid, $accountSid, $signingKeySecret);
$token->addEndpointGrant("gonzalo");
$token->enableNTS();
?>

In my HTML code:

<span id='twilio_token' style='display: none;'><?php echo $token->toJWT()?></span>

In my Javascript:

var accessToken = document.getElementById('twilio_token').innerHTML;
console.log(accessToken);
// create an Endpoint and connect to Twilio
endpoint = new Twilio.Endpoint(accessToken);
endpoint.listen().then(
  endpointConnected,
  function (error) {
    log('Could not connect to Twilio: ' + error.message);
  }
);

When you do a console.log for your Twilio token is it valid?

Upvotes: 1

Related Questions