Firebase+Auth0+Android : Authentication error when trying to request credentials with delegation API

I have implemented passworldless authentication (mobile no + OTP) in android app. I am able to successfully authenticate the user.
Now trying to include firebase as a backend. For this AuthenticationAPIClient is requesting credentials through delegation API

    String apiType = "firebase";
            final String tokenId = payload.getIdToken();
            apiClient.delegationWithIdToken(tokenId, apiType)
                    .start(new BaseCallback<Map<String, Object>, AuthenticationException>() {
                        @Override
                        public void onSuccess(Map<String, Object> payload) {

                            mAuth.signInWithCustomToken(payload.get("idToken").toString()).
                                    addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
                                        @Override
                                        public void onComplete(@NonNull Task<AuthResult> task) {
                                            if (task.isSuccessful()) {
                                                Log.d(TAG, "signnwithCustom:success");
                                                FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
                                                launchHomeOrWelcomePage();

                                            } else {
                                                //if sign in fails
                                                Log.w(TAG, "sign in with custom token failed");

                                            }

                                        }
                                    }).addOnFailureListener(new OnFailureListener() {
                                @Override
                                public void onFailure(@NonNull Exception e) {
                                    if (e instanceof FirebaseAuthException) {
                                        Log.w(TAG, "" + ((FirebaseAuthException) e).getErrorCode());
                                    }
                                }
                            });

                        }

                        @Override
                        public void onFailure(AuthenticationException error) {
                            Log.e(TAG, "authentication exception  error" +
                                    error.getMessage(), error);

                        }
                    });
        }
    });
}

};

