Chilkat Examples

ChilkatHOMEAndroid™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi DLLGoJavaNode.jsObjective-CPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwiftTclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

DataFlex Examples
Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Cert Store
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
X
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(DataFlex) 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.

Note: This example requires Chilkat v9.5.0.97 or greater.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-win32.pkg

Procedure Test
    Handle hoJsonToken
    Boolean iSuccess
    Handle hoOauth2
    Variant vSbJson
    Handle hoSbJson
    String sTemp1

    // 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.
    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonToken
    If (Not(IsComObjectCreated(hoJsonToken))) Begin
        Send CreateComObject of hoJsonToken
    End
    Get ComLoadFile Of hoJsonToken "qa_data/tokens/myAccessToken.json" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Failed to load hmrc.json"
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatOAuth2)) To hoOauth2
    If (Not(IsComObjectCreated(hoOauth2))) Begin
        Send CreateComObject of hoOauth2
    End

    Set ComTokenEndpoint Of hoOauth2 To "https://api.example.com/oauth/token"

    // Replace these with actual values.
    Set ComClientId Of hoOauth2 To "CLIENT_ID"
    Set ComClientSecret Of hoOauth2 To "CLIENT_SECRET"

    // Add the optional refresh query param.
    // Call AddRefreshQueryParam multiple times to add additional params.
    Get ComAddRefreshQueryParam Of hoOauth2 "audience" "https://api.example.com" To iSuccess

    // Provide the existing refresh token from the JSON.
    Get ComStringOf Of hoJsonToken "refresh_token" To sTemp1
    Set ComRefreshToken Of hoOauth2 To sTemp1

    // Send the HTTP POST to refresh the access token..
    Get ComRefreshAccessToken Of hoOauth2 To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoOauth2 To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Load the access token response into the json object 
    Get ComAccessTokenResponse Of hoOauth2 To sTemp1
    Get ComLoad Of hoJsonToken sTemp1 To iSuccess

    // 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.
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbJson
    If (Not(IsComObjectCreated(hoSbJson))) Begin
        Send CreateComObject of hoSbJson
    End
    Set ComEmitCompact Of hoJsonToken To False
    Get pvComObject of hoSbJson to vSbJson
    Get ComEmitSb Of hoJsonToken vSbJson To iSuccess
    Get ComWriteFile Of hoSbJson "qa_data/tokens/myAccessToken.json" "utf-8" False To iSuccess

    Showln "OAuth2 access token refreshed!"
    Get ComAccessToken Of hoOauth2 To sTemp1
    Showln "New Access Token = " sTemp1


End_Procedure

 

© 2000-2025 Chilkat Software, Inc. All Rights Reserved.