PowerBuilder
PowerBuilder
Refresh OAuth2 Access Token with Optional Params
See more OAuth2 Examples
Demonstrates how to refresh an OAuth2 access token with optional query params included in the HTTP request.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_JsonToken
oleobject loo_Oauth2
oleobject loo_SbJson
li_Success = 0
// Here is a sampling of possible optional parameters that might
// be used by some OAuth2 providers.
// Optional Parameters
//
// "scope":
// Specifies the scope of the access request. If omitted, the authorization
// server may issue a token with the same scope as the original token.
// Example: "scope=read write"
//
// "redirect_uri":
// The redirect URI used in the original authorization request. Some
// servers may require this for validation.
// Example: "redirect_uri=https://example.com/callback"
//
// "resource":
// Indicates the target resource or audience for the token. This is used in
// some implementations (e.g., Microsoft Identity Platform).
// Example: "resource=https://api.example.com"
//
// "audience":
// Similar to "resource", this specifies the intended audience for the
// token (used in some OAuth2 implementations like Auth0).
// Example: "audience=https://api.example.com"
//
// "client_assertion" and "client_assertion_type":
// Used for client authentication using a signed JWT instead of a client
// secret.
// Example:client_assertion=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9...
// client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer
//
// "token_type_hint":
// Provides a hint to the authorization server about the type of token
// being refreshed. This is rarely used but can be helpful in some cases.
// Example: "token_type_hint=refresh_token"
//
// "assertion":
// Used in some flows (e.g., SAML bearer assertion flow) to provide an
// assertion for token issuance.
// Example: "assertion=PHNhbWxwOl..."
//
// "requested_token_use":
// Specifies how the token will be used (e.g., "on_behalf_of" in the
// On-Behalf-Of flow used by Microsoft Identity Platform).
// Example: "requested_token_use=on_behalf_of"
//
// --------------------------------------------------------------------------------
// This example wll refresh the access token and includes the "audience"
// optional query parameter.
//
//
// Get the access token to be refreshed.
loo_JsonToken = create oleobject
li_rc = loo_JsonToken.ConnectToNewObject("Chilkat.JsonObject")
if li_rc < 0 then
destroy loo_JsonToken
MessageBox("Error","Connecting to COM object failed")
return
end if
li_Success = loo_JsonToken.LoadFile("qa_data/tokens/myAccessToken.json")
if li_Success <> 1 then
Write-Debug "Failed to load hmrc.json"
destroy loo_JsonToken
return
end if
loo_Oauth2 = create oleobject
li_rc = loo_Oauth2.ConnectToNewObject("Chilkat.OAuth2")
loo_Oauth2.TokenEndpoint = "https://api.example.com/oauth/token"
// Replace these with actual values.
loo_Oauth2.ClientId = "CLIENT_ID"
loo_Oauth2.ClientSecret = "CLIENT_SECRET"
// Add the optional refresh query param.
// Call AddRefreshQueryParam multiple times to add additional params.
loo_Oauth2.AddRefreshQueryParam("audience","https://api.example.com")
// Provide the existing refresh token from the JSON.
loo_Oauth2.RefreshToken = loo_JsonToken.StringOf("refresh_token")
// Send the HTTP POST to refresh the access token..
li_Success = loo_Oauth2.RefreshAccessToken()
if li_Success <> 1 then
Write-Debug loo_Oauth2.LastErrorText
destroy loo_JsonToken
destroy loo_Oauth2
return
end if
// Load the access token response into the json object
loo_JsonToken.Load(loo_Oauth2.AccessTokenResponse)
// Save the new JSON access token response to a file.
// The access + refresh tokens contained in this JSON will be needed for the next refresh.
loo_SbJson = create oleobject
li_rc = loo_SbJson.ConnectToNewObject("Chilkat.StringBuilder")
loo_JsonToken.EmitCompact = 0
loo_JsonToken.EmitSb(loo_SbJson)
loo_SbJson.WriteFile("qa_data/tokens/myAccessToken.json","utf-8",0)
Write-Debug "OAuth2 access token refreshed!"
Write-Debug "New Access Token = " + loo_Oauth2.AccessToken
destroy loo_JsonToken
destroy loo_Oauth2
destroy loo_SbJson