Okta Client using OAuth2ClientCredentials

Hello All,

I am trying to create Client using OAuth2ClientCredentials instead of TokenClientCredentials. But I don’t see any examples, most of the example that I see is using TokenClientCredentials.

Below is what I have so far, and since it seems DefaultClientBuilder looks for okta.yml or okta.properties file, I have created okta.properties file and added it to classpath.

In okta.properties file I have added all the properties mentioned in below Okta SDK okta.properties file, here okta-sdk-java/okta.properties at master · okta/okta-sdk-java · GitHub

DefaultClientBuilder defaultClientBuilder = new DefaultClientBuilder();
ClientConfiguration clientConfiguration =  defaultClientBuilder.getClientConfiguration();

AccessTokenRetrieverService accessTokenRetrieverService =
        new AccessTokenRetrieverServiceImpl(clientConfiguration);

Clients.builder()
        .setOrgUrl(orgUrl)
        .setClientCredentials(new OAuth2ClientCredentials(accessTokenRetrieverService))
        .build();

Below error I see

Caused by: java.lang.NullPointerException: null
	at com.okta.sdk.impl.config.ClientConfiguration.getRequestAuthenticator(ClientConfiguration.java:185)
	at com.okta.sdk.impl.config.ClientConfiguration.toString(ClientConfiguration.java:213)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)

Did you follow the guide to configure a service app to use OAuth for Okta?
https://developer.okta.com/docs/guides/implement-oauth-for-okta-serviceapp/overview/

Yes, I did follow that guide to configure service app, but I could not get the Okta Client to work using OAuth2ClientCredentials.

But I did get the Okta Client working as below to use OAuth

Clients.builder()
        .setOrgUrl(oktadomain)
        //.setClientCredentials(new OAuth2ClientCredentials(accessTokenRetrieverService))

        .setAuthorizationMode(AuthorizationMode.PRIVATE_KEY)
        .setPrivateKey("private key")
        .setClientId("client id")
        .setScopes(new HashSet<>(Arrays.asList("okta.users.read")))
        .build();