php – Twilio视频调用API实现
作者:互联网
我正在为我的项目实现视频调用api,当我通过“生成访问令牌”选项从https://www.twilio.com/user/account/video/dev-tools/testing-tools创建访问令牌时,它将为我提供新生成的令牌,当我在下面使用它时
var accessToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImN0eSI6InR3aWxpby1mcGE7dj0xIn0.eyJqdGkiOiJTSzk3ZDI2NGIyOTU1YjM2OWMyOGU4MjA4M2E5MjlmMmE0LTE0NjYwODE1NzQiLCJpc3MiOiJTSzk3ZDI2NGIyOTU1YjM2OWMyOGU4MjA4M2E5MjlmMmE0Iiwic3ViIjoiQUM5ZWE5YjY3ZmM3ZjBjMzE2NTBjNGRmNzkyYzhjYjI2NiIsImV4cCI6MTQ2NjA4NTE3NCwiZ3JhbnRzIjp7ImlkZW50aXR5IjoiQUM5ZWE5YjY3ZmM3ZjBjMzE2NTBjNGRmNzkyYzhjYjI2NiIsInJ0YyI6eyJjb25maWd1cmF0aW9uX3Byb2ZpbGVfc2lkIjoiVlMwNmYzZDdiNTczNGVlYTJhZDdjMWEzYzY4YmMzNjhjNSJ9fX0.976iQ2bMB_tAORxjGkgZFJ-UYGfTidTwfvV0fzySMP0";
var accessManager = new Twilio.AccessManager(accessToken);
它对我很有用.
现在当我使用第二个选项“通过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
伙计们,我在Support的帮助下解决了这个问题.
解
在旧版本的/Services/Twilio/AccessToken.php中,我有一个属性“nbf”:1466743969(不是在时间之前),这意味着它在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”的情况下生成访问令牌.
另一种解决方法是确保服务器时间准确,但您需要管理员访问权限才能进行调整.
解决方法:
您似乎没有包含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访问令牌:
<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>
我希望这能让你走上正轨.
编辑:
我没有意识到上面的脚本是针对Audio的,所以对于未来的用户来说,我想在这里发布视频脚本
>客户端:请求访问令牌(使用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还有更多信息以及有关使用对话here的更多信息
标签:php,twilio,twilio-api,twilio-php 来源: https://codeday.me/bug/20190628/1311077.html