Reputation: 351
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.
flutter doctor output is as follows:
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
Upvotes: 5
Views: 1373
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 -
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' }
Create GoogleSignIn object by only passing the scope. Don't pass clientId here.
GoogleSignIn _googleSignIn = GoogleSignIn( scopes: <String>[YT.YouTubeApi.youtubeReadonlyScope, YT.YouTubeApi.youtubeUploadScope], );
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
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