Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

Unicode C Web API Examples

Primary Categories

ABN AMRO
AWS Translate
Activix CRM
Adyen
Amazon DynamoDB
Amazon MWS
Aruba Fatturazione
Azure Maps
Azure Monitor
Azure OAuth2
Azure Storage Accounts
Bitfinex v2 REST
Bluzone
CallRail
CardConnect
ClickBank
Clickatell
Cloudfare
Constant Contact
DocuSign
ETrade
Etsy
Facebook
Frame.io
GeoOp
GetHarvest
Global Payments
Google People
Google Search Console
Hungary NAV Invoicing
IBM Text to Speech
Jira
Lightspeed
MYOB

Magento
MercadoLibre
Microsoft Calendar
Microsoft Group
Microsoft Tasks and Plans
Microsoft Teams
Okta OAuth/OIDC
OneLogin OIDC
OneNote
PayPal
Peoplevox
Populi
QuickBooks
Rabobank
SMSAPI
SOAP finkok.com
Shippo
Shopify
SimpleTexting
Square
Stripe
SugarCRM
Trello
Twilio
Twitter
VoiceBase
Vonage
Walmart
Walmart v3
WhatsApp
WiX
WooCommerce
Xero
Yousign
_Miscellaneous_
eBay
effectconnect

 

 

 

(Unicode C) ETrade OAuth1 Authorization (3-legged) Step 2

Demonstrates 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

Chilkat C/C++ Library Downloads

MS Visual C/C++

Linux/CentOS C/C++

Alpine Linux C/C++

MAC OS X C/C++

armhf/aarch64 C/C++

C++ Builder

iOS C/C++

Android C/C++

Win Mobile 5.0/Pocket PC 2003

Solaris C/C++

FreeBSD C/C++

OpenBSD C/C++

MinGW C/C++

#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-2020 Chilkat Software, Inc. All Rights Reserved.