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) Microsoft Graph -- List Users (Automatic OAuth2 Client Credentials)

Simplify OAuth2 client credentials by using Chilkat to automatically obtain the necessary access token. This example demonstrates the technique with the Microsoft Graph API's "List Users" request.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-win32.pkg

Procedure Test
    Handle hoJson
    Boolean iSuccess
    Handle hoHttp
    Variant vSbResponse
    Handle hoSbResponse
    Boolean iSuccess
    String sOdata_id
    String sDisplayName
    String sGivenName
    String sJobTitle
    String sMail
    String sMobilePhone
    String sOfficeLocation
    String sPreferredLanguage
    String sSurname
    String sUserPrincipalName
    String sId
    Integer j
    Integer iCount_j
    String sOdata_context
    Integer i
    Integer iCount_i
    String sTemp1
    Integer iTemp1

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

    // Instead of directly sending a POST request to obtain an OAuth2 access token, 
    // you can provide Chilkat with the necessary information to automatically fetch 
    // the token using the Client Credentials flow. During the first HTTP request, 
    // Chilkat will obtain the token if it doesn't have one yet, and then use it for 
    // all subsequent requests. When the token is expired or about to expire, Chilkat 
    // will automatically obtain a new one, eliminating the need for your application 
    // to manage token expiration and renewal manually. This approach also reduces the 
    // number of HTTP requests by utilizing valid tokens as long as possible.

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get ComUpdateString Of hoJson "client_secret" "CLIENT_SECRET" To iSuccess
    Get ComUpdateString Of hoJson "client_id" "CLIENT_ID" To iSuccess
    Get ComUpdateString Of hoJson "token_endpoint" "https://login.microsoftonline.com/TENANT_ID/oauth2/v2.0/token" To iSuccess
    Get ComUpdateString Of hoJson "scope" "https://graph.microsoft.com/.default" To iSuccess

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

    // Provide the information to let Chilkat automatically obtain the access token.
    Get ComEmit Of hoJson To sTemp1
    Set ComAuthToken Of hoHttp To sTemp1

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbResponse
    If (Not(IsComObjectCreated(hoSbResponse))) Begin
        Send CreateComObject of hoSbResponse
    End

    Get pvComObject of hoSbResponse to vSbResponse
    Get ComQuickGetSb Of hoHttp "https://graph.microsoft.com/v1.0/users" vSbResponse To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get pvComObject of hoSbResponse to vSbResponse
    Get ComLoadSb Of hoJson vSbResponse To iSuccess
    Set ComEmitCompact Of hoJson To False

    Get ComLastStatus Of hoHttp To iTemp1
    Showln "Status code = " iTemp1
    Get ComLastStatus Of hoHttp To iTemp1
    If (iTemp1 <> 200) Begin
        Get ComEmit Of hoJson To sTemp1
        Showln sTemp1
        Showln "Failed."
    End

    Get ComEmit Of hoJson To sTemp1
    Showln sTemp1

    // Sample output
    // {
    //   "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users",
    //   "value": [
    //     {
    //       "@odata.id": "https://graph.microsoft.com/v2/6d8ddd66-68d1-43b0-af5c-e31b4b7dd5cd/directoryObjects/fca490d8-5918-4201-8079-c5dcbeafcdc9/Microsoft.DirectoryServices.User",
    //       "businessPhones": [
    //       ],
    //       "displayName": "Joe Sample",
    //       "givenName": "Joe",
    //       "jobTitle": null,
    //       "mail": null,
    //       "mobilePhone": null,
    //       "officeLocation": null,
    //       "preferredLanguage": null,
    //       "surname": "Sample",
    //       "userPrincipalName": "admin_chilkatsoft.com#EXT#@adminchilkatsoft.onmicrosoft.com",
    //       "id": "fca490d8-5918-4201-8079-c5dcbeafcdc9"
    //     }
    //   ]
    // }

    // Use this online tool to generate parsing code from sample JSON: 
    // Generate Parsing Code from JSON

    Get ComStringOf Of hoJson '"@odata.context"' To sOdata_context
    Move 0 To i
    Get ComSizeOfArray Of hoJson "value" To iCount_i
    While (i < iCount_i)
        Set ComI Of hoJson To i
        Get ComStringOf Of hoJson 'value[i]."@odata.id"' To sOdata_id
        Get ComStringOf Of hoJson "value[i].displayName" To sDisplayName
        Get ComStringOf Of hoJson "value[i].givenName" To sGivenName
        Get ComStringOf Of hoJson "value[i].jobTitle" To sJobTitle
        Get ComStringOf Of hoJson "value[i].mail" To sMail
        Get ComStringOf Of hoJson "value[i].mobilePhone" To sMobilePhone
        Get ComStringOf Of hoJson "value[i].officeLocation" To sOfficeLocation
        Get ComStringOf Of hoJson "value[i].preferredLanguage" To sPreferredLanguage
        Get ComStringOf Of hoJson "value[i].surname" To sSurname
        Get ComStringOf Of hoJson "value[i].userPrincipalName" To sUserPrincipalName
        Get ComStringOf Of hoJson "value[i].id" To sId
        Move 0 To j
        Get ComSizeOfArray Of hoJson "value[i].businessPhones" To iCount_j
        While (j < iCount_j)
            Set ComJ Of hoJson To j
            Move (j + 1) To j
        Loop

        Move (i + 1) To i
    Loop



End_Procedure

 

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