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
(Unicode C) 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.
#include <C_CkSocketW.h> #include <C_CkAuthAzureADW.h> #include <C_CkJsonObjectW.h> #include <C_CkFileAccessW.h> #include <C_CkStringArrayW.h> #include <C_CkStringBuilderW.h> void ChilkatSample(void) { HCkSocketW socket; BOOL success; HCkAuthAzureADW azureAD; HCkJsonObjectW json; HCkFileAccessW fac; HCkStringArrayW sa; HCkStringBuilderW sbIdToken; HCkJsonObjectW jsonIdToken; const wchar_t *aud; const wchar_t *iss; int iat; int nbf; int exp; const wchar_t *aio; const wchar_t *app_displayname; const wchar_t *appid; const wchar_t *appidacr; const wchar_t *idp; const wchar_t *oid; const wchar_t *sid; const wchar_t *s_sub; const wchar_t *tid; const wchar_t *uti; const wchar_t *ver; // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. socket = CkSocketW_Create(); // Make a TLS connection to login.microsoftonline.com, waiting at most 5000 milliseconds. success = CkSocketW_Connect(socket,L"login.microsoftonline.com",443,TRUE,5000); if (success != TRUE) { wprintf(L"%s\n",CkSocketW_lastErrorText(socket)); CkSocketW_Dispose(socket); return; } // Create an Azure AD auth object, and provide the required information for authorization. azureAD = CkAuthAzureADW_Create(); CkAuthAzureADW_putClientId(azureAD,L"AZURE_AD_CLIENT_ID"); CkAuthAzureADW_putClientSecret(azureAD,L"AZURE_AD_CLIENT_SECRET"); CkAuthAzureADW_putTenantId(azureAD,L"AZURE_TENANT_ID"); CkAuthAzureADW_putResource(azureAD,L"https://outlook.office365.com/"); // Retrieve the access token using the TLS connection to login.microsoftonline.com success = CkAuthAzureADW_ObtainAccessToken(azureAD,socket); if (success != TRUE) { wprintf(L"%s\n",CkSocketW_lastErrorText(socket)); CkSocketW_Dispose(socket); CkAuthAzureADW_Dispose(azureAD); return; } // Show the access token, and then save it to a JSON file // for future use (such as with a REST method call). wprintf(L"Azure AD Access Token = %s\n",CkAuthAzureADW_accessToken(azureAD)); json = CkJsonObjectW_Create(); CkJsonObjectW_AppendString(json,L"accessToken",CkAuthAzureADW_accessToken(azureAD)); // Save our access token to a file. It will be used in subsequent REST API calls. fac = CkFileAccessW_Create(); CkFileAccessW_WriteEntireTextFile(fac,L"qa_data/tokens/azureAD_office365.json",CkJsonObjectW_emit(json),L"utf-8",FALSE); // 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 = CkStringArrayW_Create(); CkStringArrayW_SplitAndAppend(sa,CkJsonObjectW_stringOf(json,L"accessToken"),L"."); sbIdToken = CkStringBuilderW_Create(); // The 2nd string is at index 1. CkStringBuilderW_Append(sbIdToken,CkStringArrayW_getString(sa,1)); CkStringBuilderW_Decode(sbIdToken,L"base64",L"utf-8"); jsonIdToken = CkJsonObjectW_Create(); CkJsonObjectW_LoadSb(jsonIdToken,sbIdToken); CkJsonObjectW_putEmitCompact(jsonIdToken,FALSE); wprintf(L"%s\n",CkJsonObjectW_emit(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" // } // Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat. // See this example explaining how this memory should be used: const char * functions. // Get the desired information from the JSON: aud = CkJsonObjectW_stringOf(jsonIdToken,L"aud"); iss = CkJsonObjectW_stringOf(jsonIdToken,L"iss"); iat = CkJsonObjectW_IntOf(jsonIdToken,L"iat"); nbf = CkJsonObjectW_IntOf(jsonIdToken,L"nbf"); exp = CkJsonObjectW_IntOf(jsonIdToken,L"exp"); aio = CkJsonObjectW_stringOf(jsonIdToken,L"aio"); app_displayname = CkJsonObjectW_stringOf(jsonIdToken,L"app_displayname"); appid = CkJsonObjectW_stringOf(jsonIdToken,L"appid"); appidacr = CkJsonObjectW_stringOf(jsonIdToken,L"appidacr"); idp = CkJsonObjectW_stringOf(jsonIdToken,L"idp"); oid = CkJsonObjectW_stringOf(jsonIdToken,L"oid"); sid = CkJsonObjectW_stringOf(jsonIdToken,L"sid"); s_sub = CkJsonObjectW_stringOf(jsonIdToken,L"sub"); tid = CkJsonObjectW_stringOf(jsonIdToken,L"tid"); uti = CkJsonObjectW_stringOf(jsonIdToken,L"uti"); ver = CkJsonObjectW_stringOf(jsonIdToken,L"ver"); CkSocketW_Dispose(socket); CkAuthAzureADW_Dispose(azureAD); CkJsonObjectW_Dispose(json); CkFileAccessW_Dispose(fac); CkStringArrayW_Dispose(sa); CkStringBuilderW_Dispose(sbIdToken); CkJsonObjectW_Dispose(jsonIdToken); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.