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.
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.
- PHP
- Ruby
- Python
- C#
- Go
$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();
def _o_auth_token_provider(last_oauth_token, auth_manager)
# Add the callback handler to provide a new OAuth token
# It will be triggered whenever the last provided o_auth_token is null or expired
o_auth_token = load_token_from_database()
o_auth_token = auth_manager.fetch_token() if o_auth_token is nil?
return o_auth_token
end
client = SDKClient.new(
client_credentials_auth_credentials: ClientCredentialsAuthCredentials.new(
o_auth_client_id: 'OAuthClientId',
o_auth_client_secret: 'OAuthClientSecret',
o_auth_token_provider: _o_auth_token_provider,
o_auth_on_token_update: Proc.new { | o_auth_token |
# Add the callback handler to perform operations like save to DB or file etc.
# It will be triggered whenever the token gets updated
save_token_to_database(o_auth_token)
}
)
)
def _o_auth_token_provider(last_oauth_token, auth_manager):
# Add the callback handler to provide a new OAuth token
# It will be triggered whenever the last provided o_auth_token is null or expired
o_auth_token = load_token_from_database()
if o_auth_token is None:
o_auth_token = auth_manager.fetch_token()
return o_auth_token
client = SDKClient(
client_credentials_auth_credentials=ClientCredentialsAuthCredentials(
o_auth_client_id='OAuthClientId',
o_auth_client_secret='OAuthClientSecret',
o_auth_scopes=[
OAuthScopeEnum.READ_SCOPE,
OAuthScopeEnum.WRITE_SCOPE
],
o_auth_token_provider=_o_auth_token_provider,
o_auth_on_token_update=(lambda o_auth_token:
# Add the callback handler to perform operations like save to DB or file etc.
# It will be triggered whenever the token gets updated
save_token_to_database(o_auth_token))
)
)
SdkClient client = new SdkClient.Builder()
.ClientCredentialsAuth(
new ClientCredentialsAuthModel.Builder(
"OAuthClientId",
"OAuthClientSecret"
)
.oAuthTokenProvider(async (token, credentialsManager) =>
{
// Add the callback handler to provide a new OAuth token
// It will be triggered whenever the lastOAuthToken is undefined or expired
return LoadTokenFromDatabase() ?? await FetchTokenAsync()
})
.oAuthOnTokenUpdate(token ->
{
// It will be triggered whenever the token gets updated
SaveTokenToDatabase(token);
})
.Build())
.Build();
client := Sdkclient.NewClient(
Sdkclient.CreateConfiguration(
Sdkclient.WithClientCredentialsAuthCredentials(
Sdkclient.NewClientCredentialsAuthCredentials(
"OAuthClientId",
"OAuthClientSecret",
).
WithOAuthOnTokenUpdate(func(oAuthToken models.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)
}).
WithOAuthTokenProvider(func(lastOAuthToken models.OAuthToken, authManager ClientCredentialsAuthManager) models.OAuthToken {
// Add the callback function handler to provide a new OAuth token
// It will be triggered whenever the lastOAuthToken is undefined or expired
oAuthToken := loadTokenFromDatabase()
if oAuthToken.AccessToken == "" {
if token, err := authManager.FetchToken(context.TODO()); err == nil {
return token
}
}
return oAuthToken
}),
),
),
)