Jac*_*ips 5 firebase firebase-authentication google-signin flutter
我在Flutter中有一个匿名帐户,正在尝试使用Google的AuthCredential对其进行升级。我已验证凭据是使用似乎有效的accessToken和idToken创建的。
因此,如果auth.currentUser()是我的匿名FirebaseUser,则linkAccountToGoogle()在下面进行调用会导致错误。
/// Upgrade an anonymous account by linking it to a Google account.
Future<FirebaseUser> linkAccountToGoogle() async {
final credential = await _getGoogleAuthCredential();
if (credential != null) {
try {
return auth.linkWithCredential(credential); // <=== THROWS ERROR
} catch (e) {
print(e);
}
}
return null;
}
/// Tries to sign-in silently first. May return `null`.
Future<AuthCredential> _getGoogleAuthCredential() async {
GoogleSignInAccount account;
try {
account = await _googleAuthService.signInSilently() ??
await _googleAuthService.signIn();
} catch (e) {
print(e);
}
final googleAuth = await account?.authentication;
if (account == null) {
print('Unable to retrieve Google account.');
} else if (googleAuth == null) {
print('Unable to authenticate to Google account (${account.email}).');
} else {
// print(
// 'accessToken: ${googleAuth.accessToken}, idToken: ${googleAuth.idToken}');
return GoogleAuthProvider.getCredential(
accessToken: googleAuth.accessToken, idToken: googleAuth.idToken);
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪
I/flutter ( 3038): Credential: Instance of 'AuthCredential'
E/MethodChannel#plugins.flutter.io/firebase_auth( 3038): Failed to handle method call
E/MethodChannel#plugins.flutter.io/firebase_auth( 3038): java.lang.IllegalArgumentException: Given String is empty or null
E/MethodChannel#plugins.flutter.io/firebase_auth( 3038): at com.google.android.gms.common.internal.Preconditions.checkNotEmpty(Unknown Source:5)
E/MethodChannel#plugins.flutter.io/firebase_auth( 3038): at com.google.firebase.auth.EmailAuthProvider.getCredential(Unknown Source:2)
E/MethodChannel#plugins.flutter.io/firebase_auth( 3038): at io.flutter.plugins.firebaseauth.FirebaseAuthPlugin.handleLinkWithEmailAndPassword(FirebaseAuthPlugin.java:272)
E/MethodChannel#plugins.flutter.io/firebase_auth( 3038): at io.flutter.plugins.firebaseauth.FirebaseAuthPlugin.onMethodCall(FirebaseAuthPlugin.java:122)
E/MethodChannel#plugins.flutter.io/firebase_auth( 3038): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:200)
E/MethodChannel#plugins.flutter.io/firebase_auth( 3038): at io.flutter.view.FlutterNativeView$PlatformMessageHandlerImpl.handlePlatformMessage(FlutterNativeView.java:188)
E/MethodChannel#plugins.flutter.io/firebase_auth( 3038): at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:202)
E/MethodChannel#plugins.flutter.io/firebase_auth( 3038): at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#plugins.flutter.io/firebase_auth( 3038): at android.os.MessageQueue.next(MessageQueue.java:325)
E/MethodChannel#plugins.flutter.io/firebase_auth( 3038): at android.os.Looper.loop(Looper.java:142)
E/MethodChannel#plugins.flutter.io/firebase_auth( 3038): at android.app.ActivityThread.main(ActivityThread.java:6694)
E/MethodChannel#plugins.flutter.io/firebase_auth( 3038): at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#plugins.flutter.io/firebase_auth( 3038): at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
E/MethodChannel#plugins.flutter.io/firebase_auth( 3038): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769)
E/flutter ( 3038): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: PlatformException(error, Given String is empty or null, null)
E/flutter ( 3038): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:564:7)
E/flutter ( 3038): #1 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:302:33)
E/flutter ( 3038): <asynchronous suspension>
E/flutter ( 3038): #2 FirebaseAuth.linkWithCredential (file:///home/jacob/.pub-cache/hosted/pub.dartlang.org/firebase_auth-0.8.0+1/lib/src/firebase_auth.dart:371:54)
Run Code Online (Sandbox Code Playgroud)
我尝试过的
我尝试链接的Google帐户已经是该项目的用户,因此我从Firebase控制台中删除了该Google用户。然后再次尝试使用flutter clean flutter run。
在链接要链接的帐户的身份验证凭据之前,必须先触发 Google 登录流程。根据给出的错误,很可能 Google Sign-in 的 AuthCredential 中的数据丢失了某些内容,这就是它抛出的原因Given String is empty or null
我已经尝试使用下面的代码firebase_auth: ^3.3.4,它对我来说运行良好,没有问题。
// Trigger Google Auth flow.
final GoogleSignInAccount googleUser = await GoogleSignIn().signIn();
// Obtain auth details from the request.
final GoogleSignInAuthentication googleAuth = await googleUser.authentication;
// Create a new credential.
final GoogleAuthCredential googleCredential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
// Get current anonymous user.
User anonymousUser = auth.currentUser();
// Link anonymous account with Google account.
await anonymousUser.linkWithCredential(googleCredential);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
413 次 |
| 最近记录: |