For server to server integration between two salesforce org. I have used JWT exchange token authentication. I found a difference between JWT created via Apex Auth.JWTBearerTokenExchange and Named credentials. I have used same config but they generate different encoded JWT. Integration is working fine with Apex but it gives exception if I use Named credentials.
Named credentials generated request :
Named credentials generated JWT decoded:
I see scope and jti is added in JWT payload and that might be end up with different encoded JWT. Further, REST request with named credentials always returning "System.CalloutException: Unable to complete the JWT token exchange." that is not very helpful.
I confirmed all the config and values are same. We want to use NC and since we had no luck, we replicated same with Apex and not sure what is root cause. Any suggestions?


