Sample code for 30+ languages & platforms
PowerBuilder

Okta Client Credentials FLow

See more Okta OAuth/OIDC Examples

The Client Credentials flow is recommended for use in machine-to-machine authentication. Your application will need to securely store its Client ID and Secret and pass those to Okta in exchange for an access token. At a high-level, the flow only has two steps:
  • Your application passes its client credentials to your Okta authorization server.
  • If the credentials are accurate, Okta responds with an access token.

Note: This example uses "customScope". You'll replace it with whatever scope(s) you've defined for your app. Scopes are defined in your Authorization Server. See Okta Authorization Server / Scopes

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Http
oleobject loo_Req
oleobject loo_Resp
oleobject loo_SbResponseBody
oleobject loo_JResp
long li_RespStatusCode
string ls_Access_token
string ls_Token_type
long li_Expires_in
string ls_Scope

li_Success = 0

//  This example assumes the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if

//  Implements the following CURL command:

//  curl --request POST \
//    --url https://{yourOktaDomain}/oauth2/default/v1/token \
//    --header 'accept: application/json' \
//    --user "client_id:client_secret" \
//    --header 'cache-control: no-cache' \
//    --header 'content-type: application/x-www-form-urlencoded' \
//    --data 'grant_type=client_credentials&scope=customScope'

loo_Http.Login = "client_id"
loo_Http.Password = "client_secret"

loo_Req = create oleobject
li_rc = loo_Req.ConnectToNewObject("Chilkat.HttpRequest")

loo_Req.HttpVerb = "POST"
loo_Req.Path = "/oauth2/default/v1/token"
loo_Req.ContentType = "application/x-www-form-urlencoded"
loo_Req.AddParam("grant_type","client_credentials")
loo_Req.AddParam("scope","customScope")

loo_Req.AddHeader("accept","application/json")

loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

li_Success = loo_Http.HttpReq("https://{yourOktaDomain}/oauth2/default/v1/token",loo_Req,loo_Resp)
if li_Success = 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_Req
    destroy loo_Resp
    return
end if

loo_SbResponseBody = create oleobject
li_rc = loo_SbResponseBody.ConnectToNewObject("Chilkat.StringBuilder")

loo_Resp.GetBodySb(loo_SbResponseBody)
loo_JResp = create oleobject
li_rc = loo_JResp.ConnectToNewObject("Chilkat.JsonObject")

loo_JResp.LoadSb(loo_SbResponseBody)
loo_JResp.EmitCompact = 0

Write-Debug "Response Body:"
Write-Debug loo_JResp.Emit()

li_RespStatusCode = loo_Resp.StatusCode
Write-Debug "Response Status Code = " + string(li_RespStatusCode)
if li_RespStatusCode >= 400 then
    Write-Debug "Response Header:"
    Write-Debug loo_Resp.Header
    Write-Debug "Failed."
    destroy loo_Http
    destroy loo_Req
    destroy loo_Resp
    destroy loo_SbResponseBody
    destroy loo_JResp
    return
end if

//  Sample JSON response:
//  (Sample code for parsing the JSON response is shown below)

//  {
//    "access_token": "eyJraWQiO ... B2CnCLj7GRUW3mQ",
//    "token_type": "Bearer",
//    "expires_in": 3600,
//    "scope": "customScope"
//  }

//  Sample code for parsing the JSON response...
//  Use the following online tool to generate parsing code from sample JSON:
//  Generate Parsing Code from JSON

ls_Access_token = loo_JResp.StringOf("access_token")
ls_Token_type = loo_JResp.StringOf("token_type")
li_Expires_in = loo_JResp.IntOf("expires_in")
ls_Scope = loo_JResp.StringOf("scope")


destroy loo_Http
destroy loo_Req
destroy loo_Resp
destroy loo_SbResponseBody
destroy loo_JResp