OAuth 2 Client Credentials Grant

November 13, 2025 ยท View on GitHub

Documentation for accessing and setting credentials for Oauth2.

Auth Credentials

NameTypeDescriptionSetterGetter
OAuthClientIdStringOAuth 2 Client IDoAuthClientIdgetOAuthClientId()
OAuthClientSecretStringOAuth 2 Client SecretoAuthClientSecretgetOAuthClientSecret()
OAuthTokenOAuthTokenObject for storing information about the OAuth tokenoAuthTokengetOAuthToken()
OAuthClockSkewlongClock skew time in seconds applied while checking the OAuth Token expiry.oAuthClockSkewgetOAuthClockSkew()
OAuthTokenProviderBiFunction<OAuthToken, ClientCredentialsAuth, OAuthToken>Registers a callback for oAuth Token Provider used for automatic token fetching/refreshing.oAuthTokenProvidergetOAuthTokenProvider()
OAuthOnTokenUpdateConsumer<OAuthToken>Registers a callback for token update event.oAuthOnTokenUpdategetOAuthOnTokenUpdate()

Note: Auth credentials can be set using clientCredentialsAuth in the client builder and accessed through getClientCredentialsAuth method in the client instance.

Usage Example

Client Initialization

You must initialize the client with OAuth 2.0 Client Credentials Grant credentials as shown in the following code snippet. This will fetch the OAuth token automatically when any of the endpoints, requiring OAuth 2.0 Client Credentials Grant authentication, are called.

import com.paypal.sdk.PaypalServerSdkClient;
import com.paypal.sdk.authentication.ClientCredentialsAuthModel;
import com.paypal.sdk.exceptions.ApiException;
import com.paypal.sdk.models.OAuthToken;
import java.io.IOException;

public class Program {
    public static void main(String[] args) {
        PaypalServerSdkClient client = new PaypalServerSdkClient.Builder()
            .clientCredentialsAuth(new ClientCredentialsAuthModel.Builder(
                    "OAuthClientId",
                    "OAuthClientSecret"
                )
                .build())
            .build();
    }
}

Your application can also manually provide an OAuthToken using the setter oAuthToken in ClientCredentialsAuthModel object. This function takes in an instance of OAuthToken containing information for authorizing client requests and refreshing the token itself.

Adding OAuth Token Update Callback

Whenever the OAuth Token gets updated, the provided callback implementation will be executed. For instance, you may use it to store your access token whenever it gets updated.

import com.paypal.sdk.PaypalServerSdkClient;
import com.paypal.sdk.exceptions.ApiException;
import com.paypal.sdk.models.OAuthToken;
import java.io.IOException;

public class Program {
    public static void main(String[] args) {
        PaypalServerSdkClient client = new PaypalServerSdkClient.Builder()
            .clientCredentialsAuth(new ClientCredentialsAuthModel.Builder(
                    "OAuthClientId",
                    "OAuthClientSecret"
                )
                .oAuthOnTokenUpdate(oAuthToken -> {
                        // Add the callback handler to perform operations like save to DB or file etc.
                        // It will be triggered whenever the token gets updated
                        saveTokenToDatabase(oAuthToken);
                })
                .build())
            .build();
    }
}

Adding Custom OAuth Token Provider

To authorize a client using a stored access token, set up the oAuthTokenProvider in ClientCredentialsAuthModel builder along with the other auth parameters before creating the client:

import com.paypal.sdk.PaypalServerSdkClient;
import com.paypal.sdk.exceptions.ApiException;
import com.paypal.sdk.models.OAuthToken;
import java.io.IOException;

public class Program {
    public static void main(String[] args) {
        PaypalServerSdkClient client = new PaypalServerSdkClient.Builder()
            .clientCredentialsAuth(new ClientCredentialsAuthModel.Builder(
                    "OAuthClientId",
                    "OAuthClientSecret"
                )
                .oAuthTokenProvider((lastOAuthToken, credentialsManager) -> {
                        // Add the callback handler to provide a new OAuth token
                        // It will be triggered whenever the lastOAuthToken is undefined or expired
                        OAuthToken oAuthToken = loadTokenFromDatabase();
                        if (oAuthToken != null && !credentialsManager.isTokenExpired(oAuthToken)) {
                            return oAuthToken;
                        }
                        return credentialsManager.fetchToken();
                })
                .build())
            .build();
    }
}