简体   繁体   English

Flutter: Firebase 电话验证未授权,但谷歌登录成功

[英]Flutter: Firebase Phone Auth Not Authorized, but Google Sign In Successful

I try to implement various Firebase Auth method in my Flutter app, when i try to implement Firebase Phone Auth (firebase_auth), it says this error:我尝试在我的 Flutter 应用程序中实施各种 Firebase 身份验证方法,当我尝试实施 Firebase 电话身份验证(firebase_auth)时,它说这个错误:

This app is not authorized to use Firebase Authentication.此应用无权使用 Firebase 身份验证。 Please verify that the correct package name and SHA-1 are configured in the Firebase Console.请确认在 Firebase 控制台中配置了正确的 package 名称和 SHA-1。

My package name is already configured, when i setup my android app Firebase project, it connects successfully.我的 package 名称已经配置,当我设置我的 android 应用程序 Firebase 项目时,它连接成功。

Regarding the SHA-1 key, I already configured my Firebase Console to include both my debug key and my release key, i get debug key from: keytool -list -v -alias androiddebugkey -keystore %USERPROFILE%\.android\debug.keystore also i try with my release key, and build my apk in release version.关于 SHA-1 密钥,我已经配置了我的 Firebase 控制台以包含我的调试密钥和我的发布密钥,我从以下位置获取调试密钥: keytool -list -v -alias androiddebugkey -keystore %USERPROFILE%\.android\debug.keystore我也尝试使用我的发布密钥,并在发布版本中构建我的 apk。

I also re-download google-service.json and running flutter clean to ensure everything is clean.我还重新下载了 google-service.json 并运行flutter clean以确保一切都干净。

I also confirm that i run the application in real physical device, not emulator.我还确认我在真实的物理设备中运行应用程序,而不是模拟器。 But Until this point, i have no luck, still stuck (at least 2 days) in above error.但直到这一点,我没有运气,仍然卡在(至少 2 天)上述错误。

The strange thing is, when i try to login using Google Sign-In, which also (AFAIK) require correct SHA-1 information, it is works successfully.奇怪的是,当我尝试使用 Google 登录(AFAIK)也需要正确的 SHA-1 信息登录时,它可以成功运行。 But, i have no luck in Firebase Phone Auth.但是,我在 Firebase 电话验证中没有运气。

Many question and answer only address problem about running Firebase Phone in Emulator, or in unconfigured SHA-1 Firebase Console, or wrong debug/release key, or cleaning the project.许多问答仅解决有关在模拟器或未配置的 SHA-1 Firebase 控制台中运行 Firebase 电话的问题,或错误的调试/发布密钥或清理项目。 But in my case, i haven't found any answer for my problem.但就我而言,我没有找到任何问题的答案。

For reference, this is my Sign In with Phone Number dart code (which i get from firebase_auth/example github repo):作为参考,这是我的登录电话号码 dart 代码(我从 firebase_auth/example github repo 获得):

import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';

final FirebaseAuth _auth = FirebaseAuth.instance;

class SignInPage extends StatefulWidget {
  final String title = 'Registration';
  @override
  State<StatefulWidget> createState() => SignInPageState();
}

class SignInPageState extends State<SignInPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
        actions: <Widget>[
          Builder(builder: (BuildContext context) {
            return FlatButton(
              child: const Text('Sign out'),
              textColor: Theme.of(context).buttonColor,
              onPressed: () async {
                final FirebaseUser user = await _auth.currentUser();
                if (user == null) {
                  Scaffold.of(context).showSnackBar(const SnackBar(
                    content: Text('No one has signed in.'),
                  ));
                  return;
                }
                _signOut();
                final String uid = user.uid;
                Scaffold.of(context).showSnackBar(SnackBar(
                  content: Text(uid + ' has successfully signed out.'),
                ));
              },
            );
          })
        ],
      ),
      body: Builder(builder: (BuildContext context) {
        return ListView(
          scrollDirection: Axis.vertical,
          children: <Widget>[
            _PhoneSignInSection(Scaffold.of(context))
          ],
        );
      }),
    );
  }

  // Example code for sign out.
  void _signOut() async {
    await _auth.signOut();
  }
}

class _PhoneSignInSection extends StatefulWidget {
  _PhoneSignInSection(this._scaffold);

  final ScaffoldState _scaffold;
  @override
  State<StatefulWidget> createState() => _PhoneSignInSectionState();
}

