androidfluttergoogle-drive-apigoogle-signin

Flutter App Cant Authenticate to Google Drive API Using google_sign_in Package


My Flutter app (Android variant) is unable to authenticate to Google Drive API using the google_sign_in package (ver 5.4.2) along with the extension_google_sign_in_as_googleapis_auth package (vers 2.0.7).

The exact error that I am getting is:

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

I am NOT using Firebase, so all of the Google API config items is done via Google Cloud Console instead of Firebase Console as is usually the case with previous post having the same error as I have.

My box is Mac Air M1, and since my Flutter app is an Android variant and that there is still no Android emulator for the M1, my debugging is done via a real Android device tethered to the computer via a USB C cable.

Code snippet is as follows:

Future<http.Client> getHttpClientFromGoogle() async {
    
   String clientIdOAuth = <from client id string in Google Cloud Console (see below)>;
        
           

   List scopes = [ 'https://www.googleapis.com/auth/drive.file' ];

   GoogleSignIn signIn = GoogleSignIn(clientId: clientIdOAuth,scopes:scopes);

    signIn.onCurrentUserChanged.listen(
      (user) {
        print(user);
      },
    );

    var _account = await signIn.signIn();    <— Exception happens here.

    <more code here>
}

The problem occurs when I attempt to sign in, causing the consent form to come up. Exception is thrown after I select the email of the designated tester.

ie- enter image description here

flutter doctor output is as follows:

enter image description here

This is what I've done in Google Cloud Console:

I apologize for the long post. Its just that I've been at this for a while, trying all of the suggestions from previous posts, all to no avail. Also, I used to have no problems authenticating with Google API using the googleapis_auth package. Its just that when Google disabled loopback flow which googleapis_auth relied upon, it was recommended I switched to google_sign_in

Thanks in advance for any suggestions.

/Jose


Solution

  • useing package google_sign_in: ^5.4.2

      Future<void> gmailSignIn(BuildContext context) async {
        try {
          final GoogleSignIn googleSignIn = GoogleSignIn();
          final GoogleSignInAccount? googleSignInAccount = await googleSignIn.signIn();
          if (googleSignInAccount != null) {
            final GoogleSignInAuthentication googleSignInAuthentication =
            await googleSignInAccount.authentication;
            print("idToken = ${googleSignInAuthentication.idToken}");
            final AuthCredential authCredential = GoogleAuthProvider.credential(idToken: googleSignInAuthentication.idToken, accessToken: googleSignInAuthentication.accessToken);
            print("accessToken = ${authCredential.accessToken}");
            // Getting users credential
            UserCredential result = await auth.signInWithCredential(authCredential);
            User? user = result.user;
            print("user details = ${user.toString()}");
    
       
          }
        } on FirebaseAuthException catch (e) {
          print("Error on google sign in");
          print(e.message);
          rethrow;
        } catch (e) {
          print("Error hai");
          print(e);
          rethrow;
        } finally {
         
        }    
      }