【问题标题】:How do I send a user token via SMS with Authy and PHP?如何使用 Authy 和 PHP 通过 SMS 发送用户令牌?
【发布时间】:2015-10-31 00:30:45
【问题描述】:

我正在尝试在 PHP 中运行 Authy 的演示。我已经用 Composer 安装了 Authy 库,所以现在我可以使用这样的硬编码值来注册用户:

$authy_api = new Authy\AuthyApi('<TESTING API KEY>', 'http://sandbox-api.authy.com'); // actual key omitted -- using the key generated for testing, not the one for production

$user = $authy_api->registerUser('something@something.com', '999-999-9999', 30); // actual credentials omitted

if($user->ok()){
    echo "Success!";
    $id = $user->id();
    echo($id);  
}

当上面的脚本运行时,确实生成了一个 5 位数的用户 ID,所以一切似乎都很顺利,但 SMS 从未发送到我的手机。

一个可能的问题可能是我的号码已经注册为应用电话(与管理员帐户相关联),因此(根据文档)每个电话号码都必须唯一标识一个用户,也许我的号码已经为此注册了应用程序,因此无需发送新令牌。如果是这种情况,用户对象的 id 可能是之前注册的。

但是,其他电话号码仍然存在问题。所以现在我迷路了。

【问题讨论】:

    标签: php twilio twilio-php two-factor-authentication authy


    【解决方案1】:

    Guybrush

    两件事导致您缺少短信。

    1. 您的代码示例仅用于用户注册。然后,您需要调用第二个 API 来发送 SMS。

    https://docs.authy.com/totp.html#requesting-sms-codes

    1. 但是,上述 API 调用可能不会生成 SMS。您确实是正确的,您使用的手机号码与您的 Authy 应用程序相关联的事实默认情况下不会导致 SMS 消息。这里的想法是,由于 Authy 客户必须在 Authy 交易之上为 SMS 消息付费,因此那些使用移动应用程序的用户不需要 SMS。

    但是可以覆盖此行为。

    所以你的最终代码是;

    $authy_api = new Authy\AuthyApi('<TESTING API KEY>', 'http://sandbox-api.authy.com'); // actual key omitted -- using the key generated for testing, not the one for production
    
    $user = $authy_api->registerUser('something@something.com', '999-999-9999', 30); // actual credentials omitted
    
    if($user->ok()){  
        echo "Success!";
        $id = $user->id();
        echo($id);  
    
        // Now send SMS, force sending a message even if the user has the Authy mobile app installed
       $sms = $authy_api->requestSms('authy-id', array("force" => "true"));    }
    

    西蒙

    【讨论】:

      【解决方案2】:

      事实证明,代码本身没有任何问题。

      只是沙盒 API 实际上并没有通过发送 SMS。它只是为了测试目的而模拟过程,因此认证流程与生产 API 相同。

      当我切换到生产 API URL 和密钥时,我能够在手机上接收短信。

      【讨论】:

        【解决方案3】:
         $authy_api = new AuthyApi(AUTHY_API_KEY, "https://api.authy.com");
         $user = $authy_api->registerUser($email, $phone_number, $country_code);   //// Register User
         if($user->ok()) {
             $sms = $authy_api->requestSms($user->id()); //// Send SMS
             return $sms->ok();
        }
        

        【讨论】:

          猜你喜欢
          • 2010-11-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多