当我尝试使用 flutter web 进行 google 登录时,出现此错误,我已将从 google 云控制台获得的客户端 ID 添加到我的 index.html 文件的元标记中,并且还在其中添加了 sha1 和 sha256 指纹我的 Firebase 控制台以及在我的 Firebase 控制台中为该项目启用 Google 身份验证
void main() async{
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: FirebaseOptions(
apiKey: "AIzaSyBZ_p9JM8Bz6X70OyPS70dUdk0QBjP6wo4",
appId: "1:848613056189:web:9d18e75518499b79aa61d4",
messagingSenderId: "848613056189",
projectId: "gpharm-2ce1f",
authDomain: "gpharm-2ce1f.firebaseapp.com",
storageBucket: "gpharm-2ce1f.appspot.com"
)
);
// if (!kIsWeb) {
// await GoogleSignInDart.register(
// clientId:
// '1078336123339-gigk2v6oam5gnev33l551p268logfd1i.apps.googleusercontent.com',
// );
// }
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: const AuthPage(),
);
}
}
class AuthService{
signInWithGoogle() async{
try{
final GoogleSignInAccount? gUser = await GoogleSignIn().signIn();
final GoogleSignInAuthentication gAuth = await gUser!.authentication;
final credential = GoogleAuthProvider.credential(
accessToken: gAuth.accessToken,
idToken: gAuth.idToken
);
return await FirebaseAuth.instance.signInWithCredential(credential);
}catch(e){
var message = e.toString();
print("this is the error: $message");
}
}
}
墨水井(
onTap: () => AuthService().signInWithGoogle(),
child: Padding(
padding: const EdgeInsets.only(left: 24, right: 24, top: 16),
child: Container(
width: screenWidth,
height: screenHeight*0.07,
decoration: BoxDecoration(
// color: Colors.red,
border: const Border(
top: BorderSide(
color: Colors.black26
),
left: BorderSide(
color: Colors.black26
),
right: BorderSide(
color: Colors.black26
),
bottom: BorderSide(
color: Colors.black26
),
),
borderRadius: BorderRadius.circular(20)
),
child: Padding(
padding: const EdgeInsets.only(left:10.0),
child: Stack(
children: [
Positioned(
top: 10,
child: SizedBox(
height: 25,
width: 25,
child: Image.asset("assets/google.png")
),
),
Center(
child: DefaultTextStyle(
style: GoogleFonts.openSans(color: Colors.black, fontSize: 14, fontWeight: FontWeight.bold),
child: const Text("Continue with Google")
),
),
],
),
),
),
),
),
我遇到了同样的问题,只有在将 Google Cloud Console 中的客户端 ID 添加到 Firebase 控制台中外部项目的安全列表客户端 ID 后,它才开始工作:
Firebase 控制台 -> 身份验证 -> 登录方法选项卡 -> Google 登录提供商 -> 展开“来自外部项目的安全列表客户端 ID(可选)”并添加 ID,然后保存。
对于那些想知道如何从 Google Cloud 控制台获取客户端 ID 的人:Google Cloud -> 搜索“凭据”并选择结果 -> 单击您的 OAuth 2.0 客户端 ID 以查看完整 ID