![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(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.
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.