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
(PureBasic) 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.
IncludeFile "CkJsonObject.pb" IncludeFile "CkSocket.pb" IncludeFile "CkFileAccess.pb" IncludeFile "CkAuthAzureAD.pb" IncludeFile "CkStringArray.pb" IncludeFile "CkStringBuilder.pb" Procedure ChilkatExample() ; This example requires the Chilkat API to have been previously unlocked. ; See Global Unlock Sample for sample code. socket.i = CkSocket::ckCreate() If socket.i = 0 Debug "Failed to create object." ProcedureReturn EndIf ; Make a TLS connection to login.microsoftonline.com, waiting at most 5000 milliseconds. success.i = CkSocket::ckConnect(socket,"login.microsoftonline.com",443,1,5000) If success <> 1 Debug CkSocket::ckLastErrorText(socket) CkSocket::ckDispose(socket) ProcedureReturn EndIf ; Create an Azure AD auth object, and provide the required information for authorization. azureAD.i = CkAuthAzureAD::ckCreate() If azureAD.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkAuthAzureAD::setCkClientId(azureAD, "AZURE_AD_CLIENT_ID") CkAuthAzureAD::setCkClientSecret(azureAD, "AZURE_AD_CLIENT_SECRET") CkAuthAzureAD::setCkTenantId(azureAD, "AZURE_TENANT_ID") CkAuthAzureAD::setCkResource(azureAD, "https://outlook.office365.com/") ; Retrieve the access token using the TLS connection to login.microsoftonline.com success = CkAuthAzureAD::ckObtainAccessToken(azureAD,socket) If success <> 1 Debug CkSocket::ckLastErrorText(socket) CkSocket::ckDispose(socket) CkAuthAzureAD::ckDispose(azureAD) ProcedureReturn EndIf ; Show the access token, and then save it to a JSON file ; for future use (such as with a REST method call). Debug "Azure AD Access Token = " + CkAuthAzureAD::ckAccessToken(azureAD) json.i = CkJsonObject::ckCreate() If json.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkJsonObject::ckAppendString(json,"accessToken",CkAuthAzureAD::ckAccessToken(azureAD)) ; Save our access token to a file. It will be used in subsequent REST API calls. fac.i = CkFileAccess::ckCreate() If fac.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkFileAccess::ckWriteEntireTextFile(fac,"qa_data/tokens/azureAD_office365.json",CkJsonObject::ckEmit(json),"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) sa.i = CkStringArray::ckCreate() If sa.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkStringArray::ckSplitAndAppend(sa,CkJsonObject::ckStringOf(json,"accessToken"),".") sbIdToken.i = CkStringBuilder::ckCreate() If sbIdToken.i = 0 Debug "Failed to create object." ProcedureReturn EndIf ; The 2nd string is at index 1. CkStringBuilder::ckAppend(sbIdToken,CkStringArray::ckGetString(sa,1)) CkStringBuilder::ckDecode(sbIdToken,"base64","utf-8") jsonIdToken.i = CkJsonObject::ckCreate() If jsonIdToken.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkJsonObject::ckLoadSb(jsonIdToken,sbIdToken) CkJsonObject::setCkEmitCompact(jsonIdToken, 0) Debug CkJsonObject::ckEmit(jsonIdToken) ; 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: aud.s = CkJsonObject::ckStringOf(jsonIdToken,"aud") iss.s = CkJsonObject::ckStringOf(jsonIdToken,"iss") iat.i = CkJsonObject::ckIntOf(jsonIdToken,"iat") nbf.i = CkJsonObject::ckIntOf(jsonIdToken,"nbf") exp.i = CkJsonObject::ckIntOf(jsonIdToken,"exp") aio.s = CkJsonObject::ckStringOf(jsonIdToken,"aio") app_displayname.s = CkJsonObject::ckStringOf(jsonIdToken,"app_displayname") appid.s = CkJsonObject::ckStringOf(jsonIdToken,"appid") appidacr.s = CkJsonObject::ckStringOf(jsonIdToken,"appidacr") idp.s = CkJsonObject::ckStringOf(jsonIdToken,"idp") oid.s = CkJsonObject::ckStringOf(jsonIdToken,"oid") sid.s = CkJsonObject::ckStringOf(jsonIdToken,"sid") s_sub.s = CkJsonObject::ckStringOf(jsonIdToken,"sub") tid.s = CkJsonObject::ckStringOf(jsonIdToken,"tid") uti.s = CkJsonObject::ckStringOf(jsonIdToken,"uti") ver.s = CkJsonObject::ckStringOf(jsonIdToken,"ver") CkSocket::ckDispose(socket) CkAuthAzureAD::ckDispose(azureAD) CkJsonObject::ckDispose(json) CkFileAccess::ckDispose(fac) CkStringArray::ckDispose(sa) CkStringBuilder::ckDispose(sbIdToken) CkJsonObject::ckDispose(jsonIdToken) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.