gpt4 book ai didi

android - Flutter:Firebase Phone Auth 未授权,但 Google 登录成功

转载 作者:行者123 更新时间:2023-11-30 04:58:04 24 4
gpt4 key购买 nike

我尝试在我的 Flutter 应用程序中实现各种 Firebase Auth 方法,当我尝试实现 Firebase Phone Auth (firebase_auth) 时,它显示此错误:

This app is not authorized to use Firebase Authentication. Please verify that the correct package name and SHA-1 are configured in the Firebase Console.

我的包名已经配置好,当我设置我的 Android 应用程序 Firebase 项目时,它连接成功。

关于 SHA-1 key ,我已经将我的 Firebase 控制台配置为包括我的调试 key 和我的发布 key ,我从以下位置获取调试 key :keytool -list -v -alias androiddebugkey -keystore %USERPROFILE%\.android\debug.keystore 我也尝试使用我的发布 key ,并在发布版本中构建我的 apk。

我还重新下载了 google-service.json 并运行 flutter clean 以确保一切都是干净的。

我还确认我在真实的物理设备上运行应用程序,而不是模拟器。但直到此时,我没有运气,仍然陷入(至少 2 天)上述错误。

奇怪的是,当我尝试使用 Google 登录(据我所知)需要正确的 SHA-1 信息登录时,它成功运行了。但是,我在 Firebase Phone Auth 中没有运气。

许多问答仅解决有关在模拟器中或在未配置的 SHA-1 Firebase 控制台中运行 Firebase 电话的问题,或错误的调试/发布 key ,或清理项目。但就我而言,我还没有找到解决问题的方法。

作为引用,这是我使用电话号码登录 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';
}
});
}
}

感谢您在之前和之后的回复。

最佳答案

更新:在尝试了将近 2 天的一切之后,我意识到我用来测试 Firebase Phone 的设备已 Root,并安装了自定义 ROM。

当我在未安装的原始 ROM 中尝试使用 firebase phone auth 时,firebase phone auth 工作得很好。

看起来 firebase 手机身份验证不适用于已安装 root 和/或自定义 rom 的设备。

这个问题已经回答了,谢谢大家:)

关于android - Flutter:Firebase Phone Auth 未授权,但 Google 登录成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58787038/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com