# Bad Request, when trying to get Access token using JWT token

**URL:** <https://devforum.okta.com/t/bad-request-when-trying-to-get-access-token-using-jwt-token/18443>\
**Category:** Questions\
**Tags:** spring, java\
**Created:** [December 9, 2021, 1:58am UTC](https://devforum.okta.com/t/bad-request-when-trying-to-get-access-token-using-jwt-token/18443 "2021-12-09T01:58:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![d12santosh](https://avatars.discourse-cdn.com/v4/letter/d/ed655f/32.png) [@d12santosh](https://devforum.okta.com/u/d12santosh)\
**Post date:** [December 9, 2021, 1:58am UTC](https://devforum.okta.com/t/bad-request-when-trying-to-get-access-token-using-jwt-token/18443/1 "2021-12-09T01:58:36Z")

</div>

Hello Team,

I am trying to establish authorization between a web application (non OKTA) and spring boot resource server and followed the approach in below links, get bad request when trying to get access token from web application.

> **[Implement OAuth for Okta with a service app | Okta Developer](https://developer.okta.com/docs/guides/implement-oauth-for-okta-serviceapp/main/)**
>
> Secure, scalable, and highly available authentication and user management for any app.

> [@Extracting private key for jwt library from jwks](https://devforum.okta.com/t/extracting-private-key-for-jwt-library-from-jwks/8929/3):
>
> Hi Nacha, You can use the java security classes to build a java Private key using the n and d parameters of the jwk: RSAPrivateKeySpec rsaPrivateKeySpec = new RSAPrivateKeySpec( new BigInteger(1, Base64.getUrlDecoder().decode(/\* keyModulus, n \*/)), new BigInteger(1, Base64.getUrlDecoder().decode(/\* privateExponent, d \*/) ); KeyFactory factory = KeyFactory.getInstance(“RSA”); PrivateKey privateKey = factory.generatePrivate(rsaPrivateKeySpec); [https://docs.oracle.com/javase/8/docs/api/java…](https://docs.oracle.com/javase/8/docs/api/java/security/spec/RSAPrivateKeySpec.html)

```auto
RSAPrivateKeySpec rsaKey = new RSAPrivateKeySpec(new BigInteger(1, Base64.getUrlDecoder()
                    .decode("rII5ofpQRVZw2ZSCDqBGHxsHsWfukjp1")),
                    new BigInteger(1,Base64.getUrlDecoder().decode("hAnA***GoQ")));
            KeyFactory factory = KeyFactory.getInstance("RSA");
            PrivateKey privateKey = factory.generatePrivate(rsaKey);
String jwt = Jwts.builder()
                    .setAudience(<Dev OKTA Custom Server>)
                    .setIssuedAt(Date.from(now))
                    .setExpiration(Date.from(now.plus(5L, ChronoUnit.MINUTES)))
                    .setIssuer("0x7")
                    .setSubject("0o4x7")
                    .setId(UUID.randomUUID().toString())
                    .signWith(privateKey)
                    .compact();
// Rest call
            MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
            headers.set("Content-Type", "application/x-www-form-urlencoded");
            headers.set("Accept", "application/json");
 
MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
            body.set("client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer");
           body.set("grant_type", "client_credentials");
           body.set("client_assertion", jwt);
            body.set("scope", "okta.users.read");

HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(body, headers);
            ResponseEntity<String> response = restTemplate.exchange(<OKTA Dev Custom Server Token URL>,HttpMethod.POST, entity, String.class);

//

```

kindly support and also correct me if my understanding is wrong.

Thank you

---

<div class="post-metadata">

**Author:** ![andrea](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/andrea/32/2172_2.png) [@andrea](https://devforum.okta.com/u/andrea)\
**Post date:** [December 9, 2021, 4:25pm UTC](https://devforum.okta.com/t/bad-request-when-trying-to-get-access-token-using-jwt-token/18443/2 "2021-12-09T16:25:45Z")

</div>

Any specific error message coming back from /token?

---

<div class="post-metadata">

**Author:** ![d12santosh](https://avatars.discourse-cdn.com/v4/letter/d/ed655f/32.png) [@d12santosh](https://devforum.okta.com/u/d12santosh)\
**Post date:** [December 10, 2021, 3:11am UTC](https://devforum.okta.com/t/bad-request-when-trying-to-get-access-token-using-jwt-token/18443/3 "2021-12-10T03:11:28Z")

</div>

No, only receiving Bad Request Message.

---

<div class="post-metadata">

**Author:** ![andrea](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/andrea/32/2172_2.png) [@andrea](https://devforum.okta.com/u/andrea)\
**Post date:** [December 13, 2021, 5:10pm UTC](https://devforum.okta.com/t/bad-request-when-trying-to-get-access-token-using-jwt-token/18443/4 "2021-12-13T17:10:52Z")

</div>

is your request URL correct? Are you able to request different types of tokens using different oauth flows and clients?

---

<div class="post-metadata">

**Author:** ![andrea](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/andrea/32/2172_2.png) [@andrea](https://devforum.okta.com/u/andrea)\
**Post date:** [February 8, 2024, 8:13pm UTC](https://devforum.okta.com/t/bad-request-when-trying-to-get-access-token-using-jwt-token/18443/5 "2024-02-08T20:13:18Z")

</div>

Closed on February 8, 2024, 8:13pm UTC


