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 OAuth1 Authorization (3-legged) Step 2Demonstrates the final step in 3-legged OAuth1 authorization for the ETrade REST API. Example uses the OAuth1 verifier code that was copy-and-pasted from the browser in the 1st step. The end result of this final OAuth1 step is an access token that can be used to make ETrade REST API calls. See https://apisb.etrade.com/docs/api/authorization/get_access_token.html
#include <C_CkHttpW.h> #include <C_CkJsonObjectW.h> #include <C_CkHttpResponseW.h> #include <C_CkHashtableW.h> #include <C_CkFileAccessW.h> void ChilkatSample(void) { const wchar_t *consumerKey; const wchar_t *consumerSecret; const wchar_t *requestTokenUrl; const wchar_t *authorizeUrl; const wchar_t *accessTokenUrl; HCkHttpW http; BOOL success; HCkJsonObjectW jsonRequestToken; const wchar_t *requestToken; const wchar_t *requestTokenSecret; HCkHttpResponseW resp; 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. consumerKey = L"ETRADE_CONSUMER_KEY"; consumerSecret = L"ETRADE_CONSUMER_SECRET"; requestTokenUrl = L"https://apisb.etrade.com/oauth/request_token"; authorizeUrl = L"https://us.etrade.com/e/t/etws/authorize"; accessTokenUrl = L"https://apisb.etrade.com/oauth/access_token"; http = CkHttpW_Create(); success = TRUE; CkHttpW_putOAuth1(http,TRUE); CkHttpW_putOAuthConsumerKey(http,consumerKey); CkHttpW_putOAuthConsumerSecret(http,consumerSecret); CkHttpW_putOAuthCallback(http,L"oob"); jsonRequestToken = CkJsonObjectW_Create(); success = CkJsonObjectW_LoadFile(jsonRequestToken,L"qa_data/tokens/etrade_request_token.json"); requestToken = CkJsonObjectW_stringOf(jsonRequestToken,L"oauth_token"); requestTokenSecret = CkJsonObjectW_stringOf(jsonRequestToken,L"oauth_token_secret"); // ------------------------------------------------------------------------------ // Exchange the OAuth Request Token for an OAuth Access Token. CkHttpW_putOAuthToken(http,requestToken); CkHttpW_putOAuthTokenSecret(http,requestTokenSecret); // This is the verifier that was interactively copy-and-pasted from the browser back to our app. CkHttpW_putOAuthVerifier(http,L"NJ07S"); resp = CkHttpW_QuickGetObj(http,accessTokenUrl); if (CkHttpW_getLastMethodSuccess(http) != TRUE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkHttpW_Dispose(http); CkJsonObjectW_Dispose(jsonRequestToken); 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(jsonRequestToken); return; } // If successful, the resp.BodyStr contains something like this: // oauth_token=85123455-fF41296Bi3daM8eCo9Y5vZabcdxXpRv864plYPOjr&oauth_token_secret=afiYJOgabcdSfGae7BDvJVVTwys8fUGpra5guZxbmFBZo wprintf(L"%s\n",CkHttpResponseW_bodyStr(resp)); 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(jsonRequestToken); CkHashtableW_Dispose(hashTab); CkJsonObjectW_Dispose(json); CkFileAccessW_Dispose(fac); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.