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) Group: Refresh OAuth2 Access TokenRefreshes an expired or non-expired OAuth2 access token for the Microsoft Group REST API.
#include <C_CkJsonObjectW.h> #include <C_CkHttpRequestW.h> #include <C_CkHttpW.h> #include <C_CkHttpResponseW.h> #include <C_CkFileAccessW.h> void ChilkatSample(void) { HCkJsonObjectW json; BOOL success; HCkHttpRequestW req; HCkHttpW http; HCkHttpResponseW resp; HCkFileAccessW fac; // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. json = CkJsonObjectW_Create(); success = CkJsonObjectW_LoadFile(json,L"qa_data/tokens/msGraphGroup.json"); if (success != TRUE) { CkJsonObjectW_Dispose(json); return; } req = CkHttpRequestW_Create(); CkHttpRequestW_AddParam(req,L"grant_type",L"refresh_token"); CkHttpRequestW_AddParam(req,L"redirect_uri",L"http://localhost:3017/"); CkHttpRequestW_AddParam(req,L"client_id",L"MICROSOFT-GRAPH-CLIENT-ID"); CkHttpRequestW_AddParam(req,L"client_secret",L"MICROSOFT-GRAPH-CLIENT-SECRET"); CkHttpRequestW_AddParam(req,L"refresh_token",CkJsonObjectW_stringOf(json,L"refresh_token")); CkHttpRequestW_AddParam(req,L"scope",L"openid profile offline_access user.readwrite group.readwrite.all files.readwrite"); http = CkHttpW_Create(); resp = CkHttpW_PostUrlEncoded(http,L"https://login.microsoftonline.com/common/oauth2/v2.0/token",req); if (CkHttpW_getLastMethodSuccess(http) != TRUE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkJsonObjectW_Dispose(json); CkHttpRequestW_Dispose(req); CkHttpW_Dispose(http); return; } // Load the JSON response. CkJsonObjectW_Load(json,CkHttpResponseW_bodyStr(resp)); CkJsonObjectW_putEmitCompact(json,FALSE); // Show the JSON response. wprintf(L"%s\n",CkJsonObjectW_emit(json)); wprintf(L"Response status code: %d\n",CkHttpResponseW_getStatusCode(resp)); // If the response status code is not 200, then it's an error. if (CkHttpResponseW_getStatusCode(resp) != 200) { CkJsonObjectW_Dispose(json); CkHttpRequestW_Dispose(req); CkHttpW_Dispose(http); return; } CkHttpResponseW_Dispose(resp); // Save the refreshed access token JSON to a file for future requests. fac = CkFileAccessW_Create(); CkFileAccessW_WriteEntireTextFile(fac,L"qa_data/tokens/msGraphGroup.json",CkJsonObjectW_emit(json),L"utf-8",FALSE); wprintf(L"Success.\n"); CkJsonObjectW_Dispose(json); CkHttpRequestW_Dispose(req); CkHttpW_Dispose(http); CkFileAccessW_Dispose(fac); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.