|  | 
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) Group: Refresh OAuth2 Access TokenRefreshes an expired or non-expired OAuth2 access token for the Microsoft Group REST API. Note: This example requires Chilkat v11.0.0 or greater. 
 #include <C_CkJsonObjectW.h> #include <C_CkHttpRequestW.h> #include <C_CkHttpW.h> #include <C_CkHttpResponseW.h> #include <C_CkFileAccessW.h> void ChilkatSample(void) { BOOL success; HCkJsonObjectW json; HCkHttpRequestW req; HCkHttpW http; HCkHttpResponseW resp; HCkFileAccessW fac; success = FALSE; // 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(); CkHttpRequestW_putHttpVerb(req,L"POST"); CkHttpRequestW_putContentType(req,L"application/x-www-form-urlencoded"); resp = CkHttpResponseW_Create(); success = CkHttpW_HttpReq(http,L"https://login.microsoftonline.com/common/oauth2/v2.0/token",req,resp); if (success == FALSE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkJsonObjectW_Dispose(json); CkHttpRequestW_Dispose(req); CkHttpW_Dispose(http); CkHttpResponseW_Dispose(resp); 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); CkHttpResponseW_Dispose(resp); return; } // 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); CkHttpResponseW_Dispose(resp); CkFileAccessW_Dispose(fac); } | ||||||
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.