Download | Code Review | Instructions
Let’s take a look at some code (keep in mind this is not the entire script and it will not run as-is in the Groovy Console — you can, however, review the full script here or download the script along with supporting files here:
The following Grape annotations describe the Maven dependencies required to run the script — note that only those in bold need to be manually registerd.
@Grab(group='log4j', module='log4j', version='1.2.16') @Grab(group='commons-codec', module='commons-codec', version='1.3') @Grab(group='commons-lang', module='commons-lang', version='2.6') @Grab(group='commons-logging', module='commons-logging', version='1.0.4') @Grab(group='commons-httpclient', module='commons-httpclient', version='3.1') // Alternative to the the commons-ssl jar which works. @Grab(group='ca.juliusdavies', module='not-yet-commons-ssl', version='0.3.11') @Grab(group='com.thoughtworks.xstream', module='xstream', version='1.4.5') @Grab(group='com.etrade.etws', module='oauth-sdk', version='1.0') @Grab(group='com.etrade.etws', module='common-connections', version='1.0') @Grab(group='com.etrade.etws', module='accounts-sdk', version='1.0')
The following code sets up the request that will be sent to E*Trade. What’s important here is that the request will produce a different authorization URL if we restart the application, so we need to be able to get the authorization URL, paste it into a browser, and then take the resultant five letter code and use this in the same application. In this example we achieve this by allowing the user to paste this code into a Java Swing-based user interface.
The oauth_consumer_key and consumer_secret are provided by E*Trade and should not be shared publicly (and hence we do not share these here).
def env = System.getenv() /* NOTE THAT YOU NEED TO SUPPLY YOUR OWN CONSUMER KEY AND SECRET! */ def key = env['etrade_oauth_consumer_key'] def keySecret = env['etrade_consumer_secret'] def client = OAuthClientImpl.getInstance() def request = new ClientRequest() request.env = Environment.SANDBOX request.consumerKey = key request.consumerSecret = keySecret def token = client.getRequestToken(request) request.token = token.token request.tokenSecret = token.secret
The next section prints the authorization URL, which we copy and paste into a browser.
println "authorizeUrl: ${client.getAuthorizeUrl (request)}"
Finally, the doneButton’s action listener appears below, and this is where we assign the request’s verifierCode — this is the resultant five letter code that was returned when we agreed to the terms that appeared on the page that the authorization URL pointed to.
doneButton.addActionListener ( new ActionListener () { public void actionPerformed(ActionEvent actionEvent) { /* Once the request has been verified, we set the verifier code on * the original request -- we can now use this request to get an * access token, which is used below when creating the * accountClient. */ request.verifierCode = verifierCodeView.text
At this point we can create and execute a request for account information, then the script will print this information to the console.
def accessToken = client.getAccessToken(request) try { def accountRequest = new ClientRequest() accountRequest.env = Environment.SANDBOX accountRequest.consumerKey = key accountRequest.consumerSecret = keySecret accountRequest.token = accessToken.token accountRequest.tokenSecret = accessToken.secret def accountClient = new AccountsClient(accountRequest) def response = accountClient.getAccountList() def accountList = response.getResponse() def accountIterator = accountList.iterator() while (accountIterator.hasNext()) { def account = accountIterator.next() println("===================") println("Account: " + account.getAccountId()) println("===================") } } catch (Exception exception) { exception.printStackTrace (System.err) } }
The associated script is written in Groovy and the example below is executed in the Groovy Console and calls the E*Trade web services. Follow the pictures below in the order that they appear and the script should run without issue.
You’ll need to install the E*Trade API jar files as Maven dependencies using the mvn-install-all.bat batch script, which is included with the download. If you’re using one of the Unix/Linux variants it should be easy enough to take what you need from this batch file.
See also: Tutorial: Start Developing in Java