Twilio 视频通话 API 实施
Twilio Video Calling API implementation
我正在为我的项目实施视频通话 api,当我通过 "Generate Access Token" 选项从 https://www.twilio.com/user/account/video/dev-tools/testing-tools 创建访问令牌时,它将提供我是新生成的令牌,当我如下使用它时
var accessToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImN0eSI6InR3aWxpby1mcGE7dj0xIn0.eyJqdGkiOiJTSzk3ZDI2NGIyOTU1YjM2OWMyOGU4MjA4M2E5MjlmMmE0LTE0NjYwODE1NzQiLCJpc3MiOiJTSzk3ZDI2NGIyOTU1YjM2OWMyOGU4MjA4M2E5MjlmMmE0Iiwic3ViIjoiQUM5ZWE5YjY3ZmM3ZjBjMzE2NTBjNGRmNzkyYzhjYjI2NiIsImV4cCI6MTQ2NjA4NTE3NCwiZ3JhbnRzIjp7ImlkZW50aXR5IjoiQUM5ZWE5YjY3ZmM3ZjBjMzE2NTBjNGRmNzkyYzhjYjI2NiIsInJ0YyI6eyJjb25maWd1cmF0aW9uX3Byb2ZpbGVfc2lkIjoiVlMwNmYzZDdiNTczNGVlYTJhZDdjMWEzYzY4YmMzNjhjNSJ9fX0.976iQ2bMB_tAORxjGkgZFJ-UYGfTidTwfvV0fzySMP0";
var accessManager = new Twilio.AccessManager(accessToken);
它对我很有用。
现在,当我使用第二个选项 "Generate an Access Token via Helper Library with PHP" 并尝试使用
生成令牌时
// Create an Access Token
$token = new Services_Twilio_AccessToken(
$accountSid,
$apiKeySid,
$apiKeySecret,
$ttl=3600,
$identity=$identity
);
// Grant access to Conversations
$grant = new Services_Twilio_Auth_ConversationsGrant();
$grant->setConfigurationProfileSid($configurationProfileSid);
$token->addGrant($grant);
echo json_encode(array(
'identity' => $identity,
'token' => $token->toJWT(),
));
它也会为我生成令牌,但是当我使用生成的令牌作为
响应令牌:
{"identity":"GoldenZeldaGunsight","token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImN0eSI6InR3aWxpby1mcGE7dj0xIn0
.eyJqdGkiOiJTSzk3ZDI2NGIyOTU1YjM2OWMyOGU4MjA4M2E5MjlmMmE0LTE0NjYxMzc4MTkiLCJpc3MiOiJTSzk3ZDI2NGIyOTU
1YjM2OWMyOGU4MjA4M2E5MjlmMmE0Iiwic3ViIjoiQUM5ZWE5YjY3ZmM3ZjBjMzE2NTBjNGRmNzkyYzhjYjI2NiIsIm5iZiI6MTQ
2NjEzNzgxOSwiZXhwIjoxNDY2MTQxNDE5LCJncmFudHMiOnsiaWRlbnRpdHkiOiJHb2xkZW5aZWxkYUd1bnNpZ2h0IiwicnRjIjp7ImNvbmZpZ3VyYXRpb25fcHJvZmlsZV9zaWQiOiJWUzA2ZjNkN2I1NzM0ZWVhMmFkN2MxYTNjNjhiYzM2OGM1In19fQ
.vkOxI1tMIWNQVm1AUL1ySTZY5ZyjYDTdvWCKnVIkLmk"}
我在
中使用了这个响应令牌
var accessManager = new Twilio.AccessManager(data.token);
它对我不起作用。它会给我错误
"Could not connect to Twilio: undefined..."
回复:
Thu Jun 23 2016 13:22:58 GMT+0530 (India Standard Time) | sip.transport | received WebSocket text message:
SIP/2.0 403 Forbidden
CSeq: 81 REGISTER
Call-ID: 4704t9hejng0hmhih2ui8m
From: <sip:AC9ea9b67fc7f0c31650c4df792c8cb266@AC9ea9b67fc7f0c31650c4df792c8cb266.endpoint.twilio.com>;tag=l3bsmhddlr
To: <sip:AC9ea9b67fc7f0c31650c4df792c8cb266@AC9ea9b67fc7f0c31650c4df792c8cb266.endpoint.twilio.com>;tag=72132201_50f4772f_8dc5c7c0-ce8e-4a6e-803a-72f6c761338b
Via: SIP/2.0/WSS 127.0.0.1:60911;branch=z9hG4bK2792018;rport=60911
Server: Twilio
X-Twilio-Error: 31201 Authentication failed
Content-Length: 0
我还在 https://jwt.io 上测试了这两个令牌,它对我来说很完美。
任何人都可以帮助我解决我的代码错误的问题,如果你愿意,我也可以传递其他凭据详细信息吗?
提前致谢。
Solved
伙计们,我已经在支持人员的帮助下解决了这个问题。
解决方案
在我的旧版本 /Services/Twilio/AccessToken.php
中,我有一个属性 "nbf":1466743969(not-before time), which means it is not valid until
04:52:49 UTC. However according to the timestamp in the log file it was
04:50:27 UTC ( 10:20:27 IST)`。如果生成访问令牌的服务器的系统时钟快几分钟,它将导致无效的不早于时间。
更高版本的 twilio-php 库默认省略了 "nbf"
属性,因为这个时钟偏差问题。如果您在这里获得最新版本的 AccessToken.php:
https://github.com/twilio/twilio-php/blob/master/Services/Twilio/AccessToken.php
它将生成没有 "nbf"
的访问令牌。
另一种解决方法是确保服务器时间准确,但您需要管理员访问权限才能进行调整。
您似乎没有包含 twilio 的 capability.php。根据 twilio docs,您的 php 应该类似于此
include '/pathto/Services/Twilio/Capability.php';
// Twilio API credentials here
$accountSid = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$authToken = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy';
$capability = new Services_Twilio_Capability($accountSid, $authToken);
$capability->allowClientOutgoing('APabe7650f654fc34655fc81ae71caa3ff');
$token = $capability->generateToken();
然后你通过js访问token:
<script type="text/javascript">
Twilio.Device.setup("<?php echo $token; ?>");
Twilio.Device.ready(function (device) {
$("#log").text("Ready");
});
Twilio.Device.error(function (error) {
$("#log").text("Error: " + error.message);
});
Twilio.Device.connect(function (conn) {
$("#log").text("Successfully established call");
});
function call() {
Twilio.Device.connect();
}
</script>
希望这能让您走上正轨。
编辑:
我没有意识到上面的脚本是用于音频的,所以为了以后的用户,我想我会 post 这里的视频脚本
客户端:请求访问令牌(使用jquery)
$.getJSON('/token', function(data) {
// The data sent back from the server should contain a long string - this is
// the JWT token you need to initialize the SDK. Read more about JWT
// (JSON Web Token) at http://jwt.io
console.log(data.token);
//If your app has an existing authentication system, you can use
// the e-mail address or username that uniquely identifies a user instead.
console.log(data.identity);
});
服务器:创建访问令牌(php)
<?php
require_once('./twilio-php/Services/Twilio.php');
require_once('./randos.php');
require_once('./config.php');
// choose a random username for the connecting user
$identity = randomUsername();
// Create access token, which we will serialize and send to the client
$token = new Services_Twilio_AccessToken(
$TWILIO_ACCOUNT_SID,
$TWILIO_API_KEY,
$TWILIO_API_SECRET,
3600,
$identity
);
// Grant access to Conversations
$grant = new Services_Twilio_Auth_ConversationsGrant();
$grant->setConfigurationProfileSid($TWILIO_CONFIGURATION_SID);
$token->addGrant($grant);
// return serialized token and the user's randomly generated ID
echo json_encode(array(
'identity' => $identity,
'token' => $token->toJWT(),
));
客户端:使用访问令牌初始化 SDK
// Create an AccessManager to manage our Access Token
var accessManager = new Twilio.AccessManager('$TWILIO_ACCESS_TOKEN');
// Create a Conversations Client and connect to Twilio's backend
conversationsClient = new Twilio.Conversations.Client(accessManager);
conversationsClient.listen().then(function() {
console.log('Connected to Twilio!');
}, function (error) {
console.log('Could not connect to Twilio: ' + error.message);
});
The Twilio Video Docs and more information on working with conversations here
上有更多信息
我在支持人员的帮助下解决了这个问题。
解决方案
在我的旧版本 /Services/Twilio/AccessToken.php
中,我有一个属性 "nbf": 1466743969(not-before time)
,这意味着它在 04:52:49 UTC 之前无效。然而,根据日志文件中的时间戳,它是 04:50:27 UTC (10:20:27 IST)`。如果生成访问令牌的服务器的系统时钟快几分钟,它将导致无效的不早于时间。
更高版本的 twilio-php 库默认省略了 "nbf" 属性,因为这个时钟偏差问题。如果您在这里获得最新版本的 AccessToken.php:
https://github.com/twilio/twilio-php/blob/master/Services/Twilio/AccessToken.php
它将生成没有 "nbf" 的访问令牌。
另一种解决方法是确保服务器时间准确,但您需要管理员访问权限才能进行调整。
我正在为我的项目实施视频通话 api,当我通过 "Generate Access Token" 选项从 https://www.twilio.com/user/account/video/dev-tools/testing-tools 创建访问令牌时,它将提供我是新生成的令牌,当我如下使用它时
var accessToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImN0eSI6InR3aWxpby1mcGE7dj0xIn0.eyJqdGkiOiJTSzk3ZDI2NGIyOTU1YjM2OWMyOGU4MjA4M2E5MjlmMmE0LTE0NjYwODE1NzQiLCJpc3MiOiJTSzk3ZDI2NGIyOTU1YjM2OWMyOGU4MjA4M2E5MjlmMmE0Iiwic3ViIjoiQUM5ZWE5YjY3ZmM3ZjBjMzE2NTBjNGRmNzkyYzhjYjI2NiIsImV4cCI6MTQ2NjA4NTE3NCwiZ3JhbnRzIjp7ImlkZW50aXR5IjoiQUM5ZWE5YjY3ZmM3ZjBjMzE2NTBjNGRmNzkyYzhjYjI2NiIsInJ0YyI6eyJjb25maWd1cmF0aW9uX3Byb2ZpbGVfc2lkIjoiVlMwNmYzZDdiNTczNGVlYTJhZDdjMWEzYzY4YmMzNjhjNSJ9fX0.976iQ2bMB_tAORxjGkgZFJ-UYGfTidTwfvV0fzySMP0";
var accessManager = new Twilio.AccessManager(accessToken);
它对我很有用。
现在,当我使用第二个选项 "Generate an Access Token via Helper Library with PHP" 并尝试使用
生成令牌时// Create an Access Token
$token = new Services_Twilio_AccessToken(
$accountSid,
$apiKeySid,
$apiKeySecret,
$ttl=3600,
$identity=$identity
);
// Grant access to Conversations
$grant = new Services_Twilio_Auth_ConversationsGrant();
$grant->setConfigurationProfileSid($configurationProfileSid);
$token->addGrant($grant);
echo json_encode(array(
'identity' => $identity,
'token' => $token->toJWT(),
));
它也会为我生成令牌,但是当我使用生成的令牌作为
响应令牌:
{"identity":"GoldenZeldaGunsight","token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImN0eSI6InR3aWxpby1mcGE7dj0xIn0
.eyJqdGkiOiJTSzk3ZDI2NGIyOTU1YjM2OWMyOGU4MjA4M2E5MjlmMmE0LTE0NjYxMzc4MTkiLCJpc3MiOiJTSzk3ZDI2NGIyOTU
1YjM2OWMyOGU4MjA4M2E5MjlmMmE0Iiwic3ViIjoiQUM5ZWE5YjY3ZmM3ZjBjMzE2NTBjNGRmNzkyYzhjYjI2NiIsIm5iZiI6MTQ
2NjEzNzgxOSwiZXhwIjoxNDY2MTQxNDE5LCJncmFudHMiOnsiaWRlbnRpdHkiOiJHb2xkZW5aZWxkYUd1bnNpZ2h0IiwicnRjIjp7ImNvbmZpZ3VyYXRpb25fcHJvZmlsZV9zaWQiOiJWUzA2ZjNkN2I1NzM0ZWVhMmFkN2MxYTNjNjhiYzM2OGM1In19fQ
.vkOxI1tMIWNQVm1AUL1ySTZY5ZyjYDTdvWCKnVIkLmk"}
我在
中使用了这个响应令牌var accessManager = new Twilio.AccessManager(data.token);
它对我不起作用。它会给我错误
"Could not connect to Twilio: undefined..."
回复:
Thu Jun 23 2016 13:22:58 GMT+0530 (India Standard Time) | sip.transport | received WebSocket text message:
SIP/2.0 403 Forbidden
CSeq: 81 REGISTER
Call-ID: 4704t9hejng0hmhih2ui8m
From: <sip:AC9ea9b67fc7f0c31650c4df792c8cb266@AC9ea9b67fc7f0c31650c4df792c8cb266.endpoint.twilio.com>;tag=l3bsmhddlr
To: <sip:AC9ea9b67fc7f0c31650c4df792c8cb266@AC9ea9b67fc7f0c31650c4df792c8cb266.endpoint.twilio.com>;tag=72132201_50f4772f_8dc5c7c0-ce8e-4a6e-803a-72f6c761338b
Via: SIP/2.0/WSS 127.0.0.1:60911;branch=z9hG4bK2792018;rport=60911
Server: Twilio
X-Twilio-Error: 31201 Authentication failed
Content-Length: 0
我还在 https://jwt.io 上测试了这两个令牌,它对我来说很完美。
任何人都可以帮助我解决我的代码错误的问题,如果你愿意,我也可以传递其他凭据详细信息吗?
提前致谢。
Solved
伙计们,我已经在支持人员的帮助下解决了这个问题。
解决方案
在我的旧版本 /Services/Twilio/AccessToken.php
中,我有一个属性 "nbf":1466743969(not-before time), which means it is not valid until
04:52:49 UTC. However according to the timestamp in the log file it was
04:50:27 UTC ( 10:20:27 IST)`。如果生成访问令牌的服务器的系统时钟快几分钟,它将导致无效的不早于时间。
更高版本的 twilio-php 库默认省略了 "nbf"
属性,因为这个时钟偏差问题。如果您在这里获得最新版本的 AccessToken.php:
https://github.com/twilio/twilio-php/blob/master/Services/Twilio/AccessToken.php
它将生成没有 "nbf"
的访问令牌。
另一种解决方法是确保服务器时间准确,但您需要管理员访问权限才能进行调整。
您似乎没有包含 twilio 的 capability.php。根据 twilio docs,您的 php 应该类似于此
include '/pathto/Services/Twilio/Capability.php';
// Twilio API credentials here
$accountSid = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$authToken = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy';
$capability = new Services_Twilio_Capability($accountSid, $authToken);
$capability->allowClientOutgoing('APabe7650f654fc34655fc81ae71caa3ff');
$token = $capability->generateToken();
然后你通过js访问token:
<script type="text/javascript">
Twilio.Device.setup("<?php echo $token; ?>");
Twilio.Device.ready(function (device) {
$("#log").text("Ready");
});
Twilio.Device.error(function (error) {
$("#log").text("Error: " + error.message);
});
Twilio.Device.connect(function (conn) {
$("#log").text("Successfully established call");
});
function call() {
Twilio.Device.connect();
}
</script>
希望这能让您走上正轨。
编辑:
我没有意识到上面的脚本是用于音频的,所以为了以后的用户,我想我会 post 这里的视频脚本
客户端:请求访问令牌(使用jquery)
$.getJSON('/token', function(data) { // The data sent back from the server should contain a long string - this is // the JWT token you need to initialize the SDK. Read more about JWT // (JSON Web Token) at http://jwt.io console.log(data.token); //If your app has an existing authentication system, you can use // the e-mail address or username that uniquely identifies a user instead. console.log(data.identity); });
服务器:创建访问令牌(php)
<?php require_once('./twilio-php/Services/Twilio.php'); require_once('./randos.php'); require_once('./config.php'); // choose a random username for the connecting user $identity = randomUsername(); // Create access token, which we will serialize and send to the client $token = new Services_Twilio_AccessToken( $TWILIO_ACCOUNT_SID, $TWILIO_API_KEY, $TWILIO_API_SECRET, 3600, $identity ); // Grant access to Conversations $grant = new Services_Twilio_Auth_ConversationsGrant(); $grant->setConfigurationProfileSid($TWILIO_CONFIGURATION_SID); $token->addGrant($grant); // return serialized token and the user's randomly generated ID echo json_encode(array( 'identity' => $identity, 'token' => $token->toJWT(), ));
客户端:使用访问令牌初始化 SDK
// Create an AccessManager to manage our Access Token var accessManager = new Twilio.AccessManager('$TWILIO_ACCESS_TOKEN'); // Create a Conversations Client and connect to Twilio's backend conversationsClient = new Twilio.Conversations.Client(accessManager); conversationsClient.listen().then(function() { console.log('Connected to Twilio!'); }, function (error) { console.log('Could not connect to Twilio: ' + error.message); });
The Twilio Video Docs and more information on working with conversations here
上有更多信息我在支持人员的帮助下解决了这个问题。
解决方案
在我的旧版本 /Services/Twilio/AccessToken.php
中,我有一个属性 "nbf": 1466743969(not-before time)
,这意味着它在 04:52:49 UTC 之前无效。然而,根据日志文件中的时间戳,它是 04:50:27 UTC (10:20:27 IST)`。如果生成访问令牌的服务器的系统时钟快几分钟,它将导致无效的不早于时间。
更高版本的 twilio-php 库默认省略了 "nbf" 属性,因为这个时钟偏差问题。如果您在这里获得最新版本的 AccessToken.php:
https://github.com/twilio/twilio-php/blob/master/Services/Twilio/AccessToken.php
它将生成没有 "nbf" 的访问令牌。
另一种解决方法是确保服务器时间准确,但您需要管理员访问权限才能进行调整。