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) AzureWebsites OAuth2 Password FlowDemonstrates how to do OAuth 2.0 password flow for azurewebsites.net.
#include <C_CkHttpW.h> #include <C_CkHttpRequestW.h> #include <C_CkHttpResponseW.h> #include <C_CkStringBuilderW.h> #include <C_CkJsonObjectW.h> void ChilkatSample(void) { HCkHttpW http; BOOL success; HCkHttpRequestW req; const wchar_t *tokenEndpoint; HCkHttpResponseW resp; HCkStringBuilderW sbResponseBody; HCkJsonObjectW jResp; int respStatusCode; HCkStringBuilderW sbXml; const wchar_t *destUrl; // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. http = CkHttpW_Create(); req = CkHttpRequestW_Create(); CkHttpRequestW_putHttpVerb(req,L"POST"); CkHttpRequestW_putPath(req,L"/token"); CkHttpRequestW_putContentType(req,L"application/x-www-form-urlencoded"); CkHttpRequestW_AddParam(req,L"grant_type",L"password"); CkHttpRequestW_AddParam(req,L"username",L"your_username"); CkHttpRequestW_AddParam(req,L"password",L"your_password"); tokenEndpoint = L"https://your_api.azurewebsites.net/token"; resp = CkHttpW_PostUrlEncoded(http,tokenEndpoint,req); if (CkHttpW_getLastMethodSuccess(http) == FALSE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkHttpW_Dispose(http); CkHttpRequestW_Dispose(req); 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)); // Sample JSON response: // { // "access_token": "NQGHn ... xTS", // "token_type": "bearer", // "expires_in": 1209599, // "userName": "your_username", // ".issued": "Mon, 27 Apr 2020 23:49:35 GMT", // ".expires": "Mon, 11 May 2020 23:49:35 GMT" // } 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); CkHttpRequestW_Dispose(req); CkStringBuilderW_Dispose(sbResponseBody); CkJsonObjectW_Dispose(jResp); return; } CkHttpResponseW_Dispose(resp); // ---------------------------------- // Use the OAuth2 token in a request. // For example... sbXml = CkStringBuilderW_Create(); success = CkStringBuilderW_LoadFile(sbXml,L"c:/someDir/someXmlFile.xml",L"utf-8"); if (success == FALSE) { wprintf(L"Failed to load the XML file.\n"); CkHttpW_Dispose(http); CkHttpRequestW_Dispose(req); CkStringBuilderW_Dispose(sbResponseBody); CkJsonObjectW_Dispose(jResp); CkStringBuilderW_Dispose(sbXml); return; } // Get the OAuth2 token and use it for authentication CkHttpW_putAuthToken(http,CkJsonObjectW_stringOf(jResp,L"token")); destUrl = L"https://your_api.azurewebsites.net/destinationUrl"; resp = CkHttpW_PostXml(http,destUrl,CkStringBuilderW_getAsString(sbXml),L"utf-8"); if (CkHttpW_getLastMethodSuccess(http) == FALSE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkHttpW_Dispose(http); CkHttpRequestW_Dispose(req); CkStringBuilderW_Dispose(sbResponseBody); CkJsonObjectW_Dispose(jResp); CkStringBuilderW_Dispose(sbXml); return; } 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); CkHttpRequestW_Dispose(req); CkStringBuilderW_Dispose(sbResponseBody); CkJsonObjectW_Dispose(jResp); CkStringBuilderW_Dispose(sbXml); return; } // Examine the response body wprintf(L"%s\n",CkHttpResponseW_bodyStr(resp)); CkHttpResponseW_Dispose(resp); CkHttpW_Dispose(http); CkHttpRequestW_Dispose(req); CkStringBuilderW_Dispose(sbResponseBody); CkJsonObjectW_Dispose(jResp); CkStringBuilderW_Dispose(sbXml); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.