# Can't create a JWT using jsonwebtoken.dev

**URL:** <https://devforum.okta.com/t/cant-create-a-jwt-using-jsonwebtoken-dev/31070>\
**Category:** OAuth/OIDC\
**Created:** [October 30, 2024, 5:29pm UTC](https://devforum.okta.com/t/cant-create-a-jwt-using-jsonwebtoken-dev/31070 "2024-10-30T17:29:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mraible](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/mraible/32/37_2.png) [@mraible](https://devforum.okta.com/u/mraible)\
**Post date:** [October 30, 2024, 5:29pm UTC](https://devforum.okta.com/t/cant-create-a-jwt-using-jsonwebtoken-dev/31070/1 "2024-10-30T17:29:18Z")

</div>

I’m trying to use [this documentation](https://developer.okta.com/docs/guides/implement-oauth-for-okta-serviceapp/main/#create-and-sign-the-jwt) to create a JWT so I can read a list of users from Okta’s API using client credentials. When I submit the form values to generate a JWT, it results in the following error.

 ![Screenshot 2024-10-30 at 11.25.01 AM](https://global.discourse-cdn.com/oktadev/original/2X/f/f8d0849d593f6c1e3c6ef960343d03f764bf1c97.jpeg)

I realize I can [use JJWT as a workaround](https://developer.okta.com/docs/guides/build-self-signed-jwt/java/main/#build-a-jwt-with-a-private-key), but that seems like a lot of work.

My use case is I’m trying to create an API integration in another system that uses client credentials to get a user. Ideally, the following would work (from [these docs](https://developer.okta.com/docs/guides/build-api-integration/main/#request-an-access-token)):

```bash
curl --request POST \
  --url https://dev-87506503.okta.com/oauth2/v1/token \
  --header 'Accept: application/json' \
  --header 'Authorization: Basic MG9...' \
  --header 'Cache-control: no-cache' \
  --header 'Content-type: application/x-www-form-urlencoded' \
  --data 'grant_type=client_credentials&scope=okta.users.read'

```

Unfortunately, it results in the following error:

```json
{"error":"invalid_client","error_description":"Client Credentials requests to the Org Authorization Server must use the private_key_jwt token_endpoint_auth_method."}

```

---

<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:** [October 30, 2024, 5:37pm UTC](https://devforum.okta.com/t/cant-create-a-jwt-using-jsonwebtoken-dev/31070/2 "2024-10-30T17:37:05Z")

</div>

Hey @mraible! Can you provide a screenshot showing what you are providing to jsonwebtoken.dev when you try to use it to generate a JWT (censoring your private key, naturally!) I think I’ve encountered this issue before as well, but I believe the cause was due to some malformed JSON bodies being passed in

---

<div class="post-metadata">

**Author:** ![mraible](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/mraible/32/37_2.png) [@mraible](https://devforum.okta.com/u/mraible)\
**Post date:** [October 30, 2024, 5:44pm UTC](https://devforum.okta.com/t/cant-create-a-jwt-using-jsonwebtoken-dev/31070/3 "2024-10-30T17:44:24Z")

</div>

I’m using the value returned from [https://dev-87506503.okta.com/oauth2/v1/keys](https://dev-87506503.okta.com/oauth2/v1/keys) for the signing key. For the payload, I’m using:

```json
{
    "aud": "https://dev-87506503.okta.com/oauth2/v1/token",
    "iss": "0oakriog6sNqa2Owb5d7",
    "sub": "0oakriog6sNqa2Owb5d7",
    "exp": "1730267241"
}

```

---

<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:** [October 31, 2024, 3:31pm UTC](https://devforum.okta.com/t/cant-create-a-jwt-using-jsonwebtoken-dev/31070/4 "2024-10-31T15:31:27Z")

</div>

Ah, that might be it then. While your payload looks good, you need to pass it a private key, not a public key, to sign the JWT, which is why you need to generate your own key pair for this purpose.

Luckily, you can let Okta generate a Public/Private key pair for you either [within the UI](https://developer.okta.com/docs/guides/implement-oauth-for-okta-serviceapp/main/#generate-the-jwk-using-the-admin-console) or [via API](https://developer.okta.com/docs/guides/implement-oauth-for-okta-serviceapp/main/#generate-the-jwk-using-the-api). You can then use the JWKS (for the public/private key) that Okta returns as your signing key and it will contain all the parts of a private key. Structure of this key is as follows:

```auto
{
	    "d": "***",
	    "p": "***",
	    "q": "***",
	    "dp": "***",
	    "dq": "***",
	    "qi": "***",
	    "kty": "RSA",
	    "e": "AQAB",
	    "kid": "***",
	    "n": "***"
	}
```

---

<div class="post-metadata">

**Author:** ![mraible](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/mraible/32/37_2.png) [@mraible](https://devforum.okta.com/u/mraible)\
**Post date:** [November 1, 2024, 7:20pm UTC](https://devforum.okta.com/t/cant-create-a-jwt-using-jsonwebtoken-dev/31070/5 "2024-11-01T19:20:37Z")

</div>

Happy Friday, Andrea! I was able to generate a public/private key with the UI and successfully generated a signed JWT using [https://www.jsonwebtoken.dev](https://www.jsonwebtoken.dev). I set this as a TOKEN environment variable, then tried to Get an access token as specified in the docs:

```auto
curl --location --request POST 'https://dev-87506503.okta.com/oauth2/v1/token' \
    --header 'Accept: application/json' \
    --header 'Content-Type: application/x-www-form-urlencoded' \
    --data-urlencode 'grant_type=client_credentials' \
    --data-urlencode 'scope=okta.users.read' \
    --data-urlencode 'client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer' \
    --data-urlencode 'client_assertion=$TOKEN'

```

The response I get is:

```json
{
  "errorCode": "invalid_client",
  "errorSummary": "A client_id must be provided in the request.",
  "errorLink": "invalid_client",
  "errorId": "oaeuwYwd_9ZQY6MxWgxAiBvzQ",
  "errorCauses": []
}

```

The client ID I used to generate the signed JWT is from the service app I created. Do the docs need to be updated or am I doing something wrong?

---

<div class="post-metadata">

**Author:** ![mraible](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/mraible/32/37_2.png) [@mraible](https://devforum.okta.com/u/mraible)\
**Post date:** [November 1, 2024, 7:30pm UTC](https://devforum.okta.com/t/cant-create-a-jwt-using-jsonwebtoken-dev/31070/6 "2024-11-01T19:30:47Z")

</div>

If I don’t use `$TOKEN` in the curl command, and use the raw value instead, it works. Then, I can make a request to the `/api/v1/users` API with the returned access token and see users.

```bash
curl -i -X GET 'https://dev-87506503.okta.com/api/v1/users' \
  -H 'Authorization: Bearer ...' \
  -H 'Content-Type: string'

```

Thanks for all your help!

---

<div class="post-metadata">

**Author:** ![mraible](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/mraible/32/37_2.png) [@mraible](https://devforum.okta.com/u/mraible)\
**Post date:** [November 1, 2024, 7:50pm UTC](https://devforum.okta.com/t/cant-create-a-jwt-using-jsonwebtoken-dev/31070/7 "2024-11-01T19:50:25Z")

</div>

Here are the [HTTPie](https://httpie.io/) commands (for my own reference, since it’s my preferred HTTP client).

```bash
# Get an access token with a signed JWT
https --form POST dev-87506503.okta.com/oauth2/v1/token \
  'Accept: application/json' \
  grant_type=client_credentials \
  scope=okta.users.read \
  client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer \
  client_assertion=$TOKEN

ACCESS_TOKEN=<returned value>

https dev-87506503.okta.com/api/v1/users Authorization:'Bearer '$ACCESS_TOKEN

```

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/oktadev/original/1X/589c4c5f80de422d01f47cee7185a7fb8227835c.png) [@system](https://devforum.okta.com/u/system)\
**Post date:** [November 22, 2024, 4:50pm UTC](https://devforum.okta.com/t/cant-create-a-jwt-using-jsonwebtoken-dev/31070/8 "2024-11-22T16:50:13Z")

</div>

Closed on November 22, 2024, 4:50pm UTC

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.
