【问题标题】:Firebase Phone Auth Error 400Firebase 电话身份验证错误 400
【发布时间】:2018-01-19 20:47:25
【问题描述】:

刚刚开始使用 Firebase 电话身份验证。看起来很漂亮,但是我遇到了一个错误。

 {
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "invalid",
    "message": "SESSION_EXPIRED"
   }
  ],
  "code": 400,
  "message": "SESSION_EXPIRED"
 }
}

从验证码开始:(标准文档代码!)

  var applicationVerifier = new firebase.auth.RecaptchaVerifier('recaptcha-container', {
      'size': 'invisible',
      'callback': function(response) {

      },
      'expired-callback': function() {

      }
    });

它的渲染和验证码运行良好。

接下来是将验证码发送到手机的登录位。效果很好:

$scope.signInWithPhoneNumber = function signInWithPhoneNumber() {


        var phoneNumber = "*censored*";
        var appVerifier = window.recaptchaVerifier;
        firebase.auth().signInWithPhoneNumber(phoneNumber, applicationVerifier)
          .then(function (confirmationResult) {
            // SMS sent. Prompt user to type the code from the message, then sign the
            // user in with confirmationResult.confirm(code).
            window.confirmationResult = confirmationResult;




            $scope.setConfirmationResult(confirmationResult);
            alert('Result: ' + JSON.stringify(confirmationResult));
          }).catch(function (error) {
          // Error; SMS not sent
          alert('Error: ' + error);
          // ...
        });
      };

最后是验证用户从短信中输入的代码。这是我收到错误 400 的时候:

$scope.AuthenticateCode = function (code) {


        var code = String(document.getElementById("auth_code").value);

        var confirmationResult = $scope.getConfirmationResult();
        alert(code);
        confirmationResult.confirm(code).then(function (result) {
          // User signed in successfully.
          var user = result.user;
          console.log('Signed In! ' + JSON.stringify(user));
          // ...
        }).catch(function (error) {
          // User couldn't sign in (bad verification code?)
          // ...
        });

      }//end of AuthenticateCode

错误来自 VerifyPhone 方法: https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPhoneNumber?key=审查

有什么帮助或想法吗?

非常感谢, 基兰

【问题讨论】:

    标签: firebase ionic-framework firebase-authentication


    【解决方案1】:

    好的,有两个可能的原因:

    1. 代码已过期。用户提供 SMS 代码并完成登录的时间过长。
    2. 代码已成功使用。我认为这是可能的原因。在这种情况下,您需要获得一个新的 verifyId。通过您正在使用的隐形 reCAPTCHA 获取新的 reCAPTCHA 令牌。

    【讨论】:

      【解决方案2】:

      您最有可能忘记电话号码前的“国家代码”。 这就是为什么firebase会抛出错误400,这意味着参数无效

      【讨论】:

        【解决方案3】:

        如果是 Ionic3 项目,请更改以下几行:

        进口:

        import { AngularFireAuth } from 'angularfire2/auth';
        import firebase from 'firebase';
        

        创建变量:

        public recaptchaVerifier: firebase.auth.RecaptchaVerifier;
        

        关于“ionViewDidLoad()”

        this.recaptchaVerifier = new firebase.auth.RecaptchaVerifier('recaptcha-container');
        

        关于“你的方法(电话号码:号码)”

        const appVerifier = this.recaptchaVerifier;
        const phoneNumberString = "+" + phoneNumber;
        this.fireAuth.auth.signInWithPhoneNumber(phoneNumberString, appVerifier)
          .then(confirmationResult => {
            // SMS sent. Prompt user to type the code from the message, then sign the
            // user in with confirmationResult.confirm(code).
            let prompt = this.alertCtrl.create({
              title: 'Enter the Confirmation code',
              inputs: [{ name: 'confirmationCode', placeholder: 'Confirmation Code' }],
              buttons: [
                {
                  text: 'Cancel',
                  handler: data => { console.log('Cancel clicked'); }
                },
                {
                  text: 'Send',
                  handler: data => {
                    confirmationResult.confirm(data.confirmationCode)
                      .then(result => {
                        // Phone number confirmed
                      }).catch(error => {
                        // Invalid
                        console.log(error);
                      });
                  }
                }
              ]
            });
            prompt.present();
          })
          .catch(error => {
            console.error("SMS not sent", error);
          });
        

        参考: Firebase Phone Number Authentication

        【讨论】:

          【解决方案4】:

          当对谷歌 API 的 POST 请求返回错误请求 400 时,我遇到了类似的情况。当消息被记录时,它说:

          All requests from this device are blocked due to Unusual Activity. Please try again later

          问题是当 ReCaptcha 从我的开发环境中感知到一个机器人时,当我稍后尝试时它运行良好。在其余的开发过程中,为了方便工作,我关闭了此功能。

          【讨论】:

            猜你喜欢
            • 2018-02-21
            • 1970-01-01
            • 2021-06-16
            • 2021-07-13
            • 1970-01-01
            • 2021-03-16
            • 1970-01-01
            相关资源
            最近更新 更多