class _PhoneSignInSectionState extends State<_PhoneSignInSection> {
  final TextEditingController _phoneNumberController = TextEditingController();
  final TextEditingController _smsController = TextEditingController();

  String _message = '';
  String _verificationId;

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Container(
          child: const Text('Test sign in with phone number'),
          padding: const EdgeInsets.all(16),
          alignment: Alignment.center,
        ),
        TextFormField(
          controller: _phoneNumberController,
          decoration: const InputDecoration(
              labelText: 'Phone number (+x xxx-xxx-xxxx)'),
          validator: (String value) {
            if (value.isEmpty) {
              return 'Phone number (+x xxx-xxx-xxxx)';
            }
            return null;
          },
        ),
        Container(
          padding: const EdgeInsets.symmetric(vertical: 16.0),
          alignment: Alignment.center,
          child: RaisedButton(
            onPressed: () async {
              _verifyPhoneNumber();
            },
            child: const Text('Verify phone number'),
          ),
        ),
        TextField(
          controller: _smsController,
          decoration: const InputDecoration(labelText: 'Verification code'),
        ),
        Container(
          padding: const EdgeInsets.symmetric(vertical: 16.0),
          alignment: Alignment.center,
          child: RaisedButton(
            onPressed: () async {
              _signInWithPhoneNumber();
            },
            child: const Text('Sign in with phone number'),
          ),
        ),
        Container(
          alignment: Alignment.center,
          padding: const EdgeInsets.symmetric(horizontal: 16),
          child: Text(
            _message,
            style: TextStyle(color: Colors.red),
          ),
        )
      ],
    );
  }

  // Example code of how to verify phone number
  void _verifyPhoneNumber() async {
    setState(() {
      _message = '';
    });
    final PhoneVerificationCompleted verificationCompleted =
        (AuthCredential phoneAuthCredential) {
      _auth.signInWithCredential(phoneAuthCredential);
      setState(() {
        _message = 'Received phone auth credential: $phoneAuthCredential';
      });
    };

    final PhoneVerificationFailed verificationFailed =
        (AuthException authException) {
      setState(() {
        _message =
        'Phone number verification failed. Code: ${authException.code}. Message: ${authException.message}';
      });
    };

    final PhoneCodeSent codeSent =
        (String verificationId, [int forceResendingToken]) async {
      widget._scaffold.showSnackBar(const SnackBar(
        content: Text('Please check your phone for the verification code.'),
      ));
      _verificationId = verificationId;
    };

    final PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout =
        (String verificationId) {
      _verificationId = verificationId;
    };

    await _auth.verifyPhoneNumber(
        phoneNumber: _phoneNumberController.text,
        timeout: const Duration(seconds: 60),
        verificationCompleted: verificationCompleted,
        verificationFailed: verificationFailed,
        codeSent: codeSent,
        codeAutoRetrievalTimeout: codeAutoRetrievalTimeout);
  }

  // Example code of how to sign in with phone.
  void _signInWithPhoneNumber() async {
    final AuthCredential credential = PhoneAuthProvider.getCredential(
      verificationId: _verificationId,
      smsCode: _smsController.text,
    );
    final FirebaseUser user =
        (await _auth.signInWithCredential(credential)).user;
    final FirebaseUser currentUser = await _auth.currentUser();
    assert(user.uid == currentUser.uid);
    setState(() {
      if (user != null) {
        _message = 'Successfully signed in, uid: ' + user.uid;
      } else {
        _message = 'Sign in failed';
      }
    });
  }
}

Thanks for your response, before and after.感谢您的回复,之前和之后。

Update: After trying everything for almost 2 Days, i realized that device that i use to test Firebase Phone is Rooted, and installed with Custom ROM.更新:在尝试了将近 2 天之后,我意识到我用来测试 Firebase 手机的设备是 Rooted,并安装了自定义 ROM。

When i try firebase phone auth in unrooted and Original ROM Installed, firebase phone auth works beautifully.当我在无根和安装原始 ROM 中尝试 firebase 电话身份验证时,firebase 电话身份验证工作得很好。

Looks like firebase phone auth is not available with rooted and/or custom rom installed device.看起来 firebase 电话身份验证不适用于 root 和/或自定义 rom 安装设备。

This question is answered, thanks guys:)这个问题已经回答了,谢谢大家:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM