如何在 Flutter 中设置 firebase 身份验证(分别使用 ```twitter_login: ^4.0.1``` 和 ```flutter_facebook_auth: ^4.0.1```)?

How to set up firebase Authentication (with ```twitter_login: ^4.0.1``` and ```flutter_facebook_auth: ^4.0.1``` respectively) in Flutter?

Flutter 中的 Firebase 身份验证(分别使用 twitter_login: ^4.0.1flutter_facebook_auth: ^4.0.1)。我在两个身份验证过程中都遇到了错误。我还在开发者帐户的 Facebook LoginTwitter Login 中进行了设置。还浏览了许多在线文章,但似乎 none 有效。最近更新后。

收到错误消息:

E/com.facebook.GraphResponse(13052): {HttpStatus: 400, errorCode: 100, subErrorCode: 33, errorType: GraphMethodException, errorMessage: Unsupported get request. Object with ID 'XXXXXXXXXXXX' does not exist, cannot be loaded due to missing permissions, or does not support this operation. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api}

以上消息来自 Facebook,以下来自 Twitter

E/flutter (13052): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: PlatformException(400, Failed to generate request token., Please check your APIKey or APISecret., null)

AndroidManifest.xml文件.

<meta-data android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/facebook_app_id"/>

         <activity android:name="com.facebook.FacebookActivity"
            android:configChanges=
               "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:label="@string/app_name" />
         <activity
            android:name="com.facebook.CustomTabActivity"
            android:exported="true">
             <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="@string/fb_login_protocol_scheme" />
             </intent-filter>
             <intent-filter>
                 <action android:name="android.intent.action.VIEW" />
                 <category android:name="android.intent.category.DEFAULT" />
                 <category android:name="android.intent.category.BROWSABLE" />
                 <action android:name="android.intent.action.MAIN"/>
                 <category android:name="android.intent.category.LAUNCHER"/>
                 <data android:scheme="twitter-firebase-auth"/>
             </intent-filter>
         </activity>

pubspec.yaml 包,

flutter_facebook_auth: 4.0.0
twitter_login: 4.0.1
firebase_auth: 3.3.5
firebase_core: 1.11.0

Facebook 登录:

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_facebook_auth/flutter_facebook_auth.dart';


var title = "";
var displayName = "";
FirebaseAuth auth = FirebaseAuth.instance;

signInWithFacebook() async {
  try {
    final LoginResult result = await FacebookAuth.instance.login();
    switch (result.status) {
      case LoginStatus.success:
        final AuthCredential credential =
        FacebookAuthProvider.credential(result.accessToken!.token);
        if (kDebugMode) {
          print(result.accessToken!.token);
        }
        final userCredential = await auth.signInWithCredential(credential);
        if (kDebugMode) {
          print(credential.signInMethod);
        }
        // TODO: Store user.credential!.signInMethod in SharedPref.
        if (kDebugMode) {
          print(userCredential.user!.displayName);
        }
        // boolean isLoggedIn = accessToken != null && !accessToken.isExpired();
        if (kDebugMode) {
          print("status: Status.Success");
        }
        break;
      case LoginStatus.cancelled:
        if (kDebugMode) {
          print("status: Status.Cancelled");
        }
        break;
      case LoginStatus.failed:
        if (kDebugMode) {
          print("status: Status.Failed");
        }
        break;
      default:
        if (kDebugMode) {
          print("null");
        }
        break;
    }
  } catch (e) {
    if (kDebugMode) {
      print('Error occurred!' + e.toString());
    }
  }
}

Twitter 登录

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/foundation.dart';
import 'package:twitter_login/twitter_login.dart';

FirebaseAuth _auth = FirebaseAuth.instance;