But the problem is following error is popping out in onFailure(AuthenticationException error)` function

authentication exception  errorAn error occurred when trying to authenticate with the server.
  com.auth0.android.authentication.AuthenticationException: An error occurred when trying to authenticate with the server.
      at com.auth0.android.request.internal.AuthenticationErrorBuilder.from(AuthenticationErrorBuilder.java:28)
      at com.auth0.android.request.internal.AuthenticationErrorBuilder.from(AuthenticationErrorBuilder.java:9)
      at com.auth0.android.request.internal.BaseRequest.parseUnsuccessfulResponse(BaseRequest.java:135)
      at com.auth0.android.request.internal.SimpleRequest.onResponse(SimpleRequest.java:65)
      at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:177)
      at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
      at java.lang.Thread.run(Thread.java:818)

I have enabled firebase as a add on in project and made all the necessary changes under settings tab
of firebase add-on API. Any help is appreciated in resolving this issue.

https://auth0.com/docs/libraries/lock-android/delegation-api

@abhishek.hingnikar Can u please help me in solving this issue?

You need to add a delegation rule in your Auth0 dashboard.

function (user, context, callback) {
    var isFirebase = context.isDelegation && context.request.body.api_type === "firebase";
    if (isFirebase) {
      var identity = user.identities[0];
      user.firebase_data = {
        user_id: new Buffer(user.email).toString('base64'),
        //company: !user.isSocial ? context.connection.replace(/\./g, '-') : null,
        foo: 'bar'
      };
    }
    return callback(null, user, context);
  }

@pat.trivedy Thanks for answer. After adding rules in Update user profile identity in Firebase under rules tab. I am getting following error:

onSuccess received in callback
04-27 16:10:46.973 31889-31889/in.eventloop.heylo D/LoginActivity: Access token : 8bn5MTE7xEio0pWM
                                                                    IdToken :eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Ik5VWTVSRFl5UkVOQlJEVkNOVU13UWpjME4wWkdOa1F3TjBVNVFqRTFOamM0UkRFMU1EZzNSZyJ9.eyJpc3MiOiJodHRwczovL2Vyem9ocmFraGFuLmF1dGgwLmNvbS8iLCJzdWIiOiJzbXN8NTg2OTYyMzM5ODQyMGY5YTExYWNmNDMwIiwiYXVkIjoiZ2w0NkxrMFB3MExRV04zVkRKZDlsUGpiNTBQZ0E5STAiLCJleHAiOjE0OTMzMjU2NDcsImlhdCI6MTQ5MzI4OTY0N30.NDYr01vrBrYt8jcwNiGtB9RsGGM31N_UfI4VL-HIRSDm8X4ADDQdRp3Cy9-0VXJ860CBkB_A-kv_5_ji0A3ct0DoLex03DGCItqDLXaISy6GwVMCo-DANhOCveoxFf7r5k0SFNysAZvMR-FY5eB1fnVecf7xG5bN96fy1qZGiaDxAoVLkD-iRWHp7IShPvPov6tzFz6cn4S9ZNk1oif_iTIx3whldEQTrsataDYxGMnE0H0WGE-2v6GjrvANlUacfu0OtV2G6-LnKbzVNlbnD9vsVEFZc7MLs3RwRb0ywanSOciUyMbOno-qA4xqX-yBxS8KBYgwvaEy61qGREWRpQ 
                                                                    JWT token:  null
04-27 16:10:46.982 31889-831/in.eventloop.heylo D/OkHttp: --> POST https://erzohrakhan.auth0.com/delegation HTTP/1.1
04-27 16:10:46.982 31889-831/in.eventloop.heylo D/OkHttp: Content-Type: application/json; charset=utf-8
04-27 16:10:46.982 31889-831/in.eventloop.heylo D/OkHttp: Content-Length: 810
04-27 16:10:46.982 31889-831/in.eventloop.heylo D/OkHttp: Auth0-Client: eyJuYW1lIjoiQXV0aDAuQW5kcm9pZCIsInZlcnNpb24iOiIxLjcuMCJ9
04-27 16:10:46.982 31889-831/in.eventloop.heylo D/OkHttp: Accept-Language: en_US
04-27 16:10:46.982 31889-831/in.eventloop.heylo D/OkHttp: {"id_token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Ik5VWTVSRFl5UkVOQlJEVkNOVU13UWpjME4wWkdOa1F3TjBVNVFqRTFOamM0UkRFMU1EZzNSZyJ9.eyJpc3MiOiJodHRwczovL2Vyem9ocmFraGFuLmF1dGgwLmNvbS8iLCJzdWIiOiJzbXN8NTg2OTYyMzM5ODQyMGY5YTExYWNmNDMwIiwiYXVkIjoiZ2w0NkxrMFB3MExRV04zVkRKZDlsUGpiNTBQZ0E5STAiLCJleHAiOjE0OTMzMjU2NDcsImlhdCI6MTQ5MzI4OTY0N30.NDYr01vrBrYt8jcwNiGtB9RsGGM31N_UfI4VL-HIRSDm8X4ADDQdRp3Cy9-0VXJ860CBkB_A-kv_5_ji0A3ct0DoLex03DGCItqDLXaISy6GwVMCo-DANhOCveoxFf7r5k0SFNysAZvMR-FY5eB1fnVecf7xG5bN96fy1qZGiaDxAoVLkD-iRWHp7IShPvPov6tzFz6cn4S9ZNk1oif_iTIx3whldEQTrsataDYxGMnE0H0WGE-2v6GjrvANlUacfu0OtV2G6-LnKbzVNlbnD9vsVEFZc7MLs3RwRb0ywanSOciUyMbOno-qA4xqX-yBxS8KBYgwvaEy61qGREWRpQ","client_id":"gl46Lk0Pw0LQWN3VDJd9lPjb50PgA9I0","api_type":"firebase","grant_type":"urn:ietf:params:oauth:grant-type:jwt-bearer"}
04-27 16:10:46.983 31889-831/in.eventloop.heylo D/OkHttp: --> END POST (810-byte body)
04-27 16:10:47.455 31889-831/in.eventloop.heylo D/OkHttp: <-- HTTP/1.1 400 Bad Request (472ms)
04-27 16:10:47.455 31889-831/in.eventloop.heylo D/OkHttp: Date: Thu, 27 Apr 2017 10:40:47 GMT
04-27 16:10:47.455 31889-831/in.eventloop.heylo D/OkHttp: Content-Type: application/json; charset=utf-8
04-27 16:10:47.455 31889-831/in.eventloop.heylo D/OkHttp: Content-Length: 114
04-27 16:10:47.455 31889-831/in.eventloop.heylo D/OkHttp: Connection: keep-alive
04-27 16:10:47.455 31889-831/in.eventloop.heylo D/OkHttp: Keep-Alive: timeout=100
04-27 16:10:47.455 31889-831/in.eventloop.heylo D/OkHttp: X-Auth0-RequestId: 144cce8676ae38981bba
04-27 16:10:47.455 31889-831/in.eventloop.heylo D/OkHttp: X-RateLimit-Limit: 10
04-27 16:10:47.455 31889-831/in.eventloop.heylo D/OkHttp: X-RateLimit-Remaining: 9
04-27 16:10:47.456 31889-831/in.eventloop.heylo D/OkHttp: X-RateLimit-Reset: 1493289708
04-27 16:10:47.456 31889-831/in.eventloop.heylo D/OkHttp: Cache-Control: no-cache
04-27 16:10:47.456 31889-831/in.eventloop.heylo D/OkHttp: OkHttp-Sent-Millis: 1493289646995
04-27 16:10:47.456 31889-831/in.eventloop.heylo D/OkHttp: OkHttp-Received-Millis: 1493289647454
04-27 16:10:47.457 31889-831/in.eventloop.heylo D/OkHttp: {"error":"invalid_request","error_description":"must start with number, buffer, array or string","statusCode":400}
04-27 16:10:47.457 31889-831/in.eventloop.heylo D/OkHttp: <-- END HTTP (114-byte body)
04-27 16:10:47.475 31889-831/in.eventloop.heylo E/LoginActivity: authentication exception  errorAn error occurred when trying to authenticate with the server.
                                                                 com.auth0.android.authentication.AuthenticationException: An error occurred when trying to authenticate with the server.
                                                                     at com.auth0.android.request.internal.AuthenticationErrorBuilder.from(AuthenticationErrorBuilder.java:23)
                                                                     at com.auth0.android.request.internal.AuthenticationErrorBuilder.from(AuthenticationErrorBuilder.java:9)
                                                                     at com.auth0.android.request.internal.BaseRequest.parseUnsuccessfulResponse(BaseRequest.java:137)
                                                                     at com.auth0.android.request.internal.SimpleRequest.onResponse(SimpleRequest.java:68)
                                                                     at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:177)
                                                                     at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
                                                                     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                                                                     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                                     at java.lang.Thread.run(Thread.java:818)
04-27 16:11:00.620 31889-31895/in.eventloop.heylo W/art: Suspending all threads took: 158.661ms

P.S: I am using auth0 for mobile number authentication just like watsapp or viber.

I have a similar issue but with iOS app.

I simply picking id_token form debugger not even going to far with writing the complete code for making a request and passing it to the curl suggested by Auth0 docs. Nothing works.

curl --data “client_id=eVqDFsoCcgtagfOpvcmbWkhVlxaIkJY3&
&grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer
&id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.euJpc3MiOiJodHRwczovL3N0YXJ0dXBjcmFmdC5ldS5hdXRoMC5jb20vIiwic3ViIjoiYXV0aDB8NTkxYTVjZTU4ZTQ2NmY2ZDY4NWRmYTlmIiwiYXVkIjoiZVZxREZzb0NjZ3RobXNP
cHZjbWJXa2hWbHhhRmlKWTMiLCJleHAiOjE0OTUyMTlzMjMsImlhdCI6MTQ5NTE4MzMyM30.p165Qdzq3a9cq_3RyozEWccKeLvLwQOIUFA-npEh2lM
&scope=open_id
&api_type=firebase” https://app.eu.auth0.com/delegation

I have this error:
{“error”:“invalid_token”,“error_description”:“Not enough or too many segments”,“statusCode”:401}

I don’t want to give up on Auth0 and go straight to Firebase which also has auth. I have lost 3 hours trying to solve that. If it’s late to help me - someone else could receive your help :wink:

Thanks!

Well I will suggest you to move on to firebase auth because auth0 support is really bad. I have raised this problem with 4-5 engineers of auth0 but no one replied. This product is good but the biggest problem is support and this is the not the first time I am facing this issue with auth0 support.
Even I am planning to move to firebase auth(mobile no + SMS code).