Skip to main content

Introducing Automatic Auth-Token Retrieval in PHP, Python, Ruby, Go, and C# SDKs

· 4 min read

We are introducing automatic token retrieval and refreshing in the OAuth 2.0 grant types.

Details

SDKs now have automated auth-token retrieval or refreshing feature prior to any API call necessitating OAuth 2.0 Grant Type.

What's New?

Earlier, it was user's responsibility to fetch/refresh the auth-token and update the SDK client with newly fetched auth-token.

Now, after configuring OAuth 2.0 grant type credentials in the SDK client, the SDK handles this automatically before making any API call that requires an auth-token.

note

This feature is currently supported in all languages SDKs using OAuth 2.0 client credentials grant type.

The SDK automatically retrieves or refreshes the auth-token from the auth server if it's expired or undefined by default.

For those wanting a custom auth-token provider, you can register a hook to supply the auth-token from a unique source. Additionally, you can register for the auth-token update event, which triggers whenever the auth-token is updated.

Here's how to set up callbacks in the client for the auth-token provider and auth-token update event.

$client = SdkClientBuilder::init()
->clientCredentialsAuthCredentials(
ClientCredentialsAuthCredentialsBuilder::init(
'OAuthClientId',
'OAuthClientSecret'
)
->oAuthTokenProvider(
function (?OAuthToken $lastOAuthToken, ClientCredentialsAuthManager $authManager): OAuthToken {
// Add the callback handler to provide a new OAuth token.
// It will be triggered whenever the lastOAuthToken is null or expired.
return $this->loadTokenFromDatabase() ?? $authManager->fetchToken();
}
)
->oAuthOnTokenUpdate(
function (OAuthToken $oAuthToken): void {
// Add the callback handler to perform operations like save to DB or file etc.
// It will be triggered whenever the token gets updated.
$this->saveTokenToDatabase($oAuthToken);
}
)
)
->build();