androidfluttergoogle-drive-apigoogle-workspace

Error with Google Sign-In using Client ID in Flutter Android App


I'm encountering an issue with Google Sign-In in my Flutter Android application. Specifically, I'm experiencing a PlatformException error when using a clientId for Google Sign-In. Below are the details of the problem and the relevant code:

Code:

uploadToGoogleDrive() async {
  try {
    final GoogleSignIn googleSignIn = GoogleSignIn(
        clientId: clientId, scopes: [ga.DriveApi.driveFileScope]);

    final GoogleSignInAccount? googleUser = await googleSignIn.signIn();

    if (googleUser != null) {
      final auth.AuthClient? client =
          await googleSignIn.authenticatedClient();

      final dir = await getApplicationDocumentsDirectory();

      fa.File file = fa.File("${dir.path}/db_backup.isar");
      await isar.copyToFile("${dir.path}/db_backup.isar");

      ga.File fileToUpload = ga.File();
      DateTime now = DateTime.now();

      fileToUpload.name =
          "${now.toIso8601String()}_${p.basename(file.absolute.path)}";

      final drive = ga.DriveApi(client!);

      ga.File x = await drive.files.create(fileToUpload,
          uploadMedia: ga.Media(file.openRead(), file.lengthSync()));

      if (x.id != null) {
        print(x.id!); // this refers to the id of the file created in Google Drive
        setState(() {
          fileId = x.id!; //we save it in a variable called fileId
        });
      }

      if (file.existsSync()) {
        file.deleteSync();
      }
    } else {
      if (context.mounted) {
        ScaffoldMessenger.of(context)
            .showSnackBar(const SnackBar(content: Text("Failed to sign in")));
      }
    }
  } catch (e) {
    if (context.mounted) {
      ScaffoldMessenger.of(context)
          .showSnackBar(SnackBar(content: Text(e.toString())));
    }
  }
}

Issue:

When using a clientId, I receive the following error:

ERROR: PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException: 10: , null, null)

However, the sign-in process works correctly without specifying a clientId. I'm confident that my clientId is correct, and I have configured the SHA-1 key in the Google Cloud Project. My OAuth2 consent screen is also set up as required.

Dependencies:

dependencies:
  googleapis: ^13.2.0
  googleapis_auth: ^1.6.0
  google_sign_in: ^6.2.1
  extension_google_sign_in_as_googleapis_auth: ^2.0.12

What I've Tried:

  1. Verified that the clientId is correct.
  2. Confirmed that the SHA-1 key is correctly added to the Google Cloud Project. Without SHA-1 Google Sign don't Pop up
  3. Ensured that the OAuth2 consent screen is properly configured.

Question:

What could be causing this issue when using the clientId? How can I resolve the PlatformException error and successfully use the clientId for Google Sign-In in my Flutter app?

Thank you for your help!


Solution

  • When configuring Google Sign-In for Flutter, use clientId for iOS and set it to null for Android.

      final GoogleSignIn googleSignIn = GoogleSignIn(
        clientId: Platform.isAndroid ? null : iosClientId,
      );