Firebase Phone Auth Error 400

时间:2017-08-11 10:51:50

标签: firebase ionic-framework firebase-authentication

刚开始使用Firebase手机验证。看起来很漂亮然而我碰到了一个有虫子的墙。

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

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

  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= 截尾

任何帮助或想法?

非常感谢, 基兰

3 个答案:

答案 0 :(得分:0)

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

进口:

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

创建var:

public recaptchaVerifier: firebase.auth.RecaptchaVerifier;

on" ionViewDidLoad()"

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

on" your_method(phoneNumber:number)"

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

答案 1 :(得分:0)

您最有可能忘记电话号码前的“国家/地区代码”。 这就是为什么Firebase抛出错误400表示参数无效的原因

答案 2 :(得分:0)

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

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

问题是当 ReCaptcha 从我的开发环境中检测到机器人时,当我稍后尝试时它运行良好。在剩下的开发过程中,为了方便工作,我关闭了这个功能。