0

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

收到错误信息:

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

脸书登录:

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());
    }
  }
}

推特登录

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");
      }
  }
}
4

1 回答 1

0

问题解决了,

推特解决方案:

(浏览 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>

脸书解决方案:

获取客户端令牌,如下所示:

应用仪表板 -> 设置 -> 高级 -> 安全 -> 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>

并且还从下面的链接生成哈希键:

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

必须SHA1:作为输入。(由 tomeko.net 和 stackoverflow.com 提供)

于 2022-02-01T09:08:02.407 回答