Flutter:PlatformException(sign_in_failed,com.google.android.gms.common.api.ApiException:10:,空)

时间:2019-09-21 20:00:15

标签: firebase flutter dart oauth google-signin

我有一个Flutter应用程序,并且我试图允许用户登录其Gmail,因为我希望能够列出其电子邮件。当我按下LoginButton时,系统会提示您输入Gmail密码,然后输入凭据,但随后又返回LoginButton到屏幕,并且出现错误:

Unhandled Exception: PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException: 10: , null)

我尝试过的事情:

  • google-services.json添加到/ android / app,并将GoogleService-Info.plist添加到ios / Runner。
  • 启用gmail以进行Firebase身份验证。
  • 将调试SHA1和SHA256添加到我的Firebase身份验证中。
  • 填写OAuth同意书(我在其他所有步骤后都这样做了。我不知道这是否会影响它。)

有什么想法吗?谢谢!

这是我的代码:

class AuthService {
  final GoogleSignIn _googleSignIn = GoogleSignIn();
  final FirebaseAuth _auth = FirebaseAuth.instance;
  final Firestore _db = Firestore.instance;

  Observable<FirebaseUser> user;
  Observable<Map<String, dynamic>> profile;
  PublishSubject loading = PublishSubject();

  AuthService() {
    user = Observable(_auth.onAuthStateChanged);

    profile = user.switchMap((FirebaseUser u) {
      if (u != null) {
        return _db
            .collection('users')
            .document(u.uid)
            .snapshots()
            .map((snap) => snap.data);
      } else {
        return Observable.just({});
      }
    });
  }

  Future<FirebaseUser> googleSignIn() async {
    loading.add(true);
    GoogleSignInAccount googleUser = await _googleSignIn.signIn();
    GoogleSignInAuthentication googleAuth = await googleUser.authentication;
    final AuthCredential credential = GoogleAuthProvider.getCredential(
      accessToken: googleAuth.accessToken,
      idToken: googleAuth.idToken,
    );
    FirebaseUser user = (await _auth.signInWithCredential(credential)).user;

    updateUserData(user);
    print("signed in " + user.displayName);

    loading.add(false);
    return user;
  }

  void updateUserData(FirebaseUser user) async {
    DocumentReference ref = _db.collection('user').document('user.uid');

    return ref.setData({
      'uid': user.uid,
      'email': user.email,
      'photoURL': user.photoUrl,
      'displayName': user.displayName,
      'lastSeen': DateTime.now()
    }, merge: true);
  }

  void signOut() {
    _auth.signOut();
  }
}

