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
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
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

 

 

 

(DataFlex) Get an Azure AD Access Token

Demonstrates how to obtain an Azure AD access token for authentication using a client ID, client secret, and tenant ID.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-9.5.0-win32.pkg

Procedure Test
    Variant vSocket
    Handle hoSocket
    Boolean iSuccess
    Handle hoAzureAD
    Handle hoJson
    Handle hoFac
    Handle hoSa
    Variant vSbIdToken
    Handle hoSbIdToken
    Handle hoJsonIdToken
    String sAud
    String sIss
    Integer iIat
    Integer iNbf
    Integer iExp
    String sAio
    String sApp_displayname
    String sAppid
    String sAppidacr
    String sIdp
    String sOid
    String sSid
    String sS_sub
    String sTid
    String sUti
    String sVer
    String sTemp1

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

    Get Create (RefClass(cComChilkatSocket)) To hoSocket
    If (Not(IsComObjectCreated(hoSocket))) Begin
        Send CreateComObject of hoSocket
    End

    // Make a TLS connection to login.microsoftonline.com, waiting at most 5000 milliseconds.
    Get ComConnect Of hoSocket "login.microsoftonline.com" 443 True 5000 To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoSocket To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Create an Azure AD auth object, and provide the required information for authorization.
    Get Create (RefClass(cComChilkatAuthAzureAD)) To hoAzureAD
    If (Not(IsComObjectCreated(hoAzureAD))) Begin
        Send CreateComObject of hoAzureAD
    End
    Set ComClientId Of hoAzureAD To "AZURE_AD_CLIENT_ID"
    Set ComClientSecret Of hoAzureAD To "AZURE_AD_CLIENT_SECRET"
    Set ComTenantId Of hoAzureAD To "AZURE_TENANT_ID"

    Set ComResource Of hoAzureAD To "https://outlook.office365.com/"

    // Retrieve the access token using the TLS connection to login.microsoftonline.com
    Get pvComObject of hoSocket to vSocket
    Get ComObtainAccessToken Of hoAzureAD vSocket To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoSocket To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Show the access token, and then save it to a JSON file
    // for future use (such as with a REST method call).
    Get ComAccessToken Of hoAzureAD To sTemp1
    Showln "Azure AD Access Token = " sTemp1

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get ComAccessToken Of hoAzureAD To sTemp1
    Get ComAppendString Of hoJson "accessToken" sTemp1 To iSuccess

    // Save our access token to a file.  It will be used in subsequent REST API calls.
    Get Create (RefClass(cComCkFileAccess)) To hoFac
    If (Not(IsComObjectCreated(hoFac))) Begin
        Send CreateComObject of hoFac
    End
    Get ComEmit Of hoJson To sTemp1
    Get ComWriteEntireTextFile Of hoFac "qa_data/tokens/azureAD_office365.json" sTemp1 "utf-8" False To iSuccess

    // Let's get the id information out of the access token.
    // Our JSON looks like this:
    // {"accessToken":"eyJ0eXAiO---TdjT3RjIn0.eyJhdWQiOiJo---jEuMCJ9.CIx0sUT8s---KvzKKUw"}

    // I used "---" instead of "..." to indicate a large quantity of omitted chars.
    // The accessToken is a long string composed of 3 base64 strings concatenated with "." chars.
    // The 1st part is the JOSE header.  If you decode from base64, you'll get the JSON JOSE header.
    // The 2nd part is the id token.  When decoded this is the JSON that contains information about the authenticated application.
    // The 3rd part is binary and does not decode to JSON.

    // Let's get the information from the 2nd part (the id token)
    Get Create (RefClass(cComCkStringArray)) To hoSa
    If (Not(IsComObjectCreated(hoSa))) Begin
        Send CreateComObject of hoSa
    End
    Get ComStringOf Of hoJson "accessToken" To sTemp1
    Send ComSplitAndAppend To hoSa sTemp1 "."

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbIdToken
    If (Not(IsComObjectCreated(hoSbIdToken))) Begin
        Send CreateComObject of hoSbIdToken
    End
    // The 2nd string is at index 1.
    Get ComGetString Of hoSa 1 To sTemp1
    Get ComAppend Of hoSbIdToken sTemp1 To iSuccess
    Get ComDecode Of hoSbIdToken "base64" "utf-8" To iSuccess

    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonIdToken
    If (Not(IsComObjectCreated(hoJsonIdToken))) Begin
        Send CreateComObject of hoJsonIdToken
    End
    Get pvComObject of hoSbIdToken to vSbIdToken
    Get ComLoadSb Of hoJsonIdToken vSbIdToken To iSuccess
    Set ComEmitCompact Of hoJsonIdToken To False
    Get ComEmit Of hoJsonIdToken To sTemp1
    Showln sTemp1

    // We have something like this:
    // {
    //   "aud": "https://outlook.office365.com/",
    //   "iss": "https://sts.windows.net/6e8ddd66-68d1-43b0-af5c-e31b4b7dd5cd/",
    //   "iat": 1586350465,
    //   "nbf": 1586350465,
    //   "exp": 1586354365,
    //   "aio": "42dgYNjyOtX8ZZB7JLfMFJGeKUmvAA==",
    //   "app_displayname": "ChilkatTest",
    //   "appid": "f125d695-c50e-456e-a578-a486f06d1213",
    //   "appidacr": "1",
    //   "idp": "https://sts.windows.net/6d8ddd66-68d1-43b0-af5c-e31b4b7dd5cd/",
    //   "oid": "7545a2fd-3f0d-48a4-9c58-a1a5700a24b1",
    //   "sid": "ab981252-9378-4d0c-964b-eb2e1451138f",
    //   "sub": "7546a2fc-3f0d-48a4-9c58-a1a5700a24b1",
    //   "tid": "6d8ddd66-68d1-43b0-af5c-e31b4b7dd5cd",
    //   "uti": "huIJBAa1tvGpczwV5S1BAA",
    //   "ver": "1.0"
    // }

    // Get the desired information from the JSON:
    Get ComStringOf Of hoJsonIdToken "aud" To sAud
    Get ComStringOf Of hoJsonIdToken "iss" To sIss
    Get ComIntOf Of hoJsonIdToken "iat" To iIat
    Get ComIntOf Of hoJsonIdToken "nbf" To iNbf
    Get ComIntOf Of hoJsonIdToken "exp" To iExp
    Get ComStringOf Of hoJsonIdToken "aio" To sAio
    Get ComStringOf Of hoJsonIdToken "app_displayname" To sApp_displayname
    Get ComStringOf Of hoJsonIdToken "appid" To sAppid
    Get ComStringOf Of hoJsonIdToken "appidacr" To sAppidacr
    Get ComStringOf Of hoJsonIdToken "idp" To sIdp
    Get ComStringOf Of hoJsonIdToken "oid" To sOid
    Get ComStringOf Of hoJsonIdToken "sid" To sSid
    Get ComStringOf Of hoJsonIdToken "sub" To sS_sub
    Get ComStringOf Of hoJsonIdToken "tid" To sTid
    Get ComStringOf Of hoJsonIdToken "uti" To sUti
    Get ComStringOf Of hoJsonIdToken "ver" To sVer


End_Procedure

 

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