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) Refresh WiX Access TokenRequest a new access token each time you call a WiX API. Use the refresh token together with your secret key, to request refresh tokens For more information, see https://dev.wix.com/api/rest/authorization/oauth-2/refresh-an-access-token
#include <C_CkHttpW.h> #include <C_CkJsonObjectW.h> #include <C_CkHttpResponseW.h> #include <C_CkStringBuilderW.h> void ChilkatSample(void) { HCkHttpW http; HCkJsonObjectW jsonToken; BOOL success; const wchar_t *refreshToken; HCkJsonObjectW json; HCkHttpResponseW resp; HCkStringBuilderW sbResponseBody; HCkJsonObjectW jResp; int respStatusCode; const wchar_t *refresh_token; const wchar_t *access_token; // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. http = CkHttpW_Create(); // Implements the following CURL command: // curl -X POST \ // https://www.wix.com/oauth/access \ // -H 'Content-Type: application/json' \ // -d '{ // "grant_type": "refresh_token", // "client_id": <CLIENT_ID>, // "client_secret": <CLIENT_SECRET>, // "refresh_token": <REFRESH_TOKEN> // }' // It is assumed we previously obtained an OAuth2 access token. // This example loads the JSON access token file // saved by this example: Get WiX OAuth2 Access Token jsonToken = CkJsonObjectW_Create(); success = CkJsonObjectW_LoadFile(jsonToken,L"qa_data/tokens/wix.json"); if (success != TRUE) { wprintf(L"Failed to load square.json\n"); CkHttpW_Dispose(http); CkJsonObjectW_Dispose(jsonToken); return; } // Get the "refresh_token" refreshToken = CkJsonObjectW_stringOf(jsonToken,L"refresh_token"); // The following JSON is sent in the request body. // { // "grant_type": "refresh_token", // "client_id": <APP_ID>, // "client_secret": <APP_SECRET>, // "refresh_token": <REFRESH_TOKEN> // } json = CkJsonObjectW_Create(); CkJsonObjectW_UpdateString(json,L"grant_type",L"refresh_token"); CkJsonObjectW_UpdateString(json,L"client_id",L"CLIENT_ID"); CkJsonObjectW_UpdateString(json,L"client_secret",L"CLIENT_SECRET"); CkJsonObjectW_UpdateString(json,L"refresh_token",refreshToken); CkHttpW_SetRequestHeader(http,L"Content-Type",L"application/json"); resp = CkHttpW_PostJson3(http,L"https://www.wix.com/oauth/access",L"application/json",json); if (CkHttpW_getLastMethodSuccess(http) == FALSE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkHttpW_Dispose(http); CkJsonObjectW_Dispose(jsonToken); CkJsonObjectW_Dispose(json); return; } sbResponseBody = CkStringBuilderW_Create(); CkHttpResponseW_GetBodySb(resp,sbResponseBody); jResp = CkJsonObjectW_Create(); CkJsonObjectW_LoadSb(jResp,sbResponseBody); CkJsonObjectW_putEmitCompact(jResp,FALSE); wprintf(L"Response Body:\n"); wprintf(L"%s\n",CkJsonObjectW_emit(jResp)); respStatusCode = CkHttpResponseW_getStatusCode(resp); wprintf(L"Response Status Code = %d\n",respStatusCode); if (respStatusCode >= 400) { wprintf(L"Response Header:\n"); wprintf(L"%s\n",CkHttpResponseW_header(resp)); wprintf(L"Failed.\n"); CkHttpResponseW_Dispose(resp); CkHttpW_Dispose(http); CkJsonObjectW_Dispose(jsonToken); CkJsonObjectW_Dispose(json); CkStringBuilderW_Dispose(sbResponseBody); CkJsonObjectW_Dispose(jResp); return; } CkHttpResponseW_Dispose(resp); // Sample JSON response: // { // "refresh_token": "OAUTH2.eyJraWQ ... vnB4cQ", // "access_token": "OAUTH2.eyJra ... la18lrw" // } refresh_token = CkJsonObjectW_stringOf(jResp,L"refresh_token"); access_token = CkJsonObjectW_stringOf(jResp,L"access_token"); // Save the new JSON access token response to a file. CkStringBuilderW_WriteFile(sbResponseBody,L"qa_data/tokens/wix.json",L"utf-8",FALSE); CkHttpW_Dispose(http); CkJsonObjectW_Dispose(jsonToken); CkJsonObjectW_Dispose(json); CkStringBuilderW_Dispose(sbResponseBody); CkJsonObjectW_Dispose(jResp); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.