Home > other >  Flutter App Cant Authenticate to Google Drive API Using google_sign_in Package
Flutter App Cant Authenticate to Google Drive API Using google_sign_in Package

Time:12-16

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 created a Google Cloud project

  • Under the APIs & Services > Enabled APIs & Service page, I enabled Google Drive API and People API.


  • Under the APIs & Services > Credentials page, I hit Create Credentials, to create an OAuth Client ID by registering an app with the following characteristics:

  • Application Type: Android


  • Package Name: <— set to value of the package attribute in the manifest tag in <project_root>/android/app/src/main/AndroidManifest.xml


  • SHA1 certificate fingerprint: <— set to the SHA1 specified in the app signing report generated by running ./gradlew signingReport in the Android Studio terminal. NOTE: I also verified that this SHA1 is in my debug.keystore file.

  • Under APIs & Services > OAuth Consent Screen page, I verified the following:


  • My publishing status: Testing


  • User Type: External

  • I then hit Edit App.


  • In the resulting page, I hit Save and Continue to get to the Scopes page where I specify the scope that I need when using the Google Drive API.


  • I hit Save and Continue again to get to the page where I specify the email of the users that shall be allowed to the test the app.


  • Test Users: <— Set to the email of the users allowed to test the app.

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

CodePudding user response:

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 {
     
    }    
  }
  • Related