我目前使用Python Twilio应用服务器,并通过以下代码行将推送功能授予令牌:

@app.route('/accessToken')
def token():
  account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID)
  api_key = os.environ.get("API_KEY", API_KEY)
  api_key_secret = os.environ.get("API_KEY_SECRET", API_KEY_SECRET)
  push_credential_sid = os.environ.get("PUSH_CREDENTIAL_SID", PUSH_CREDENTIAL_SID)
  app_sid = os.environ.get("APP_SID", APP_SID)

  grant = VoiceGrant(
    push_credential_sid=push_credential_sid,
    outgoing_application_sid=app_sid
  )

  token = AccessToken(account_sid, api_key, api_key_secret, IDENTITY)
  token.add_grant(grant)

  return str(token)


有没有办法用PHP做到这一点?我已经用作曲家加载了Twilio依赖项,并且可以很好地获得令牌。我只是不知道如何向令牌添加推送功能。这些行当前生成令牌(但不具有推送功能):

<?php
include('./vendor/autoload.php');
include('./config.php');
include('./randos.php');

use Twilio\Jwt\ClientToken;
use Twilio\Jwt\Grants;

// choose a random username for the connecting user
$identity = $_GET['identity'];

$capability = new ClientToken($TWILIO_ACCOUNT_SID, $TWILIO_AUTH_TOKEN);
$capability->allowClientOutgoing($TWILIO_TWIML_APP_SID);
$capability->allowClientIncoming($identity);
$token = $capability->generateToken();

echo $token;
?>

最佳答案

感谢Zack和Twilio的提示,我终于完成了这项工作!

include('./vendor/autoload.php');
include('./config.php');
include('./randos.php');

use Twilio\Jwt\AccessToken;
use Twilio\Jwt\Grants;
use Twilio\Jwt\Grants\VoiceGrant;
use Twilio\Rest\Client;

// choose a random username for the connecting user
$identity = randomUsername();


$token = new AccessToken($TWILIO_ACCOUNT_SID, $API_KEY, $API_KEY_SECRET, 3600, $identity);

$grant = new VoiceGrant();
$grant->setPushCredentialSid($PUSH_CREDENTIAL_SID);
$grant->setOutgoingApplicationSid($TWILIO_TWIML_APP_SID);
$token->addGrant($grant);

echo $token;

09-19 20:00