signInWithTwitter() async {
  final twitterLogin = TwitterLogin(
    apiKey: "XXXXXXXXXXXXXX",
    apiSecretKey: "XXXXXXXXXXXXXXXXXXXX",
    redirectURI: "twitter-firebase-auth://",
  );
  final authResult = await twitterLogin.login();

  switch (authResult.status) {
    case TwitterLoginStatus.loggedIn:
      if (kDebugMode) {
        print("status: LogIn Success");
      }
      final AuthCredential twitterAuthCredential =
          TwitterAuthProvider.credential(
              accessToken: authResult.authToken!,
              secret: authResult.authTokenSecret!);

      final userCredential =
          await _auth.signInWithCredential(twitterAuthCredential);
      if (kDebugMode) {
        print("status: SignIn With Credential Success");
      }
      break;
    case TwitterLoginStatus.cancelledByUser:
      if (kDebugMode) {
        print("status: Cancelled By User");
      }
      break;
    case TwitterLoginStatus.error:
      if (kDebugMode) {
        print("status: Error");
      }
      break;
    default:
      if (kDebugMode) {
        print("status: null");
      }
  }
}

问题已解决,

Twitter 解决方案:

(已阅读 twitter_login:^4.0.1 文档)。

<data android:scheme="flutter-twitter-auth"/>

将上面的替换为,

<data android:scheme="your_app_name"/>

也替换下面的片段

final twitterLogin = TwitterLogin(
      apiKey: "xxxxxxxxxx",
      apiSecretKey: "xxxxxxxxx",
      redirectURI: 'flutter-twitter-auth://',
    );

final twitterLogin = TwitterLogin(
      apiKey: "xxxxxxxxxx",
      apiSecretKey: "xxxxxxxxx",
      redirectURI: 'your_app_name://',
    );

最后在 Twitter 开发者帐户中的回调 URL 应该是,

your_app_name://

已更改 XML 文件:

<meta-data android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/facebook_app_id"/>

         <activity android:name="com.facebook.FacebookActivity"
            android:configChanges=
               "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:label="@string/app_name" />
         <activity
            android:name="com.facebook.CustomTabActivity"
            android:exported="true">
             <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="@string/fb_login_protocol_scheme" />
             </intent-filter>
             <intent-filter>
                 <action android:name="android.intent.action.VIEW" />
                 <category android:name="android.intent.category.DEFAULT" />
                 <category android:name="android.intent.category.BROWSABLE" />
                 <action android:name="android.intent.action.MAIN"/>
                 <category android:name="android.intent.category.LAUNCHER"/>
                 <data android:scheme="twitter-firebase-auth"/>
             </intent-filter>
         </activity>

如下图:

             <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
            <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="buildyourenglish"/>
            </intent-filter>
         <meta-data android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/facebook_app_id"/>

         <activity android:name="com.facebook.FacebookActivity"
            android:configChanges=
               "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:label="@string/app_name" />
         <activity
            android:name="com.facebook.CustomTabActivity"
            android:exported="true">
             <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="@string/fb_login_protocol_scheme" />
             </intent-filter>
             
         </activity>

Facebook 解决方案:

获取客户​​端令牌如下图:

应用程序面板 -> 设置 -> 高级 -> 安全 -> Client_Token(您的 facebook 客户端令牌)

转到 Project_Folder -> android -> app -> src -> main -> res -> values -> value.xml (如果不存在则创建)并粘贴下面的代码。

同样,转到 Project_Folder -> android -> app -> src -> main -> res -> values -> value.xml (如果不存在则创建)和粘贴以下代码。

<?xml version="1.0" encoding="utf-8"?>

<resources>
    <string name="app_name">your_app_name</string>
    <string name="facebook_app_id">your_facebook_app_id</string>
    <string name="fb_login_protocol_scheme">your_fb_login_protocol_scheme (fb+your_facebook_app_id)</string>
    <string name="facebook_client_token">your_facebook_client_token</string>
</resources>

并且还从下面生成哈希键 link:

http://tomeko.net/online_tools/hex_to_base64.php 

必须输入 SHA1:。 (礼貌 tomeko.net 和 whosebug.com)