Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.0.0+

ETrade OAuth1 Authorization (3-legged) Step 2

See more ETrade Examples

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL cConsumerKey
LOCAL cConsumerSecret
LOCAL cRequestTokenUrl
LOCAL cAuthorizeUrl
LOCAL cAccessTokenUrl
LOCAL oHttp
LOCAL oJsonRequestToken
LOCAL cRequestToken
LOCAL cRequestTokenSecret
LOCAL oResp
LOCAL oHashTab
LOCAL cAccessToken
LOCAL cAccessTokenSecret
LOCAL oJson
LOCAL oFac

nSuccess := 0

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

cConsumerKey := "ETRADE_CONSUMER_KEY"
cConsumerSecret := "ETRADE_CONSUMER_SECRET"

cRequestTokenUrl := "https://apisb.etrade.com/oauth/request_token"
cAuthorizeUrl := "https://us.etrade.com/e/t/etws/authorize"
cAccessTokenUrl := "https://apisb.etrade.com/oauth/access_token"

oHttp := CreateObject("Chilkat.Http")
nSuccess := 1

oHttp:OAuth1 := 1
oHttp:OAuthConsumerKey := cConsumerKey
oHttp:OAuthConsumerSecret := cConsumerSecret
oHttp:OAuthCallback := "oob"

oJsonRequestToken := CreateObject("Chilkat.JsonObject")
nSuccess := oJsonRequestToken:LoadFile("qa_data/tokens/etrade_request_token.json")
cRequestToken := oJsonRequestToken:StringOf("oauth_token")
cRequestTokenSecret := oJsonRequestToken:StringOf("oauth_token_secret")

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

oHttp:OAuthToken := cRequestToken
oHttp:OAuthTokenSecret := cRequestTokenSecret

//  This is the verifier that was interactively copy-and-pasted from the browser back to our app.
oHttp:OAuthVerifier := "NJ07S"

//  Use the explicit string "INCLUDE_OAUTH_TOKEN" to tell Chilkat to include the "oauth_token" param in the Authorization header field
oHttp:UncommonOptions := "INCLUDE_OAUTH_TOKEN"

oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpNoBody("GET", cAccessTokenUrl, oResp)
IF (nSuccess == 0)
    ? oHttp:LastErrorText
    oHttp:destroy()
    oJsonRequestToken:destroy()
    oResp:destroy()
    RETURN
ENDIF

//  Make sure a successful response was received.
IF (oResp:StatusCode != 200)
    ? oResp:StatusLine
    ? oResp:Header
    ? oResp:BodyStr
    oHttp:destroy()
    oJsonRequestToken:destroy()
    oResp:destroy()
    RETURN
ENDIF

//  If successful, the resp.BodyStr contains something like this:
//  oauth_token=85123455-fF41296Bi3daM8eCo9Y5vZabcdxXpRv864plYPOjr&oauth_token_secret=afiYJOgabcdSfGae7BDvJVVTwys8fUGpra5guZxbmFBZo
? oResp:BodyStr

oHashTab := CreateObject("Chilkat.Hashtable")
oHashTab:AddQueryParams(oResp:BodyStr)

cAccessToken := oHashTab:LookupStr("oauth_token")
cAccessTokenSecret := oHashTab:LookupStr("oauth_token_secret")

//  The access token + secret is what should be saved and used for
//  subsequent REST API calls.
? "Access Token = " + cAccessToken
? "Access Token Secret = " + cAccessTokenSecret

//  Save this access token for future calls.
//  Just in case we need user_id and screen_name, save those also..
oJson := CreateObject("Chilkat.JsonObject")
oJson:AppendString("oauth_token", cAccessToken)
oJson:AppendString("oauth_token_secret", cAccessTokenSecret)

oFac := CreateObject("Chilkat.FileAccess")
oFac:WriteEntireTextFile("qa_data/tokens/etrade.json", oJson:Emit(), "utf-8", 0)

? "Success."

oHttp:destroy()
oJsonRequestToken:destroy()
oResp:destroy()
oHashTab:destroy()
oJson:destroy()
oFac:destroy()