繁体   English   中英

Flutter:如何获取 Apple 凭据以将 Apple 帐户链接到我的 firebase 应用程序上的帐户?

[英]Flutter: how do I get Apple credential to link an apple account to an account on my firebase app?

官方 Firebase 文档说:

...
3.为新的身份验证提供商获取凭证 object:

// Google Sign-in
final credential = GoogleAuthProvider.credential(idToken: idToken);

// Email and password sign-in
final credential =
    EmailAuthProvider.credential(email: emailAddress, password: password);

// Etc.

4.将Credential object传递给登录用户的linkWithCredential()方法:

try {
  final userCredential = await FirebaseAuth.instance.currentUser
      ?.linkWithCredential(credential);
} on FirebaseAuthException catch (e) {
  // ...
}

我还没有找到任何方法来获取 Apple 的凭据(更具体地说,是AuthCredential ),这是 function 的参数所必需的:

FirebaseAuth.instance.currentUser.linkWithCredential(AuthCredential credential)

我已经按照上述方法找到了将 Google 和 Facebook 帐户链接到我的 firebase 应用程序上现有帐户的方法,只是想不出一种方法来获取 Apple 的凭据 object。 这是我尝试过的:
对于谷歌,有:

final credential = GoogleAuthProvider.credential(...);

对于 Facebook,有:

final credential = FacebookAuthProvider.credential(...);

我在任何文档中都找不到这个,我查了一下,是的,Apple 有一个类似的 function:

String accessToken = ? //how do I get this???
final credential = AppleAuthProvider.credential(accessToken);

问题是,我如何获得需要作为参数的 accessToken 字符串? 我到处搜索,找不到解决方案。


我现在看到将苹果帐户链接到我的应用程序上的现有帐户的唯一可能性是:

  • 调用我用于登录 Apple 的相同代码
  • 并警告用户他应该使用“显示我的电子邮件”而不是“隐藏我的电子邮件” ,以便将此“使用 Apple 登录”添加到现有的 firebase 帐户(重要:我链接了我的帐户中具有相同 email 的帐户firebase 身份验证设置)。
    因为如果用户改为选择“隐藏我的电子邮件”,则会使用 Apple 随机生成的 email 地址(用于匿名)创建一个新的 firebase 帐户。

我之前使用过“ the_apple_sign_in ”package。 在这个 package 中:

  • 您可以从凭证中获取访问令牌,如String.fromCharCodes(appleIdCredential.authorizationCode!)
  • 您可以从凭证中获取 id 令牌作为String.fromCharCodes(appleIdCredential..identityToken!)

也许您可以类似地获得 Firebase 凭证。 您还可以在这里找到带有此 package 的示例代码:

final FirebaseAuth _auth = FirebaseAuth.instance;

  Future<UserModel?> signInWithApple(
      {List<Scope> scopes = const [Scope.email, Scope.fullName]}) async {
    UserModel userModel;
    // 1. perform the sign-in request
    final result = await TheAppleSignIn.performRequests(
        [AppleIdRequest(requestedScopes: scopes)]);
    // 2. check the result
    switch (result.status) {
      case AuthorizationStatus.authorized:
        final appleIdCredential = result.credential;
        final oAuthProvider = OAuthProvider("apple.com");
        final credential = oAuthProvider.credential(
          idToken: String.fromCharCodes(appleIdCredential!.identityToken!),
          accessToken:
              String.fromCharCodes(appleIdCredential.authorizationCode!),
        );
        final authResult = await _auth.signInWithCredential(credential);
        final firebaseUser = authResult.user;
        firebaseUser!.updateDisplayName(
            '${appleIdCredential.fullName!.givenName} ${appleIdCredential.fullName!.familyName}');

        userModel = UserModel(
            uid: firebaseUser.uid,
            name: firebaseUser.displayName,
            email: firebaseUser.email,
            image: firebaseUser.photoURL,
            idToken:
                String.fromCharCodes(appleIdCredential.authorizationCode!));
        return userModel;
      case AuthorizationStatus.error:
        return null;
      case AuthorizationStatus.cancelled:
        return null;
    }
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM