Chilkat Examples

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

Tcl Examples

ASN.1
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
Facebook
FileAccess
Firebase
GMail REST API
GeoOp
Google APIs
Google Calendar
Google Cloud Storage
Google Drive
Google Sheets
Gzip
HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)

Java KeyStore (JKS)
Jira
MHT / HTML Email
MIME
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
PayPal
Peoplevox
QuickBooks
REST
REST Misc
RSA Encryption
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Shopify
Socket/SSL/TLS
Spider
Stream
Stripe
SugarCRM
Tar Archive
Twitter
Upload
VoiceBase
Walmart
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Xero
Zip
curl
eBay

 

 

 

(Tcl) 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://developer.etrade.com/ctnt/dev-portal/getDetail?contentUri=V0_Documentation-AuthorizationAPI-GetAccessToken )

This example uses the sandbox access token URL. The live access token URL would be "https://etws.etrade.com/oauth/access_token".

Note: Requires Chilkat v9.5.0.64 or later.

Chilkat Tcl Extension Downloads

Chilkat Tcl Extension Downloads

load ./chilkat.dll

#  Requires Chilkat v9.5.0.64 or later.

#  This requires the Chilkat API to have been previously unlocked.
#  See Global Unlock Sample for sample code.

set consumerKey "ETRADE_CONSUMER_KEY"
set consumerSecret "ETRADE_CONSUMER_SECRET"

set requestTokenUrl "https://etwssandbox.etrade.com/oauth/request_token"
set authorizeUrl "https://us.etrade.com/e/t/etws/authorize"
set accessTokenUrl "https://etwssandbox.etrade.com/oauth/access_token"

set http [new_CkHttp]

set success 1

CkHttp_put_OAuth1 $http 1
CkHttp_put_OAuthConsumerKey $http $consumerKey
CkHttp_put_OAuthConsumerSecret $http $consumerSecret
CkHttp_put_OAuthCallback $http "oob"

set jsonRequestToken [new_CkJsonObject]

set success [CkJsonObject_LoadFile $jsonRequestToken "qa_data/tokens/etrade_request_token.json"]
set requestToken [CkJsonObject_stringOf $jsonRequestToken "oauth_token"]
set requestTokenSecret [CkJsonObject_stringOf $jsonRequestToken "oauth_token_secret"]

#  ------------------------------------------------------------------------------
#  Exchange the OAuth Request Token for an OAuth Access Token.

CkHttp_put_OAuthToken $http $requestToken
CkHttp_put_OAuthTokenSecret $http $requestTokenSecret

#  This is the verifier that was interactively copy-and-pasted from the browser back to our app.
CkHttp_put_OAuthVerifier $http "MFREP"

# resp is a CkHttpResponse
set resp [CkHttp_QuickGetObj $http $accessTokenUrl]
if {[expr [CkHttp_get_LastMethodSuccess $http] != 1]} then {
    puts [CkHttp_lastErrorText $http]
    delete_CkHttp $http
    delete_CkJsonObject $jsonRequestToken
    exit
}

#  Make sure a successful response was received.
if {[expr [CkHttpResponse_get_StatusCode $resp] != 200]} then {
    puts [CkHttpResponse_statusLine $resp]
    puts [CkHttpResponse_header $resp]
    puts [CkHttpResponse_bodyStr $resp]
    delete_CkHttp $http
    delete_CkJsonObject $jsonRequestToken
    exit
}

#  If successful, the resp.BodyStr contains something like this:
#  oauth_token=85123455-fF41296Bi3daM8eCo9Y5vZabcdxXpRv864plYPOjr&oauth_token_secret=afiYJOgabcdSfGae7BDvJVVTwys8fUGpra5guZxbmFBZo
puts [CkHttpResponse_bodyStr $resp]

set hashTab [new_CkHashtable]

CkHashtable_AddQueryParams $hashTab [CkHttpResponse_bodyStr $resp]

set accessToken [CkHashtable_lookupStr $hashTab "oauth_token"]
set accessTokenSecret [CkHashtable_lookupStr $hashTab "oauth_token_secret"]

delete_CkHttpResponse $resp

#  The access token + secret is what should be saved and used for
#  subsequent REST API calls.
puts "Access Token = $accessToken"
puts "Access Token Secret = $accessTokenSecret"

#  Save this access token for future calls.
#  Just in case we need user_id and screen_name, save those also..
set json [new_CkJsonObject]

CkJsonObject_AppendString $json "oauth_token" $accessToken
CkJsonObject_AppendString $json "oauth_token_secret" $accessTokenSecret

set fac [new_CkFileAccess]

CkFileAccess_WriteEntireTextFile $fac "qa_data/tokens/etrade.json" [CkJsonObject_emit $json] "utf-8" 0

puts "Success."

delete_CkHttp $http
delete_CkJsonObject $jsonRequestToken
delete_CkHashtable $hashTab
delete_CkJsonObject $json
delete_CkFileAccess $fac

 

© 2000-2016 Chilkat Software, Inc. All Rights Reserved.