Flutter Google登录未在Firebase中注册 [英] Flutter Google Login not Getting Registered in Firebase

查看:125
本文介绍了Flutter Google登录未在Firebase中注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Firebase和Flutter实现简单的Facebook和Google登录按钮,但是Google版本表现得很奇怪.当我打印 FirebaseAuth.instance.currentUser()时,我会收到错误消息或以前的Facebook登录数据.

I'm trying to implement simple Facebook and Google Sign-in buttons with Firebase and Flutter, however the Google version is acting very weird. When I print the FirebaseAuth.instance.currentUser() I either get errors or prior Facebook login data.

当我使用Facebook登录时,我的帐户确实存在出现在Firebase上,但是Google登录似乎没有任何作用.

When I login with Facebook, my account does appear on Firebase, however, the Google login seems to do nothing.

这是我在下面的代码中弄乱了吗,还是AndroidX与这些库的某些部分不兼容?还有吗?

Is this something I messed up in the code below or some incompatibility problem with AndroidX with parts of these libraries? Something else?

此外,对我来说还不是很清楚,我是否必须将 Project面向公众的名称放入我的项目中的某个位置,以使与Firebase的集成工作正常(我必须做一些类似于设置的工作)Facebook登录按钮).

Also, it is not very clear to me if I have to put the Project public-facing name somewhere inside my project to make the integration with Firebase work (I had to do something similar to setup the Facebook Login button).

我必须用 signInWithCredential 替换 logInWithReadPermissions ,因为最近的版本已更改了它们的API.我也尝试过使用软件包的早期版本,但是遇到了很多错误(可能是由于AndroidX):

I had to replace logInWithReadPermissions with signInWithCredential because recent versions have changed their API. I've also tried to use previous versions of the packages, but encountered many errors (probably due to AndroidX):

final _auth = FirebaseAuth.instance;

Future<FirebaseUser> _loginWithFacebook () async {
  final facebookLogin = FacebookLogin();
  final result = await facebookLogin.logInWithReadPermissions(['email']);

  if (result.status == FacebookLoginStatus.loggedIn){
    final FacebookAccessToken accessToken = result.accessToken;
    AuthCredential credential = FacebookAuthProvider.getCredential(
      accessToken: accessToken.token,
    );

    AuthResult signInResult = await _auth.signInWithCredential(credential);

    FirebaseUser fbUser = signInResult.user;
    return fbUser;
  }
  else{
    return null;
  }
}

3.Google登录

同样,

signInWithCredential 是最近的API:

3. The Google Login

Again, signInWithCredential seems to be the more recent API:

Future<FirebaseUser> _loginWithGoogle () async{
  final GoogleSignIn _googleSignIn = GoogleSignIn();

  GoogleSignInAccount googleUser = await _googleSignIn.signIn();

  final GoogleSignInAuthentication googleAuth = await googleUser.authentication;
  final AuthCredential credential = GoogleAuthProvider.getCredential(
    accessToken: googleAuth.accessToken,
    idToken: googleAuth.idToken,
  );
  AuthResult signInResult = await _auth.signInWithCredential(credential);

  final FirebaseUser user = signInResult.user;

  print(user);

  return user;
}

编辑

我已经在Android 9.0(Pie)模拟器上进行了尝试,但仍然无法正常工作.

Edit

I've tried it on an Android 9.0 (Pie) emulator and it still doesn't work.

推荐答案

很明显,考虑到最新的API,我的身份验证代码对于Google和Facebook都是正确的.

Apparently, my authentication code was correct for both Google and Facebook, considering the most up-to-date API.

但是我需要将 SHA1指纹集成到我的Firebase环境中,这是我不知道需要做的事情,因为我遵循的所有教程都没有提到它,并且可以很容易地将其删除在Firebase中启用Google登录方法时.

However I needed to integrate a SHA1 Fingerprint to my Firebase environment, which is something I didn't realize I needed to do because none of the tutorials I followed mentioned it and it can be easily dismissed when enabling the Google Sign-in method inside Firebase.

无论如何,这是获取 SHA1指纹所需的步骤:

Anyway, here are the steps you will need to complete to get the SHA1 Fingerprint:

  1. 打开命令提示符并 cd C:\ Program Files \ Android \ Android Studio \ jre \ bin .
  2. 获取 SHA1 密钥:
keytool -list -v -alias androiddebugkey -keystore %USERPROFILE%\.android\debug.keystore

  • 转到 Firebase控制台>项目设置>添加密钥并添加密钥.
  • Go to Firebase Console > Project settings > Add Key and add the key.
  • 这篇关于Flutter Google登录未在Firebase中注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