Search code examples
androidmongodbauthenticationmongodb-stitch

error connecting to MongoDB service cluster: server returned error on SASL authentication step: bad auth Authentication failed


I am trying to use the Stitch service by Mongodb. When I try to connect to MongoDB, it gives me this error :

Error while executing pipeline com.mongodb.stitch.android.StitchException$StitchServiceException: error connecting to MongoDB service cluster: server returned error on SASL authentication step: bad auth Authentication failed.

I have already seen this question, but here he was using login and password for authentication but I'am using FacebookAuthProvider for login. So there shouldn't be any issue of bad auth, as I can clearly see the users Facebook data.

This is the code I used for the same.

        stitchClient = new StitchClient(getApplicationContext(), "user-recognition-tgnek");
        mongoClient = new MongoClient(stitchClient, "mongodb-atlas");
        db = mongoClient.getDatabase("<DatabaseName>");

        FacebookAuthProvider fbProvider = FacebookAuthProvider.fromAccessToken(AccessToken.getCurrentAccessToken().getToken());
        stitchClient.logInWithProvider(fbProvider).continueWithTask(new Continuation<String, Task<Void>>() {
            @Override
            public Task<Void> then(@NonNull Task<String> task) throws Exception {
                final Document updateDoc = new Document(
                        "owner_id",
                        task.getResult()
                );
                updateDoc.put("Name", "Deepanshu Luhach");
                return db.getCollection("Users").updateOne(null, updateDoc, true);
            }
        }).continueWithTask(new Continuation<Void, Task<List<Document>>>() {
            @Override
            public Task<List<Document>> then(@NonNull Task<Void> task) throws Exception {
                if (!task.isSuccessful()) {
                    throw task.getException();
                }
                return db.getCollection("Users").find(
                        new Document("owner_id", stitchClient.getUserId()),
                        100
                );
            }
        }).addOnCompleteListener(new OnCompleteListener<List<Document>>() {
            @Override
            public void onComplete(@NonNull Task<List<Document>> task) {
                if (task.isSuccessful()) {
                    Log.d("STITCH", task.getResult().toString());
                    return;
                }
                Log.e("STITCH", task.getException().toString());
            }
        });

This is the complete Logcat:

12-23 23:21:30.691 7086-7119/com.example.nwagh.myapplication E/Volley: [4067] BasicNetwork.performRequest: Unexpected response code 502 for https://stitch.mongodb.com/api/client/v1.0/app/user-recognition-tgnek/pipeline 12-23 23:21:30.705 7086-7086/com.example.nwagh.myapplication E/Stitch: Error while executing pipeline com.mongodb.stitch.android.StitchException$StitchServiceException: error connecting to MongoDB service cluster: server returned error on SASL authentication step: bad auth Authentication failed. at com.mongodb.stitch.android.StitchError.a(SourceFile:53) at com.mongodb.stitch.android.StitchClient$10.onErrorResponse(SourceFile:754) at com.android.volley.Request.deliverError(Request.java:598) at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:101) at android.os.Handler.handleCallback(Handler.java:790) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6515) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

Please help me with this issue, I can't find anything related to this.


Solution

  • There was some error with connection to Atlas cluster. Just try to relink the app with the cluster and everything will work perfectly.

    To relink your app go to MongoDB Atlas -> Stitch Apps -> In the Actions you will see the option to unlink the app, then you can relink it again.

    Credits: @edaniels