Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode 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
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
MS Storage Providers
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
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(DataFlex) OAuth2 Token using IdentityServer4 with Client Credentials

Demonstrates how to get an OAuth2 access token using the client credential flow with IdentityServer4.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-win32.pkg

Procedure Test
    Handle hoHttp
    Variant vResp
    Handle hoResp
    Handle hoJson
    Boolean iSuccess
    String sTokenEndpoint
    Variant vGrantTypes
    Handle hoGrantTypes
    Integer iClientCredentialsIdx
    Variant vReq
    Handle hoReq
    String sAccessToken
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

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

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End

    // The first step is to fetch your IdentityServer4's discovery document
    // (OpenID Connect defines a discovery mechanism, called OpenID Connect Discovery, where an OpenID server publishes its metadata at a well-known URL, 
    // typically https://server.com/.well-known/openid-configuration

    Get ComQuickRequest Of hoHttp "GET" "https://localhost:5000/.well-known/openid-configuration" To vResp
    If (IsComObject(vResp)) Begin
        Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
        Set pvComObject Of hoResp To vResp
    End
    Get ComLastMethodSuccess Of hoHttp To bTemp1
    If (bTemp1 <> True) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComStatusCode Of hoResp To iTemp1
    If (iTemp1 <> 200) Begin
        Get ComStatusCode Of hoResp To iTemp1
        Showln "Received response status code " iTemp1
        Showln "Response body containing error text or JSON:"
        Get ComBodyStr Of hoResp To sTemp1
        Showln sTemp1
        Send Destroy of hoResp
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get ComBodyStr Of hoResp To sTemp1
    Get ComLoad Of hoJson sTemp1 To iSuccess
    Send Destroy of hoResp

    // We have the discovery document, which contains something like this:

    // You can use this online tool to generate parsing code from sample JSON: 
    // Generate Parsing Code from JSON

    // {
    //   "issuer": "https://localhost:5000",
    //   "jwks_uri": "https://localhost:5000/.well-known/openid-configuration/jwks",
    //   "authorization_endpoint": "https://localhost:5000/connect/authorize",
    //   "token_endpoint": "https://localhost:5000/connect/token",
    //   "userinfo_endpoint": "https://localhost:5000/connect/userinfo",
    //   "end_session_endpoint": "https://localhost:5000/connect/endsession",
    //   "check_session_iframe": "https://localhost:5000/connect/checksession",
    //   "revocation_endpoint": "https://localhost:5000/connect/revocation",
    //   "introspection_endpoint": "https://localhost:5000/connect/introspect",
    //   "frontchannel_logout_supported": true,
    //   "frontchannel_logout_session_supported": true,
    //   "backchannel_logout_supported": true,
    //   "backchannel_logout_session_supported": true,
    //   "scopes_supported": [
    //     "openid",
    //     "profile",
    //     "email",
    //     "MyCompany.profile",
    //     "MyCompany.Identity.WebApi",
    //     "MyCompany.TriHub.WebApi",
    //     "offline_access"
    //   ],
    //   "claims_supported": [
    //     "sub",
    //     "updated_at",
    //     "locale",
    //     "zoneinfo",
    //     "birthdate",
    //     "gender",
    //     "website",
    //     "profile",
    //     "preferred_username",
    //     "nickname",
    //     "middle_name",
    //     "given_name",
    //     "family_name",
    //     "name",
    //     "picture",
    //     "email_verified",
    //     "email",
    //     "userId",
    //     "groups",
    //     "fullname"
    //   ],
    //   "grant_types_supported": [
    //     "authorization_code",
    //     "client_credentials",
    //     "refresh_token",
    //     "implicit",
    //     "password"
    //   ],
    //   "response_types_supported": [
    //     "code",
    //     "token",
    //     "id_token",
    //     "id_token token",
    //     "code id_token",
    //     "code token",
    //     "code id_token token"
    //   ],
    //   "response_modes_supported": [
    //     "form_post",
    //     "query",
    //     "fragment"
    //   ],
    //   "token_endpoint_auth_methods_supported": [
    //     "client_secret_basic",
    //     "client_secret_post"
    //   ],
    //   "subject_types_supported": [
    //     "public"
    //   ],
    //   "id_token_signing_alg_values_supported": [
    //     "RS256"
    //   ],
    //   "code_challenge_methods_supported": [
    //     "plain",
    //     "S256"
    //   ]
    // }
    // 

    // The next steps are to (1) get the token_endpoint,
    // and (2) verify that the client_credentials grant type is supported.

    Get ComStringOf Of hoJson "token_endpoint" To sTokenEndpoint

    Get ComArrayOf Of hoJson "grant_types_supported" To vGrantTypes
    If (IsComObject(vGrantTypes)) Begin
        Get Create (RefClass(cComChilkatJsonArray)) To hoGrantTypes
        Set pvComObject Of hoGrantTypes To vGrantTypes
    End
    Get ComFindString Of hoGrantTypes "client_credentials" True To iClientCredentialsIdx
    Send Destroy of hoGrantTypes

    // If clientCredentialsIdx is less then zero (-1) then the "client_credentials" string was not found.
    If (iClientCredentialsIdx < 0) Begin
        Showln "The client credentials grant type is not supported."
        Procedure_Return
    End

    // Request the access token using our Client ID and Client Secret.
    // We're going to duplicate this CURL statement:

    // curl --request POST \
    //   --url '<tokenEndpoint>' \
    //   --header 'content-type: application/x-www-form-urlencoded' \
    //   --data 'grant_type=client_credentials&client_id=CLIENT_ID&client_secret=CLIENT_SECRET'

    Get Create (RefClass(cComChilkatHttpRequest)) To hoReq
    If (Not(IsComObjectCreated(hoReq))) Begin
        Send CreateComObject of hoReq
    End
    Set ComHttpVerb Of hoReq To "POST"
    Send ComAddParam To hoReq "grant_type" "client_credentials"
    Send ComAddParam To hoReq "client_id" "CLIENT_ID"
    Send ComAddParam To hoReq "client_secret" "CLIENT_SECRET"

    Get pvComObject of hoReq to vReq
    Get ComPostUrlEncoded Of hoHttp sTokenEndpoint vReq To vResp
    If (IsComObject(vResp)) Begin
        Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
        Set pvComObject Of hoResp To vResp
    End
    Get ComLastMethodSuccess Of hoHttp To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Make sure we got a 200 response status code, otherwise it's an error.
    Get ComStatusCode Of hoResp To iTemp1
    If (iTemp1 <> 200) Begin
        Showln "POST to token endpoint failed."
        Get ComStatusCode Of hoResp To iTemp1
        Showln "Received response status code " iTemp1
        Showln "Response body containing error text or JSON:"
        Get ComBodyStr Of hoResp To sTemp1
        Showln sTemp1
        Send Destroy of hoResp
        Procedure_Return
    End

    Get ComBodyStr Of hoResp To sTemp1
    Get ComLoad Of hoJson sTemp1 To iSuccess
    Send Destroy of hoResp

    // Our JSON response should contain this:
    // {
    //   "access_token":"eyJz93a...k4laUWw",
    //   "token_type":"Bearer",
    //   "expires_in":86400
    // }

    // Get the access token:
    Get ComStringOf Of hoJson "access_token" To sAccessToken

    // The access token is what gets added to "Authorization: Bearer <access_token>"
    // for the subsequent REST API calls..


End_Procedure

 

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