按照this教程,在谷歌登录后,我被重定向到Google.com,而不是带我回到我的应用程序.我怎么才能解决这个问题呢?

登录页面的示例代码:

class _LoginScreenState extends State<LoginScreen> {
  final supabase = Supabase.instance.client;
  @override
  void initState() {
    _setupAuthListener();
    super.initState();
  }

  void _setupAuthListener() {
    supabase.auth.onAuthStateChange.listen((data) {
      final event = data.event;
      if (event == AuthChangeEvent.signedIn) {
        Navigator.of(context).pushReplacement(
          MaterialPageRoute(
            builder: (context) => const ProfileScreen(),
          ),
        );
      }
    });
  }

  /// Function to generate a random 16 character string.
  String _generateRandomString() {
    final random = Random.secure();
    return base64Url.encode(List<int>.generate(16, (_) => random.nextInt(256)));
  }

  @override
  Widget build(BuildContext context) {
    return  Center(
        child: ElevatedButton(
          onPressed: () async {
            const appAuth = FlutterAppAuth();

            final rawNonce = _generateRandomString();
            final hashedNonce =
            sha256.convert(utf8.encode(rawNonce)).toString();

            final clientId =
             'MYCLIENTID';

            /// Set as reversed DNS form of Google Client ID + `:/` for Google login
            final redirectUrl = '${clientId.split('.').reversed.join('.')}:/';
            

            const discoveryUrl =
                'https://accounts.google.com/.well-known/openid-configuration';

            // authorize the user by opening the concent page
            final result = await appAuth.authorize(
              AuthorizationRequest(
                clientId,
                redirectUrl,
                discoveryUrl: discoveryUrl,
                nonce: hashedNonce,
                scopes: [
                  'openid',
                  'email',
                  'profile',
                ],
              ),
            );

            if (result == null) {
              throw 'No result';
            }

            // Request the access and id token to google
            final tokenResult = await appAuth.token(
              TokenRequest(
                clientId,
                redirectUrl,
                authorizationCode: result.authorizationCode,
                discoveryUrl: discoveryUrl,
                codeVerifier: result.codeVerifier,
                nonce: result.nonce,
                scopes: [
                  'openid',
                  'email',
                ],
              ),
            );

            final idToken = tokenResult?.idToken;

            if (idToken == null) {
              throw 'No idToken';
            }

            await supabase.auth.signInWithIdToken(
              provider: Provider.google,
              idToken: idToken,
              nonce: rawNonce,
            );
          },
          child: const Text('Google login'),
        ),
      );
  }
}

Build.gradle:

  defaultConfig {
        
        applicationId "com.user.appname"
        minSdkVersion 33
        targetSdkVersion flutter.targetSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        manifestPlaceholders += [
                'appAuthRedirectScheme': applicationId
        ]
    }

代码与上面链接的Supabase文档中的代码相同.希望找到解决这个问题的办法.如果您有任何问题,请随时留言

推荐答案

您的appAuthReDirect方案与您的Flutter 代码中提供的重定向URL不匹配.你可以像这样更新你的build.gradle文件,以匹配你Flutter 代码中的重定向URL.

defaultConfig {
    applicationId "com.user.appname"
    minSdkVersion 33
    targetSdkVersion flutter.targetSdkVersion
    versionCode flutterVersionCode.toInteger()
    versionName flutterVersionName
    manifestPlaceholders += [
        'appAuthRedirectScheme':'com.googleusercontent.apps.*account_id*'
    ]
}

Flutter相关问答推荐

创建多个具有适当填充和对齐的按钮的最佳方法

在Flutter中,CachedNetworks Image不工作(不从CachedData加载)

如何让一个名为ONTAP的回调函数、另一个名为onTapDown的回调函数和另一个名为onTapUp的回调函数不重叠?

如何使用Dio/Ffltter(前端)和amp;Go(后端)向API发送正确的请求

在Flutter 中每次点击按钮时都很难调用自定义函数

Flutter :URI的目标不存在

如何画三角形的圆角?

如何使listview.builder在筛选列表后实时更新

如何将小部件代码移动到另一个文件以获得更好的可读性

如何将 Flutter 项目迁移到具有多个插件的 Gradle 8?

TextField 中的富文本并获取 RenderParagraph

Select 文本时如何更改 Flutter 中的光标 colored颜色 ?

如何在 Flutter 中创建 Facebook 创建帖子屏幕?

Flutter - 异步文件读取异常处理

从物理设备 Flutter 中移除 USB 后启动画面不可见?

如何使用列Flutter 中文本占用的空间来控制svg宽度

如何使按钮被选中?

根据索引/变量更改小部件的 colored颜色

如何从 Flutter 中的列表中提取 JSON?

Flutter:如何将 AssetImage 转换为 Unit8List?