Delphi DLL
Delphi DLL
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 Delphi DLL Downloads
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Http, FileAccess, Hashtable, HttpResponse, JsonObject;
...
procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
consumerKey: PWideChar;
consumerSecret: PWideChar;
requestTokenUrl: PWideChar;
authorizeUrl: PWideChar;
accessTokenUrl: PWideChar;
http: HCkHttp;
jsonRequestToken: HCkJsonObject;
requestToken: PWideChar;
requestTokenSecret: PWideChar;
resp: HCkHttpResponse;
hashTab: HCkHashtable;
accessToken: PWideChar;
accessTokenSecret: PWideChar;
json: HCkJsonObject;
fac: HCkFileAccess;
begin
success := False;
// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
consumerKey := 'ETRADE_CONSUMER_KEY';
consumerSecret := 'ETRADE_CONSUMER_SECRET';
requestTokenUrl := 'https://apisb.etrade.com/oauth/request_token';
authorizeUrl := 'https://us.etrade.com/e/t/etws/authorize';
accessTokenUrl := 'https://apisb.etrade.com/oauth/access_token';
http := CkHttp_Create();
success := True;
CkHttp_putOAuth1(http,True);
CkHttp_putOAuthConsumerKey(http,consumerKey);
CkHttp_putOAuthConsumerSecret(http,consumerSecret);
CkHttp_putOAuthCallback(http,'oob');
jsonRequestToken := CkJsonObject_Create();
success := CkJsonObject_LoadFile(jsonRequestToken,'qa_data/tokens/etrade_request_token.json');
requestToken := CkJsonObject__stringOf(jsonRequestToken,'oauth_token');
requestTokenSecret := CkJsonObject__stringOf(jsonRequestToken,'oauth_token_secret');
// ------------------------------------------------------------------------------
// Exchange the OAuth Request Token for an OAuth Access Token.
CkHttp_putOAuthToken(http,requestToken);
CkHttp_putOAuthTokenSecret(http,requestTokenSecret);
// This is the verifier that was interactively copy-and-pasted from the browser back to our app.
CkHttp_putOAuthVerifier(http,'NJ07S');
// Use the explicit string "INCLUDE_OAUTH_TOKEN" to tell Chilkat to include the "oauth_token" param in the Authorization header field
CkHttp_putUncommonOptions(http,'INCLUDE_OAUTH_TOKEN');
resp := CkHttpResponse_Create();
success := CkHttp_HttpNoBody(http,'GET',accessTokenUrl,resp);
if (success = False) then
begin
Memo1.Lines.Add(CkHttp__lastErrorText(http));
Exit;
end;
// Make sure a successful response was received.
if (CkHttpResponse_getStatusCode(resp) <> 200) then
begin
Memo1.Lines.Add(CkHttpResponse__statusLine(resp));
Memo1.Lines.Add(CkHttpResponse__header(resp));
Memo1.Lines.Add(CkHttpResponse__bodyStr(resp));
Exit;
end;
// If successful, the resp.BodyStr contains something like this:
// oauth_token=85123455-fF41296Bi3daM8eCo9Y5vZabcdxXpRv864plYPOjr&oauth_token_secret=afiYJOgabcdSfGae7BDvJVVTwys8fUGpra5guZxbmFBZo
Memo1.Lines.Add(CkHttpResponse__bodyStr(resp));
hashTab := CkHashtable_Create();
CkHashtable_AddQueryParams(hashTab,CkHttpResponse__bodyStr(resp));
accessToken := CkHashtable__lookupStr(hashTab,'oauth_token');
accessTokenSecret := CkHashtable__lookupStr(hashTab,'oauth_token_secret');
// The access token + secret is what should be saved and used for
// subsequent REST API calls.
Memo1.Lines.Add('Access Token = ' + accessToken);
Memo1.Lines.Add('Access Token Secret = ' + accessTokenSecret);
// Save this access token for future calls.
// Just in case we need user_id and screen_name, save those also..
json := CkJsonObject_Create();
CkJsonObject_AppendString(json,'oauth_token',accessToken);
CkJsonObject_AppendString(json,'oauth_token_secret',accessTokenSecret);
fac := CkFileAccess_Create();
CkFileAccess_WriteEntireTextFile(fac,'qa_data/tokens/etrade.json',CkJsonObject__emit(json),'utf-8',False);
Memo1.Lines.Add('Success.');
CkHttp_Dispose(http);
CkJsonObject_Dispose(jsonRequestToken);
CkHttpResponse_Dispose(resp);
CkHashtable_Dispose(hashTab);
CkJsonObject_Dispose(json);
CkFileAccess_Dispose(fac);
end;