Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PowerBuilder) Get an Azure AD Access TokenDemonstrates how to obtain an Azure AD access token for authentication using a client ID, client secret, and tenant ID.
integer li_rc oleobject loo_Socket integer li_Success oleobject loo_AzureAD oleobject loo_Json oleobject loo_Fac oleobject loo_Sa oleobject loo_SbIdToken oleobject loo_JsonIdToken string ls_Aud string ls_Iss integer li_Iat integer li_Nbf integer li_Exp string ls_Aio string ls_App_displayname string ls_Appid string ls_Appidacr string ls_Idp string ls_Oid string ls_Sid string ls_S_sub string ls_Tid string ls_Uti string ls_Ver // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. loo_Socket = create oleobject li_rc = loo_Socket.ConnectToNewObject("Chilkat_9_5_0.Socket") if li_rc < 0 then destroy loo_Socket MessageBox("Error","Connecting to COM object failed") return end if // Make a TLS connection to login.microsoftonline.com, waiting at most 5000 milliseconds. li_Success = loo_Socket.Connect("login.microsoftonline.com",443,1,5000) if li_Success <> 1 then Write-Debug loo_Socket.LastErrorText destroy loo_Socket return end if // Create an Azure AD auth object, and provide the required information for authorization. loo_AzureAD = create oleobject li_rc = loo_AzureAD.ConnectToNewObject("Chilkat_9_5_0.AuthAzureAD") loo_AzureAD.ClientId = "AZURE_AD_CLIENT_ID" loo_AzureAD.ClientSecret = "AZURE_AD_CLIENT_SECRET" loo_AzureAD.TenantId = "AZURE_TENANT_ID" loo_AzureAD.Resource = "https://outlook.office365.com/" // Retrieve the access token using the TLS connection to login.microsoftonline.com li_Success = loo_AzureAD.ObtainAccessToken(loo_Socket) if li_Success <> 1 then Write-Debug loo_Socket.LastErrorText destroy loo_Socket destroy loo_AzureAD return end if // Show the access token, and then save it to a JSON file // for future use (such as with a REST method call). Write-Debug "Azure AD Access Token = " + loo_AzureAD.AccessToken loo_Json = create oleobject li_rc = loo_Json.ConnectToNewObject("Chilkat_9_5_0.JsonObject") loo_Json.AppendString("accessToken",loo_AzureAD.AccessToken) // Save our access token to a file. It will be used in subsequent REST API calls. loo_Fac = create oleobject li_rc = loo_Fac.ConnectToNewObject("Chilkat_9_5_0.FileAccess") loo_Fac.WriteEntireTextFile("qa_data/tokens/azureAD_office365.json",loo_Json.Emit(),"utf-8",0) // 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) loo_Sa = create oleobject li_rc = loo_Sa.ConnectToNewObject("Chilkat_9_5_0.StringArray") loo_Sa.SplitAndAppend(loo_Json.StringOf("accessToken"),".") loo_SbIdToken = create oleobject li_rc = loo_SbIdToken.ConnectToNewObject("Chilkat_9_5_0.StringBuilder") // The 2nd string is at index 1. loo_SbIdToken.Append(loo_Sa.GetString(1)) loo_SbIdToken.Decode("base64","utf-8") loo_JsonIdToken = create oleobject li_rc = loo_JsonIdToken.ConnectToNewObject("Chilkat_9_5_0.JsonObject") loo_JsonIdToken.LoadSb(loo_SbIdToken) loo_JsonIdToken.EmitCompact = 0 Write-Debug loo_JsonIdToken.Emit() // 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: ls_Aud = loo_JsonIdToken.StringOf("aud") ls_Iss = loo_JsonIdToken.StringOf("iss") li_Iat = loo_JsonIdToken.IntOf("iat") li_Nbf = loo_JsonIdToken.IntOf("nbf") li_Exp = loo_JsonIdToken.IntOf("exp") ls_Aio = loo_JsonIdToken.StringOf("aio") ls_App_displayname = loo_JsonIdToken.StringOf("app_displayname") ls_Appid = loo_JsonIdToken.StringOf("appid") ls_Appidacr = loo_JsonIdToken.StringOf("appidacr") ls_Idp = loo_JsonIdToken.StringOf("idp") ls_Oid = loo_JsonIdToken.StringOf("oid") ls_Sid = loo_JsonIdToken.StringOf("sid") ls_S_sub = loo_JsonIdToken.StringOf("sub") ls_Tid = loo_JsonIdToken.StringOf("tid") ls_Uti = loo_JsonIdToken.StringOf("uti") ls_Ver = loo_JsonIdToken.StringOf("ver") destroy loo_Socket destroy loo_AzureAD destroy loo_Json destroy loo_Fac destroy loo_Sa destroy loo_SbIdToken destroy loo_JsonIdToken |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.