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) Amazon SP-API Request an LWA access tokenSee more Amazon SP-API ExamplesRequest an LWA access token, make a secure HTTP POST to the LWA authentication server (https://api.amazon.com/auth/o2/token) For more information, see https://developer-docs.amazon.com/sp-api/docs/connecting-to-the-selling-partner-api
#include <C_CkHttpW.h> #include <C_CkHttpRequestW.h> #include <C_CkHttpResponseW.h> #include <C_CkJsonObjectW.h> void ChilkatSample(void) { HCkHttpW http; HCkHttpRequestW req; HCkHttpResponseW resp; int statusCode; HCkJsonObjectW json; BOOL success; // 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://example.com/auth/o2/token \ // -H "Host: api.amazon.com" \ // -H "Content-Type: application/x-www-form-urlencoded;charset=UTF-8" \ // -d "grant_type=refresh_token" \ // -d "refresh_token=Aztr|..." \ // -d "client_id=foodev" \ // -d "client_secret=Y76SDl2F" // Use the following online tool to generate HTTP code from a CURL command // Convert a cURL Command to HTTP Source Code req = CkHttpRequestW_Create(); CkHttpRequestW_AddParam(req,L"grant_type",L"refresh_token"); CkHttpRequestW_AddParam(req,L"refresh_token",L"Aztr|..."); CkHttpRequestW_AddParam(req,L"client_id",L"foodev"); CkHttpRequestW_AddParam(req,L"client_secret",L"Y76SDl2F"); CkHttpRequestW_AddHeader(req,L"Host",L"api.amazon.com"); resp = CkHttpW_PostUrlEncoded(http,L"https://api.amazon.com/auth/o2/token",req); if (CkHttpW_getLastMethodSuccess(http) == FALSE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkHttpW_Dispose(http); CkHttpRequestW_Dispose(req); return; } statusCode = CkHttpResponseW_getStatusCode(resp); wprintf(L"Status Code = %d\n",statusCode); json = CkJsonObjectW_Create(); CkJsonObjectW_Load(json,CkHttpResponseW_bodyStr(resp)); CkHttpResponseW_Dispose(resp); CkJsonObjectW_putEmitCompact(json,FALSE); wprintf(L"%s\n",CkJsonObjectW_emit(json)); if (statusCode != 200) { wprintf(L"Failed.\n"); CkHttpW_Dispose(http); CkHttpRequestW_Dispose(req); CkJsonObjectW_Dispose(json); return; } // Success returns this JSON: // { // "access_token": "Atza|...", // "refresh_token": "Atzr|...", // "token_type": "bearer", // "expires_in": 3600 // } // Save the JSON token to a file for use in subsequent calls. // (this example is passing a relative file path. You don't need to save to a file // if, for example, you keep the access token in-memory for subsequent use...) success = CkJsonObjectW_WriteFile(json,L"qa_data/tokens/sp_api_lwa_token.json"); wprintf(L"access_token: %s\n",CkJsonObjectW_stringOf(json,L"access_token")); wprintf(L"Success.\n"); CkHttpW_Dispose(http); CkHttpRequestW_Dispose(req); CkJsonObjectW_Dispose(json); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.