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) ETrade Renew Access TokenRenews an ETrade OAuth access token. For more information, see https://apisb.etrade.com/docs/api/authorization/renew_access_token.html
#include <C_CkHttpW.h> #include <C_CkJsonObjectW.h> #include <C_CkHttpResponseW.h> #include <C_CkStringBuilderW.h> #include <C_CkHashtableW.h> #include <C_CkFileAccessW.h> void ChilkatSample(void) { HCkHttpW http; HCkJsonObjectW jsonToken; BOOL success; HCkHttpResponseW resp; HCkStringBuilderW sbRespBody; HCkHashtableW hashTab; const wchar_t *accessToken; const wchar_t *accessTokenSecret; HCkJsonObjectW json; HCkFileAccessW fac; // This requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. http = CkHttpW_Create(); CkHttpW_putOAuth1(http,TRUE); CkHttpW_putOAuthVerifier(http,L""); CkHttpW_putOAuthConsumerKey(http,L"ETRADE_CONSUMER_KEY"); CkHttpW_putOAuthConsumerSecret(http,L"ETRADE_CONSUMER_SECRET"); // Load the access token previously obtained via the OAuth1 Authorization jsonToken = CkJsonObjectW_Create(); success = CkJsonObjectW_LoadFile(jsonToken,L"qa_data/tokens/etrade.json"); if (success != TRUE) { wprintf(L"Failed to load OAuth1 token\n"); CkHttpW_Dispose(http); CkJsonObjectW_Dispose(jsonToken); return; } CkHttpW_putOAuthToken(http,CkJsonObjectW_stringOf(jsonToken,L"oauth_token")); CkHttpW_putOAuthTokenSecret(http,CkJsonObjectW_stringOf(jsonToken,L"oauth_token_secret")); resp = CkHttpW_QuickGetObj(http,L"https://api.etrade.com/oauth/renew_access_token"); if (CkHttpW_getLastMethodSuccess(http) != TRUE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkHttpW_Dispose(http); CkJsonObjectW_Dispose(jsonToken); return; } // Make sure a successful response was received. if (CkHttpResponseW_getStatusCode(resp) != 200) { wprintf(L"%s\n",CkHttpResponseW_statusLine(resp)); wprintf(L"%s\n",CkHttpResponseW_header(resp)); wprintf(L"%s\n",CkHttpResponseW_bodyStr(resp)); CkHttpW_Dispose(http); CkJsonObjectW_Dispose(jsonToken); return; } // If successful, the resp.BodyStr contains something like this: // oauth_token=%3TiQRgQCRGPo7Xdk6G8QDSEzX0Jsy6sKNcULcDavAGgU%3D&oauth_token_secret=%7RrC9scEpzcwSEMy4vE7nodSzPLqfRINnTNY4voczyFM%3D wprintf(L"%s\n",CkHttpResponseW_bodyStr(resp)); sbRespBody = CkStringBuilderW_Create(); CkHttpResponseW_GetBodySb(resp,sbRespBody); if (CkStringBuilderW_ContentsEqual(sbRespBody,L"Access Token has been renewed",FALSE)) { // The documentation at https://apisb.etrade.com/docs/api/authorization/renew_access_token.html // indicates that the response should be as described above. However, the response received when // trying to refresh a non-expired token was "Access Token has been renewed" wprintf(L"Keeping the same access token, but it's renewed...\n"); CkHttpW_Dispose(http); CkJsonObjectW_Dispose(jsonToken); CkStringBuilderW_Dispose(sbRespBody); return; } hashTab = CkHashtableW_Create(); CkHashtableW_AddQueryParams(hashTab,CkHttpResponseW_bodyStr(resp)); accessToken = CkHashtableW_lookupStr(hashTab,L"oauth_token"); accessTokenSecret = CkHashtableW_lookupStr(hashTab,L"oauth_token_secret"); CkHttpResponseW_Dispose(resp); // The access token + secret is what should be saved and used for // subsequent REST API calls. wprintf(L"Access Token = %s\n",accessToken); wprintf(L"Access Token Secret = %s\n",accessTokenSecret); // Save this access token for future calls. // Just in case we need user_id and screen_name, save those also.. json = CkJsonObjectW_Create(); CkJsonObjectW_AppendString(json,L"oauth_token",accessToken); CkJsonObjectW_AppendString(json,L"oauth_token_secret",accessTokenSecret); fac = CkFileAccessW_Create(); CkFileAccessW_WriteEntireTextFile(fac,L"qa_data/tokens/etrade.json",CkJsonObjectW_emit(json),L"utf-8",FALSE); wprintf(L"Success.\n"); CkHttpW_Dispose(http); CkJsonObjectW_Dispose(jsonToken); CkStringBuilderW_Dispose(sbRespBody); CkHashtableW_Dispose(hashTab); CkJsonObjectW_Dispose(json); CkFileAccessW_Dispose(fac); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.