使用 PHP 的 Twilio AccessToken 不工作

Twilio AccessToken using PHP not working

我正在尝试在 PHP 中生成一个服务器端 Twilio 可编程语音访问令牌,以便与 Swift iPhone 应用程序一起使用,但它的生成时间太短而失败在应用程序中注册推送通知时。

错误是"Unauthorized answer from a service. Notification exception during registration: TNException: Couldn't create registration"

我可以使用快速入门中的代码在 Python 中使令牌正常

@app.route('/accessToken', methods=['GET', 'POST'])
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 时出现了问题 - 这段代码没有抛出任何错误并生成了一个标记,但它的长度是 550 个字符而不是 570 个字符,而且正如我所说,使用时无法注册。

变量都是一样的,我检查过它们都在那里。

$token = new AccessToken(
    $twilioAccountSid,
    $twilioApiKey,
    $twilioApiSecret,
    3000,
    $identity
);

// Create Voice grant
$VoiceGrant = new VoiceGrant();
$VoiceGrant->setOutgoingApplicationSid($appSid);
$VoiceGrant->setPushCredentialSid($pushCredentialSid);

// Add grant to token
$token->addGrant($VoiceGrant);

// echo token
echo $token;

有什么帮助吗?

此处为 Twilio 开发人员布道师。

在这种情况下最好的办法是确保您已经添加了访问令牌所需的所有授权。

为此,将生成的令牌复制并粘贴到 debugger here。如果您看到赠款或赠款的一部分(如身份缺失),请确保将其正确传递给您的函数。