|  | 
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
| (Unicode C) Refresh Expiring OAuth2 Access Token for Azure Registered AppSee more OAuth2 ExamplesShows how to renew an Azure App's access token using the refresh token when it's near expiration.
 #include <C_CkJsonObjectW.h> #include <C_CkDateTimeW.h> #include <C_CkOAuth2W.h> #include <C_CkFileAccessW.h> void ChilkatSample(void) { HCkJsonObjectW json; BOOL success; HCkDateTimeW dtExpire; HCkOAuth2W oauth2; HCkFileAccessW fac; // We previously obtained an access token and saved the JSON to a file using this example: // Get OAuth2 Access Token for Azure Registered App // This example will examine the JSON and expiration date, and if near expiration will // refresh the access token. json = CkJsonObjectW_Create(); success = CkJsonObjectW_LoadFile(json,L"qa_data/tokens/_myAzureApp.json"); if (success != TRUE) { wprintf(L"Failed to load the access token.\n"); CkJsonObjectW_Dispose(json); return; } // The contents of the JSON look like this: // { // "token_type": "Bearer", // "scope": "User.Read Mail.ReadWrite Mail.Send", // "expires_in": 3600, // "ext_expires_in": 0, // "access_token": "EwBAA8l6B...", // "refresh_token": "MCRMdbe6Cd...", // "id_token": "eyJ0eXAiOiJ...", // "expires_on": "1494112119" // } // The "expires_on" value is a Unix time. dtExpire = CkDateTimeW_Create(); CkDateTimeW_SetFromUnixTime(dtExpire,FALSE,CkJsonObjectW_IntOf(json,L"expires_on")); // If this date/time expires within 10 minutes of the current system time, refresh the token. if (CkDateTimeW_ExpiresWithin(dtExpire,10,L"minutes") != TRUE) { wprintf(L"No need to refresh, the access token won't expire within the next 10 minutes.\n"); CkJsonObjectW_Dispose(json); CkDateTimeW_Dispose(dtExpire); return; } // OK, we need to refresh the access token.. oauth2 = CkOAuth2W_Create(); // Note: The endpoint depends on the Azure App Registration. // See How to Choose the Correct Endpoints for your Azure App Registration CkOAuth2W_putTokenEndpoint(oauth2,L"https://login.microsoftonline.com/common/oauth2/v2.0/token"); // Use your client ID. CkOAuth2W_putClientId(oauth2,L"CLIENT_ID"); // Get the existing refresh token. CkOAuth2W_putRefreshToken(oauth2,CkJsonObjectW_stringOf(json,L"refresh_token")); // Send the HTTP POST to refresh the access token. success = CkOAuth2W_RefreshAccessToken(oauth2); if (success == FALSE) { wprintf(L"%s\n",CkOAuth2W_lastErrorText(oauth2)); CkJsonObjectW_Dispose(json); CkDateTimeW_Dispose(dtExpire); CkOAuth2W_Dispose(oauth2); return; } wprintf(L"OAuth2 authorization granted!\n"); wprintf(L"Access Token = %s\n",CkOAuth2W_accessToken(oauth2)); // Get the full JSON response: CkJsonObjectW_Load(json,CkOAuth2W_accessTokenResponse(oauth2)); CkJsonObjectW_putEmitCompact(json,FALSE); // If an "expires_on" member does not exist, then add the JSON member by // getting the current system date/time and adding the "expires_in" seconds. // This way we'll know when the token expires. if (CkJsonObjectW_HasMember(json,L"expires_on") != TRUE) { CkDateTimeW_SetFromCurrentSystemTime(dtExpire); CkDateTimeW_AddSeconds(dtExpire,CkJsonObjectW_IntOf(json,L"expires_in")); CkJsonObjectW_AppendString(json,L"expires_on",CkDateTimeW_getAsUnixTimeStr(dtExpire,FALSE)); } wprintf(L"%s\n",CkJsonObjectW_emit(json)); // Save the new access token JSON to a file for future requests. fac = CkFileAccessW_Create(); CkFileAccessW_WriteEntireTextFile(fac,L"qa_data/tokens/_myAzureApp.json",CkJsonObjectW_emit(json),L"utf-8",FALSE); CkJsonObjectW_Dispose(json); CkDateTimeW_Dispose(dtExpire); CkOAuth2W_Dispose(oauth2); CkFileAccessW_Dispose(fac); } | ||||||
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.