flutter如何实现google登录

在Flutter项目中集成Google登录时遇到问题,按照官方文档配置了Firebase和Google Cloud Platform,但点击登录按钮后始终无法获取用户信息。具体表现为:Android端返回PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException: 10: , null)错误,iOS端则直接无反应。已确认SHA1证书指纹和iOS Bundle ID配置正确,也添加了必要的OAuth客户端ID。请问该如何排查这个问题?是否需要额外配置Flutter端的google_sign_in插件参数?


更多关于flutter如何实现google登录的实战教程也可以访问 https://www.itying.com/category-92-b0.html

2 回复

Flutter中实现Google登录,需使用google_sign_in插件。步骤如下:

  1. 在Google Cloud Console配置OAuth 2.0客户端ID。
  2. 添加google_sign_in依赖到pubspec.yaml
  3. 调用GoogleSignIn().signIn()进行登录。
  4. 处理返回的GoogleSignInAuthentication对象获取访问令牌。

示例代码:

final GoogleSignInAccount? user = await GoogleSignIn().signIn();
final GoogleSignInAuthentication auth = await user!.authentication;
String? accessToken = auth.accessToken;

更多关于flutter如何实现google登录的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


在Flutter中实现Google登录,可以通过以下步骤完成:

1. 添加依赖

pubspec.yaml 中添加依赖:

dependencies:
  google_sign_in: ^6.1.1
  firebase_auth: ^4.17.3

2. Firebase配置

  • Firebase Console 创建项目
  • 添加Android/iOS应用并下载配置文件:
    • Android: google-services.json 放到 android/app/ 目录
    • iOS: GoogleService-Info.plist 放到 ios/Runner 目录

3. 配置平台设置

Android

  • android/app/src/main/AndroidManifest.xml 添加:
<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />

iOS

  • ios/Runner/Info.plist 添加 URL Scheme:
<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>com.googleusercontent.apps.YOUR_CLIENT_ID</string>
        </array>
    </dict>
</array>

4. 实现登录代码

import 'package:google_sign_in/google_sign_in.dart';
import 'package:firebase_auth/firebase_auth.dart';

class GoogleSignInService {
  final GoogleSignIn _googleSignIn = GoogleSignIn();
  final FirebaseAuth _auth = FirebaseAuth.instance;

  Future<User?> signInWithGoogle() async {
    try {
      final GoogleSignInAccount? googleUser = await _googleSignIn.signIn();
      if (googleUser == null) return null;

      final GoogleSignInAuthentication googleAuth = 
          await googleUser.authentication;

      final credential = GoogleAuthProvider.credential(
        accessToken: googleAuth.accessToken,
        idToken: googleAuth.idToken,
      );

      final UserCredential authResult = 
          await _auth.signInWithCredential(credential);
      return authResult.user;
    } catch (error) {
      print("Google登录失败: $error");
      return null;
    }
  }

  Future<void> signOut() async {
    await _googleSignIn.signOut();
    await _auth.signOut();
  }
}

5. 使用示例

FloatingActionButton(
  onPressed: () async {
    User? user = await GoogleSignInService().signInWithGoogle();
    if (user != null) {
      print("登录成功: ${user.displayName}");
    }
  },
  child: Text('Google登录'),
)

注意事项:

  1. 确保SHA-1证书指纹在Firebase中正确配置
  2. iOS需要在开发者后台配置Bundle ID
  3. 需要在Google Cloud Console启用Google+ API

这样就完成了Flutter应用的Google登录集成。

回到顶部