使用 flutter 和 firebase 登录 Twitter 无法 100% 工作

问题描述 投票:0回答:2

我想在我的应用程序中实现 Twitter 登录...到目前为止一切顺利,我可以运行它,但现在我有点卡住了,我不知道发生了什么...我写了一个函数(见下文),可以让你使用 apikey 等登录,但是当我单击按钮时,我被重定向到 twitter API,但 firebase 上没有新用户...我不知道为什么会发生这种情况...请参阅下面的代码: (对于 API 密钥中的“示例”,我插入了 API 密钥,我只是不想在这里发布它们......)

此外,当我在 iPhone 13 模拟器上关闭 Google 登录对话框时,尽管我使用了 try-catch 块,但我收到了错误(PlatformException)...在我的物理 Android 设备上它工作正常...我不知道为什么会发生这种情况。 ..

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:twitter_login/twitter_login.dart';
import 'package:wineapp/constants.dart';

class AuthService {
  FirebaseAuth firebaseAuth = FirebaseAuth.instance;

  //Register User

  Future<User?> emailRegister(
      String email, String password, BuildContext context) async {
    try {
      UserCredential userCredential =
          await firebaseAuth.createUserWithEmailAndPassword(
        email: email,
        password: password,
      );
      return userCredential.user;
    } on FirebaseAuthException catch (e) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
          content: Text(
            e.message.toString(),
            style: GoogleFonts.poppins(
              textStyle: const TextStyle(
                color: mainTextColor,
                fontSize: 12,
                fontWeight: FontWeight.w600,
              ),
            ),
          ),
          backgroundColor: primaryColor,
        ),
      );
    } catch (e) {
      print(e);
    }
  }

  //User login
  Future<User?> emailLogin(
      String email, String password, BuildContext context) async {
    try {
      UserCredential userCredential =
          await firebaseAuth.signInWithEmailAndPassword(
        email: email,
        password: password,
      );
      return userCredential.user;
    } on FirebaseAuthException catch (e) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
          content: Text(
            e.message.toString(),
            style: GoogleFonts.poppins(
              textStyle: const TextStyle(
                color: mainTextColor,
                fontSize: 12,
                fontWeight: FontWeight.w600,
              ),
            ),
          ),
          backgroundColor: primaryColor,
        ),
      );
    }
  }

  //User SignIn with Google
  Future<User?> signInWithGoogle() async {
    try {
      //Triger the authentication flow
      final GoogleSignInAccount? googleUser = await GoogleSignIn().signIn();
      if (googleUser != null) {
        //Obtain the auth details from the request
        final GoogleSignInAuthentication googleAuth =
            await googleUser.authentication;
        //Create a new credential
        final credential = GoogleAuthProvider.credential(
          accessToken: googleAuth.accessToken,
          idToken: googleAuth.idToken,
        );
        //Once signed in, return the UserCredential
        UserCredential userCredential =
            await firebaseAuth.signInWithCredential(credential);
        return userCredential.user;
      }
    } on FirebaseAuthException catch (e) {
      print(
        e.toString(),
      );
    }
    return null;
  }

  //Sign Out function
  Future googleSignOut() async {
    await GoogleSignIn().signOut();
    await firebaseAuth.signOut();
  }

      void twitterLogin() async {
    // Create a TwitterLogin instance
    final twitterLogin = TwitterLogin(
        apiKey: 'example',
        apiSecretKey: 'example',
        redirectURI: 'flutter-twitter-login://');

    // Trigger the sign-in flow
    await twitterLogin.login().then((value) async {
      if (value.authToken != null || value.authTokenSecret != null) {
        final twitterAuthCredential = TwitterAuthProvider.credential(
          accessToken: value.authToken,
          secret: value.authTokenSecret,
        );

        await FirebaseAuth.instance.signInWithCredential(twitterAuthCredential);
      }
    });
  }
}

提前感谢您的帮助:)

flutter dart twitter firebase-authentication
2个回答
0
投票

推特

文档中非常清楚如何实现twitter_login。您必须在 Android 清单 xml 文件中使用

android:scheme
添加意图,但不包括
redirectURI
。您还可以使用
://
将其添加到您的 twitter 应用回调 uri。最佳实践是输入您用于创建此项目的应用程序名称。例如,如果您使用
://
,那么您的重定向或回调 uri 将为
flutter create fruitsapp
,但在
fruitsapp://
中,请确保将以下
androidmanifest.xml
放入
intent-filter
所在的
application
标签内。在
android:label
标签内的下面放置以下内容:
activity

请记住,firebaseAuth 不会返回使用 twitter 登录的用户的电子邮件地址,因此请勿使用任何 
<intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="fruitsapp" /> </intent-filter>

运算符进行空检查,因为它始终会返回

!
值。

谷歌

如果用户在

null

块捕获

try-catch
内取消登录异常,则捕获正确的 google 登录,如下所示:
PlatformException



0
投票
twitter_auth_firebase

非常容易集成并且工作正常。 try{ //Google signin } catch (e){ if (GoogleSignIn.kSignInCanceledError.isNotEmpty()) { //Show snackbar or something } }

注释

此插件仅与通过 Firebase 登录 Twitter 兼容。

Flutter 版本:>=3.3.0 和 Dart SDK 版本:>=3.1.5

Twitter 身份验证需要 Firebase 设置。<4.0.0.

在 Firebase 登录部分启用 Twitter 身份验证。

© www.soinside.com 2019 - 2024. All rights reserved.