final AuthService authService = AuthService();
Launching lib\main.dart on Android SDK built for x86 in debug mode...
Initializing gradle...
Resolving dependencies...
Running Gradle task 'assembleDebug'...
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
Built build\app\outputs\apk\debug\app-debug.apk.
Installing build\app\outputs\apk\app.apk...
I/FA      ( 4974): Tag Manager is not found and thus will not be used
W/d.memail.memai( 4974): Accessing hidden method Landroid/view/accessibility/AccessibilityNodeInfo;->getSourceNodeId()J (light greylist, reflection)
W/d.memail.memai( 4974): Accessing hidden method Landroid/view/accessibility/AccessibilityRecord;->getSourceNodeId()J (light greylist, reflection)
W/d.memail.memai( 4974): Accessing hidden field Landroid/view/accessibility/AccessibilityNodeInfo;->mChildNodeIds:Landroid/util/LongArray; (light greylist, reflection)
W/d.memail.memai( 4974): Accessing hidden method Landroid/util/LongArray;->get(I)J (light greylist, reflection)
Syncing files to device Android SDK built for x86...
I/OpenGLRenderer( 4974): Initialized EGL, version 1.4
D/OpenGLRenderer( 4974): Swap behavior 1
D/        ( 4974): HostConnection::get() New Host Connection established 0xd831cb40, tid 5016
D/EGL_emulation( 4974): eglCreateContext: 0xec485d80: maj 2 min 0 rcv 2
D/EGL_emulation( 4974): eglMakeCurrent: 0xec485d80: ver 2 0 (tinfo 0xec483d80)
D/        ( 4974): HostConnection::get() New Host Connection established 0xd3f22780, tid 5005
D/EGL_emulation( 4974): eglCreateContext: 0xec485a80: maj 2 min 0 rcv 2
D/EGL_emulation( 4974): eglMakeCurrent: 0xec485a80: ver 2 0 (tinfo 0xec483c50)
D/EGL_emulation( 4974): eglMakeCurrent: 0xec485d80: ver 2 0 (tinfo 0xec483d80)
I/d.memail.memai( 4974): Background concurrent copying GC freed 35614(5MB) AllocSpace objects, 27(1068KB) LOS objects, 50% free, 1856KB/3MB, paused 498us total 214.123ms
I/OpenGLRenderer( 4974): Davey! duration=719ms; Flags=1, IntendedVsync=65710145267, Vsync=66060145253, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=66062704600, AnimationStart=66062759800, PerformTraversalsStart=66062789600, DrawStart=66070699600, SyncQueued=66071765200, SyncStart=66073102800, IssueDrawCommandsStart=66074268300, SwapBuffers=66323835100, FrameCompleted=66431085200, DequeueBufferDuration=26966000, QueueBufferDuration=372000, 
E/EnhancedIntentService( 4974): binding to the service failed
D/EGL_emulation( 4974): eglMakeCurrent: 0xec485a80: ver 2 0 (tinfo 0xec483c50)
W/ActivityThread( 4974): handleWindowVisibility: no activity for token android.os.BinderProxy@b021561
D/EGL_emulation( 4974): eglMakeCurrent: 0xec485d80: ver 2 0 (tinfo 0xec483d80)
D/EGL_emulation( 4974): eglMakeCurrent: 0xec485d80: ver 2 0 (tinfo 0xec483d80)
D/EGL_emulation( 4974): eglCreateContext: 0xec485b40: maj 2 min 0 rcv 2
D/EGL_emulation( 4974): eglMakeCurrent: 0xec485b40: ver 2 0 (tinfo 0xec483c50)
D/EGL_emulation( 4974): eglMakeCurrent: 0xec485b40: ver 2 0 (tinfo 0xec483c50)
D/EGL_emulation( 4974): eglMakeCurrent: 0xec485d80: ver 2 0 (tinfo 0xec483d80)
D/EGL_emulation( 4974): eglMakeCurrent: 0xec485d80: ver 2 0 (tinfo 0xec483d80)
D/EGL_emulation( 4974): eglMakeCurrent: 0xec485d80: ver 2 0 (tinfo 0xec483d80)
E/flutter ( 4974): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException: 10: , null)
E/flutter ( 4974): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:564:7)
E/flutter ( 4974): #1      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:316:33)
E/flutter ( 4974): <asynchronous suspension>
E/flutter ( 4974): #2      MethodChannel.invokeMapMethod (package:flutter/src/services/platform_channel.dart:344:48)
E/flutter ( 4974): <asynchronous suspension>
E/flutter ( 4974): #3      GoogleSignIn._callMethod (package:google_sign_in/google_sign_in.dart:218:23)
E/flutter ( 4974): <asynchronous suspension>
E/flutter ( 4974): #4      GoogleSignIn._addMethodCall (package:google_sign_in/google_sign_in.dart:257:20)
E/flutter ( 4974): #5      GoogleSignIn.signIn (package:google_sign_in/google_sign_in.dart:324:48)
E/flutter ( 4974): #6      AuthService.googleSignIn (package:memail/auth.dart:33:58)
E/flutter ( 4974): <asynchronous suspension>
E/flutter ( 4974): #7      LoginButton.build.<anonymous closure>.<anonymous closure> (package:memail/main.dart:86:46)
E/flutter ( 4974): #8      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:635:14)
E/flutter ( 4974): #9      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:711:32)
E/flutter ( 4974): #10     GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
E/flutter ( 4974): #11     TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:365:11)
E/flutter ( 4974): #12     TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:275:7)
E/flutter ( 4974): #13     PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:455:9)
E/flutter ( 4974): #14     PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:75:13)
E/flutter ( 4974): #15     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:102:11)
E/flutter ( 4974): #16     _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:218:19)
E/flutter ( 4974): #17     _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22)
E/flutter ( 4974): #18     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
E/flutter ( 4974): #19     _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
E/flutter ( 4974): #20     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
E/flutter ( 4974): #21     _rootRunUnary (dart:async/zone.dart:1136:13)
E/flutter ( 4974): #22     _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter ( 4974): #23     _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
E/flutter ( 4974): #24     _invoke1 (dart:ui/hooks.dart:250:10)
E/flutter ( 4974): #25     _dispatchPointerDataPacket (dart:ui/hooks.dart:159:5)
E/flutter ( 4974): 

2 个答案:

答案 0 :(得分:4)

我也遇到了这个问题。以我为例,我尝试在已经处于生产级别的其中一个应用中实现GoogleSignIn

当我尝试执行此操作时..我也遇到了相同的错误.. APIException: 10 ..看起来您的情况也相同。.

我尝试了很多次。.但最终,我最终妥协了,并决定不在我的应用程序中包含GoogleSignIn

但是,..我又通过在SHA1上添加2个FireBase Project键来解决了这个问题。.一个用于debug mode,另一个用于release mode .. < / p>

如果仅放置调试SHA1键,则它在release模式下不起作用,但在debug模式下工作良好,并且仅放置release SHA1键。 .it无法在debug模式下工作,但可以在release模式下工作。

不确定为什么会发生..但是它正在发生..并且以某种方式可以正常工作..

好吧..现在.. debugrelease键是什么..它们不一样..我如何获得它们..?

是这里。

转到Command Prompt中的项目。.然后cd android,然后键入。.

gradlew signingReport

它将提供SHA1键。.

debug keystore生成一个密钥,release keystore生成另一个密钥。

将它们都添加到项目中。并且它(希望如此)..就像对我一样。

希望有帮助。

注意:我使用的是Windows。不确定是否在Mac上生成密钥是否也是同一过程。

答案 1 :(得分:0)

只需连接SHA并解决问题 https://developers.google.com/android/guides/client-auth

并替换以下分析依赖项:

implementation 'com.google.firebase:firebase-core:16.0.6'