Flutter中使用Firebase手机号认证时出现内部服务器错误
Firebase Flutter手机号认证出现Internal Server Error(17499)问题排查
问题描述
我是Firebase和Flutter新手,使用Firebase手机号认证时收到Internal server error提示,包名为com.example.shubh_chintak。相关日志和代码如下:
日志信息
I/zzh (18073): ForceRecaptchaFlow from phoneAuthOptions = false, ForceRecaptchaFlow from firebaseSettings = false I/PlayCore(18073): UID: [10405] PID: [18073] IntegrityService : requestIntegrityToken(IntegrityTokenRequest{nonce=1J4FG6Jr_wQLusX84KtIkX62iH537b731LPs_mt3i2g, cloudProjectNumber=551503664846}) I/PlayCore(18073): UID: [10405] PID: [18073] IntegrityService : Initiate binding to the service. I/PlayCore(18073): UID: [10405] PID: [18073] IntegrityService : ServiceConnectionImpl.onServiceConnected(ComponentInfo{com.android.vending/com.google.android.finsky.integrityservice.IntegrityService}) I/PlayCore(18073): UID: [10405] PID: [18073] IntegrityService : linkToDeath I/PlayCore(18073): UID: [10405] PID: [18073] OnRequestIntegrityTokenCallback : onRequestIntegrityToken I/PlayCore(18073): UID: [10405] PID: [18073] IntegrityService : Unbind from service. W/System (18073): Ignoring header X-Firebase-Locale because its value was null. W/BpBinder(18073): Slow Binder: BpBinder transact took 290 ms, interface=com.google.android.gms.auth.api.phone.internal.ISmsRetrieverApiService, code=1 oneway=false E/FirebaseAuth(18073): [SmsRetrieverHelper] SMS verification code request failed: unknown status code: 17499 Internal error encountered. D/FirebaseAuth(18073): Invoking original failure callbacks after phone verification failure for +916394994875, error - An internal error has occurred. [ Internal error encountered. ]
认证代码
import 'package:firebase_auth/firebase_auth.dart'; import 'package:flutter/material.dart'; import 'package:shubh_chintak/ShubhChintak/Buyer/BuyerCreateAccount.dart'; import 'package:shubh_chintak/ShubhChintak/Buyer/BuyerMainPage/BuyerMainPage.dart'; import 'package:shubh_chintak/ShubhChintak/constants.dart'; import 'package:shubh_chintak/ShubhChintak/reusableWidget.dart'; import 'package:shubh_chintak/ShubhChintakPostAPI/ShubhChintakHttp/ReturnBuyerIdHttp.dart'; class BuyerLoginPage extends StatefulWidget { static String? buyer_id1 = _BuyerLoginPageState.buyer_id; const BuyerLoginPage({Key? key}) : super(key: key); @override State<BuyerLoginPage> createState() => _BuyerLoginPageState(); } class _BuyerLoginPageState extends State<BuyerLoginPage> { final FirebaseAuth auth = FirebaseAuth.instance; final _formKey = GlobalKey<FormState>(); String? useBuyerId; static String? buyer_id; final TextEditingController _bPhoneController = TextEditingController(); final TextEditingController _otpController = TextEditingController(); @override Widget build(BuildContext context) { var height = MediaQuery.of(context).size.height; var width = MediaQuery.of(context).size.width; var devicePixelRatio = MediaQuery.of(context).devicePixelRatio; return Scaffold( body: Form( key: _formKey, child: SizedBox( height: double.infinity, width: double.infinity, child: SingleChildScrollView( child: Column( children: <Widget>[ SizedBox( height: height * 0.25, ), reusableLocaleText( "login", devicePixelRatio * 12, TextAlign.center), SizedBox( height: height * 0.02, ), SizedBox( height: height * 0.32, width: width * 0.8, child: Card( shadowColor: kPrimaryColor, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(devicePixelRatio * 10), side: const BorderSide(color: kPrimaryColor)), child: Container( padding: EdgeInsets.fromLTRB( width * 0.08, height * 0.03, width * 0.08, height * 0.02, ), child: Column( children: <Widget>[ reusableTextFormField( context, "phone", Icons.phone_android_sharp, true, true, false, false, false, false, _bPhoneController, ), SizedBox( height: height * 0.05, ), reusableElevatedButton( context, "sendotp", 25, height * 0.055, width * 0.8, () async { if (_bPhoneController.text.isEmpty) { reusableSnackBar( context, RoundedRectangleBorder( borderRadius: BorderRadius.circular( devicePixelRatio * 10), ), SnackBarBehavior.floating, reusableLocaleText("pleaseenterphonenumber", 15, TextAlign.center), ); } else if (_bPhoneController.text.length < 10 || _bPhoneController.text .startsWith(0.toString())) { reusableSnackBar( context, RoundedRectangleBorder( borderRadius: BorderRadius.circular( devicePixelRatio * 10), ), SnackBarBehavior.floating, reusableLocaleText( "pleaseentervalidphonenumber", 15, TextAlign.center), ); } else { FocusNode currentFocus = FocusScope.of(context); if (currentFocus.hasPrimaryFocus) { currentFocus.unfocus(); } if (_formKey.currentState!.validate()) { ReturnBuyerIdHttp returnBuyerLoginId = ReturnBuyerIdHttp(); useBuyerId = await returnBuyerLoginId .returnBuyerID(_bPhoneController.text); buyer_id = returnBuyerLoginId.storeBuyerId; if (returnBuyerLoginId.storeBuyerIdStatus == "Success") { await FirebaseAuth.instance .verifyPhoneNumber( phoneNumber: '+91${_bPhoneController.text}', verificationCompleted: (PhoneAuthCredential credential) {}, verificationFailed: (FirebaseAuthException e) {}, codeSent: (String verificationId, int? resendToken) { try { sendOTPAlertDialogBox( context, "enterotpsendonyourregisteredphone", _otpController, () async { try { // Create a PhoneAuthCredential with the code PhoneAuthCredential credential = PhoneAuthProvider.credential( verificationId: verificationId, smsCode: _otpController.text); // Sign the user in (or link) with the credential await auth.signInWithCredential( credential); Navigator.push( context, MaterialPageRoute( builder: (context) => const BuyerMainPage(), ), ); } catch (exception) { reusableSnackBar( context, RoundedRectangleBorder( borderRadius: BorderRadius.circular( devicePixelRatio * 10), ), SnackBarBehavior.floating, reusableLocaleText( "pleaseentervalidotp", 15, TextAlign.center), ); } }, "ok", () {}, "resendotp", ); } on Exception catch (exception) { reusableSnackBar( context, RoundedRectangleBorder( borderRadius: BorderRadius.circular( devicePixelRatio * 10), ), SnackBarBehavior.floating, reusableLocaleText( exception.toString(), 15, TextAlign.center), ); } }, codeAutoRetrievalTimeout: (String verificationId) {}, ); } else { reusableSnackBar( context, RoundedRectangleBorder( borderRadius: BorderRadius.circular( devicePixelRatio * 10), ), SnackBarBehavior.floating, reusableLocaleText( "Invalidlogincredntials", 15, TextAlign.center), ); } } } }, ), const Spacer(), Row( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ reusableLocaleText( "don'thaveaccount?", devicePixelRatio * 5, TextAlign.center, ), reusableTextButton( context, "create", () { Navigator.pushReplacement( context, MaterialPageRoute( builder: (context) => const BuyerCreateAccount(), ), ); }, ) ], ) ], ), ), ), ), ], ), ), ), ), ); } }
解决方案
针对错误码17499的内部错误,可按以下步骤排查:
检查Firebase控制台配置:确保手机号认证已在「Authentication」->「登录方法」中启用;确认项目的SHA-1和SHA-256指纹已正确添加到项目设置的「应用」部分,调试和发布版本指纹都需要补充,且包名
com.example.shubh_chintak需与控制台配置完全一致。验证Play Integrity服务:确保设备安装了最新版Google Play服务;在Google Cloud控制台启用「Play Integrity API」,并确认API密钥拥有该服务的访问权限。
完善代码异常处理:当前
verificationFailed回调为空,无法捕获具体错误,建议补充实现:
verificationFailed: (FirebaseAuthException e) { print('认证失败:${e.message}'); reusableSnackBar( context, RoundedRectangleBorder( borderRadius: BorderRadius.circular(devicePixelRatio * 10), ), SnackBarBehavior.floating, reusableLocaleText(e.message ?? '认证失败', 15, TextAlign.center), ); },
通过该回调可获取更具体的错误信息,帮助定位问题。
检查网络与配置文件:确保设备网络稳定,能正常访问Firebase服务;重新从Firebase控制台下载
google-services.json文件,替换项目中的旧文件,保证文件内的包名、API密钥等信息与项目一致。使用真机测试:部分模拟器存在Google Play服务缺失问题,建议用真实Android设备进行测试。
内容的提问来源于stack exchange,提问作者VIKAS VERMA
相关产品推荐
相关产品推荐

