# Okta Client using OAuth2ClientCredentials

**URL:** <https://devforum.okta.com/t/okta-client-using-oauth2clientcredentials/14651>\
**Category:** OAuth/OIDC\
**Created:** [June 10, 2021, 7:32am UTC](https://devforum.okta.com/t/okta-client-using-oauth2clientcredentials/14651 "2021-06-10T07:32:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![abta](https://avatars.discourse-cdn.com/v4/letter/a/b77776/32.png) [@abta](https://devforum.okta.com/u/abta)\
**Post date:** [June 10, 2021, 7:32am UTC](https://devforum.okta.com/t/okta-client-using-oauth2clientcredentials/14651/1 "2021-06-10T07:32:03Z")

</div>

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](https://github.com/okta/okta-sdk-java/blob/master/impl/src/main/resources/com/okta/sdk/config/okta.properties)

```auto
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

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![okra-okta](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/okra-okta/32/2838_2.png) [@okra-okta](https://devforum.okta.com/u/okra-okta)\
**Post date:** [June 15, 2021, 4:31pm UTC](https://devforum.okta.com/t/okta-client-using-oauth2clientcredentials/14651/2 "2021-06-15T16:31:27Z")

</div>

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/](https://developer.okta.com/docs/guides/implement-oauth-for-okta-serviceapp/overview/)

---

<div class="post-metadata">

**Author:** ![abta](https://avatars.discourse-cdn.com/v4/letter/a/b77776/32.png) [@abta](https://devforum.okta.com/u/abta)\
**Post date:** [June 17, 2021, 4:48am UTC](https://devforum.okta.com/t/okta-client-using-oauth2clientcredentials/14651/3 "2021-06-17T04:48:08Z")

</div>

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

```auto
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();

```

---

<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 10, 2024, 12:31am UTC](https://devforum.okta.com/t/okta-client-using-oauth2clientcredentials/14651/4 "2024-02-10T00:31:44Z")

</div>

Closed on February 10, 2024, 12:31am UTC


