Google Auth sometimes doesn't work- base64 issues?

I followed the Docs of Google Sign-in (here: https://www.mongodb.com/docs/realm/sdk/java/examples/authenticate-users/#google-user)
It seems fine, but sometimes (1 of 4 on average), loginAsync fails.
Here is my code:

private void signInWithGoogle() {
    GoogleSignInOptions gso = new GoogleSignInOptions
            .Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken("MY-KEY.apps.googleusercontent.com")
            .build();
    GoogleSignInClient googleSignInClient = GoogleSignIn.getClient(this, gso);
    Intent signInIntent = googleSignInClient.getSignInIntent();
    ActivityResultLauncher<Intent> resultLauncher =
            registerForActivityResult(
                    new ActivityResultContracts.StartActivityForResult(),
            new ActivityResultCallback<ActivityResult>() {
                @Override
                public void onActivityResult(ActivityResult result) {
                    Task<GoogleSignInAccount> task =
                            GoogleSignIn.getSignedInAccountFromIntent(result.getData());
                    handleSignInResult(task);
                }
            });
    resultLauncher.launch(signInIntent);
}

private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
    try {
        if (completedTask.isSuccessful()) {
            GoogleSignInAccount account = completedTask.getResult(ApiException.class);
            String token = account.getIdToken();
            Credentials googleCredentials =
                    Credentials.google(token, GoogleAuthType.ID_TOKEN);
            app.loginAsync(googleCredentials, it -> {
                if (it.isSuccess()) {
                    Log.v("AUTH",
                            "Successfully logged in to MongoDB Realm using Google OAuth.");
                } else {
                    Log.e("AUTH",
                            "Failed to log in to MongoDB Realm: ", it.getError());
                }
            });
        } else {
            Log.e("AUTH", "Google Auth failed: "
                    + completedTask.getException().toString());
        }
    } catch (ApiException e) {
        Log.w("AUTH", "Failed to log in with Google OAuth: " + e.getMessage());
    }
}

After choosing Google account to login with,
Some of the errors I receive:

Failed to log in to MongoDB Realm: 
    AUTH_ERROR(realm::app::ServiceError:47): illegal base64 data at input byte 345
        at io.realm.internal.jni.OsJNIResultCallback.onError(OsJNIResultCallback.java:60)
        at io.realm.internal.objectstore.OsApp.nativeLogin(Native Method)
        at io.realm.internal.objectstore.OsApp.login(OsApp.java:99)
        at io.realm.mongodb.App.login(App.java:360)
        at io.realm.mongodb.App$3.run(App.java:411)
        at io.realm.mongodb.App$3.run(App.java:408)
        at io.realm.internal.mongodb.Request$1.run(Request.java:57)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:923)

or:

 E/REALM_JNI: jni: ThrowingException 6, [json.exception.type_error.316] invalid UTF-8 byte at index 1012: 0xFF in /tmp/realm-java/realm/realm-library/src/main/cpp/io_realm_internal_objectstore_OsApp.cpp line 195, .
E/REALM_JNI: Exception has been thrown: Unrecoverable error. [json.exception.type_error.316] invalid UTF-8 byte at index 1012: 0xFF in /tmp/realm-java/realm/realm-library/src/main/cpp/io_realm_internal_objectstore_OsApp.cpp line 195

Failed to log in to MongoDB Realm: 
    UNKNOWN(unknown:-1): Unexpected error
    io.realm.exceptions.RealmError: Unrecoverable error. [json.exception.type_error.316] invalid UTF-8 byte at index 1012: 0xFF in /tmp/realm-java/realm/realm-library/src/main/cpp/io_realm_internal_objectstore_OsApp.cpp line 195
        at io.realm.internal.objectstore.OsApp.nativeLogin(Native Method)
        at io.realm.internal.objectstore.OsApp.login(OsApp.java:99)
        at io.realm.mongodb.App.login(App.java:360)
        at io.realm.mongodb.App$3.run(App.java:411)
        at io.realm.mongodb.App$3.run(App.java:408)
        at io.realm.internal.mongodb.Request$1.run(Request.java:57)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:923)
    
        at io.realm.internal.mongodb.Request$1.run(Request.java:61)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:923)
1 Like

Same here … did you manage to fix it ?

Hello @Yarin_Belker ,

I am having the same sporadical problem.
Did you manage to find a solution?

Best regards,
Andrei