<iframe src="https://www.googletagmanager.com/ns.html?id=GTM-M74D8PB" height="0" width="0" style="display:none;visibility:hidden">
Loading
Skip to NavigationSkip to Main Content
0D54z00007SJiK2CALOkta Classic EngineAPI Access ManagementAnswered2026-06-01T22:31:43.000Z2022-02-21T08:50:49.000Z2026-06-01T22:31:43.000Z

dac3n (dac3n) asked a question.

Access token for API services

Hi All,

 

I am trying to generate JWT token in my standalone Java code by following the guideline provided in https://developer.okta.com/docs/guides/build-self-signed-jwt/java/main/*build-a-jwt-with-a-private-key

 

However while generating the JWT using the private key I am getting below exception:

 

Base64-encoded key bytes may only be specified for HMAC signatures. If using RSA or Elliptic Curve, use the signWith(SignatureAlgorithm, Key) method instead.

 

Below is code snippet:

 

public void getJWT1() throws Exception

{

Gson gson = new Gson();

Map<String,String> keyMap = new HashMap<>();

keyMap.put("d", "Value");

keyMap.put("q","Value");

keyMap.put("p", "Value");

keyMap.put("dp", "Value");

keyMap.put("dq", "Value");

keyMap.put("qi", "Value");

keyMap.put("kty", "RSA");

keyMap.put("e", "AQAB");

keyMap.put("kid", "sVdxUhe6FJ1d0uDp4-qPqkCbrR1SnZUmXXFiVhKP4rw");

keyMap.put("n", "Value");

String key = gson.toJson(keyMap);

Instant now = Instant.now();

String UUID="avbch";

String jwt = Jwts.builder()

    .setAudience("https://tenent-url/oauth2/default/v1/token")

    .setIssuedAt(Date.from(now))

    .setExpiration(Date.from(now.plus(5L, ChronoUnit.MINUTES)))

    .setIssuer("Clinet Id")

    .setSubject("Client id")

    .setId(UUID) 

    .signWith(SignatureAlgorithm.RS256, key)

    .compact();

System.out.println(jwt);

 

Also when I try to generate using client id and secret using same article I get below exception

 

Exception in thread "main" java.lang.NoSuchMethodError: io.jsonwebtoken.SignatureAlgorithm.getMinKeyLength()I

at io.jsonwebtoken.security.Keys.hmacShaKeyFor(Keys.java:69)

 

Let me know if anyone can suggest the changes to make this working.

 

Regards

Rahul Jha


This question is closed.
Loading
Access token for API services
Okta Support Agent (Beta)

Terms of Service

Hello! I'm the Okta Support Agent (Beta), your go-to resource for questions about Okta products. Please remember for your security and privacy, do not share any Personally Identifiable Information (PII) such as passwords, IP addresses, or other details with me.

Okta Privacy Policy and Terms of Service apply