Jose
Jose

Reputation: 351

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

Upvotes: 5

Views: 1373

Answers (2)

nantitv
nantitv

Reputation: 3733

I was also struggling with similar error for couple of days. I was trying to build a YouTube video uploader app based on the working code (update 3) from this post and then I ran into the PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException:10:,null, null.

If you are not using firebase and issue is happening on android then you have to follow step 4 of this blog those are -

  1. Enable the Google cloud api for that you are interested in and create OAuth credential as explained by the OP.

2.Go to build.gradle in android/app and find dependencies again. Then, add this line of code. You may have to update the version of the play-services-auth library.

dependencies { implementation 'com.google.android.gms:play-services-auth:20.4.1' }

  1. Create GoogleSignIn object by only passing the scope. Don't pass clientId here. GoogleSignIn _googleSignIn = GoogleSignIn( scopes: <String>[YT.YouTubeApi.youtubeReadonlyScope, YT.YouTubeApi.youtubeUploadScope], );

  2. Update pubsec.yaml for the google_sign_in library.

As mentioned, I had to only update the app/build.gradle but not the root level build.gradle for the classpath 'com.google.gms:google-services:4.3.15'as mentioned in the above medium blog. Infcat, I did not had such block in sample.

It is possible that you may run into access blocked issue later , in that case please follow this answer to add yourself as a test user

Upvotes: 0

Sachin Kumawat
Sachin Kumawat

Reputation: 437

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

Upvotes: 1

Related Questions