Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Adds API support for TradeStation #339

Closed
wants to merge 1 commit into from

2 participants

John Jelinek IV Pablo Fernandez
John Jelinek IV

No description provided.

John Jelinek IV johnjelinek commented on the diff
src/main/java/org/scribe/oauth/OAuth20ServiceImpl.java
@@ -28,6 +28,7 @@ public OAuth20ServiceImpl(DefaultApi20 api, OAuthConfig config)
public Token getAccessToken(Token requestToken, Verifier verifier)
{
OAuthRequest request = new OAuthRequest(api.getAccessTokenVerb(), api.getAccessTokenEndpoint());
+ request.addQuerystringParameter(OAuthConstants.GRANT_TYPE, OAuthConstants.AUTHORIZATION_CODE);

@fernandezpablo85 I needed to modify this to get the an access token from our API. The OAuth2 spec says to that grant_type=authorization_code is a required parameter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
John Jelinek IV

@fernandezpablo85 why did this get rejected?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 20, 2012
  1. John Jelinek IV
This page is out of date. Refresh to see the latest.
33 src/main/java/org/scribe/builder/api/TradeStationLiveApi.java
View
@@ -0,0 +1,33 @@
+package org.scribe.builder.api;
+
+import org.scribe.extractors.AccessTokenExtractor;
+import org.scribe.extractors.JsonTokenExtractor;
+import org.scribe.model.OAuthConfig;
+import org.scribe.model.Verb;
+import org.scribe.utils.OAuthEncoder;
+import org.scribe.utils.Preconditions;
+
+/**
+ * @author John Jelinek IV
+ */
+public class TradeStationLiveApi extends DefaultApi20 {
+ private static final String BASEURL = "https://api.tradestation.com/v2/";
+ private static final String AUTHORIZE_URL = BASEURL + "authorize?client_id=%s&response_type=code&redirect_uri=%s";
+
+ @Override public String getAccessTokenEndpoint() {
+ return BASEURL + "security/authorize";
+ }
+
+ @Override public String getAuthorizationUrl(OAuthConfig config) {
+ Preconditions.checkValidUrl(config.getCallback(), "Must provide a valid url as callback. TradeStation does not support OOB.");
+ return String.format(AUTHORIZE_URL, config.getApiKey(), OAuthEncoder.encode(config.getCallback()));
+ }
+
+ @Override public Verb getAccessTokenVerb() {
+ return Verb.POST;
+ }
+
+ @Override public AccessTokenExtractor getAccessTokenExtractor() {
+ return new JsonTokenExtractor();
+ }
+}
33 src/main/java/org/scribe/builder/api/TradeStationSimulatedApi.java
View
@@ -0,0 +1,33 @@
+package org.scribe.builder.api;
+
+import org.scribe.extractors.AccessTokenExtractor;
+import org.scribe.extractors.JsonTokenExtractor;
+import org.scribe.model.OAuthConfig;
+import org.scribe.model.Verb;
+import org.scribe.utils.OAuthEncoder;
+import org.scribe.utils.Preconditions;
+
+/**
+ * @author John Jelinek IV
+ */
+public class TradeStationSimulatedApi extends DefaultApi20 {
+ private static final String BASEURL = "https://sim.api.tradestation.com/v2/";
+ private static final String AUTHORIZE_URL = BASEURL + "authorize?client_id=%s&response_type=code&redirect_uri=%s";
+
+ @Override public String getAccessTokenEndpoint() {
+ return BASEURL + "security/authorize";
+ }
+
+ @Override public String getAuthorizationUrl(OAuthConfig config) {
+ Preconditions.checkValidUrl(config.getCallback(), "Must provide a valid url as callback. TradeStation does not support OOB.");
+ return String.format(AUTHORIZE_URL, config.getApiKey(), OAuthEncoder.encode(config.getCallback()));
+ }
+
+ @Override public Verb getAccessTokenVerb() {
+ return Verb.POST;
+ }
+
+ @Override public AccessTokenExtractor getAccessTokenExtractor() {
+ return new JsonTokenExtractor();
+ }
+}
2  src/main/java/org/scribe/model/OAuthConstants.java
View
@@ -45,5 +45,7 @@
public static final String CLIENT_SECRET = "client_secret";
public static final String REDIRECT_URI = "redirect_uri";
public static final String CODE = "code";
+ public static final String GRANT_TYPE = "grant_type";
+ public static final String AUTHORIZATION_CODE = "authorization_code";
}
1  src/main/java/org/scribe/oauth/OAuth20ServiceImpl.java
View
@@ -28,6 +28,7 @@ public OAuth20ServiceImpl(DefaultApi20 api, OAuthConfig config)
public Token getAccessToken(Token requestToken, Verifier verifier)
{
OAuthRequest request = new OAuthRequest(api.getAccessTokenVerb(), api.getAccessTokenEndpoint());
+ request.addQuerystringParameter(OAuthConstants.GRANT_TYPE, OAuthConstants.AUTHORIZATION_CODE);

@fernandezpablo85 I needed to modify this to get the an access token from our API. The OAuth2 spec says to that grant_type=authorization_code is a required parameter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
request.addQuerystringParameter(OAuthConstants.CLIENT_ID, config.getApiKey());
request.addQuerystringParameter(OAuthConstants.CLIENT_SECRET, config.getApiSecret());
request.addQuerystringParameter(OAuthConstants.CODE, verifier.getValue());
100 src/test/java/org/scribe/examples/TradesStationExample.java
View
@@ -0,0 +1,100 @@
+package org.scribe.examples;
+
+import java.util.Scanner;
+
+import org.scribe.builder.ServiceBuilder;
+import org.scribe.builder.api.Api;
+import org.scribe.builder.api.TradeStationLiveApi;
+import org.scribe.builder.api.TradeStationSimulatedApi;
+import org.scribe.model.OAuthRequest;
+import org.scribe.model.Response;
+import org.scribe.model.Token;
+import org.scribe.model.Verb;
+import org.scribe.model.Verifier;
+import org.scribe.oauth.OAuthService;
+
+/**
+ * @author John Jelinek IV
+ */
+public class TradesStationExample {
+ private static final Token EMPTY_TOKEN = null;
+
+ public static void main(String[] args) {
+ simExample();
+ liveExample();
+ }
+
+ private static void simExample() {
+ // Replace these with your own api key and secret, environment API and example protected resource
+ String apiKey = "your key goes here";
+ String apiSecret = "your secret goes here";
+ Class<TradeStationSimulatedApi> apiClass = TradeStationSimulatedApi.class;
+ String protectedResource = "https://sim.api.tradestation.com/v2/data/quote/msft";
+
+ example(apiKey, apiSecret, apiClass, protectedResource);
+ }
+
+ private static void liveExample() {
+ // Replace these with your own api key and secret, environment API and example protected resource
+ String apiKey = "your key goes here";
+ String apiSecret = "your secret goes here";
+ Class<TradeStationLiveApi> apiClass = TradeStationLiveApi.class;
+ String protectedResource = "https://api.tradestation.com/v2/data/quote/msft";
+
+ example(apiKey, apiSecret, apiClass, protectedResource);
+ }
+
+ private static void example(String apiKey, String apiSecret, Class<? extends Api> apiClass, String protectedResource) {
+ OAuthService service = new ServiceBuilder()
+ .provider(apiClass)
+ .apiKey(apiKey)
+ .apiSecret(apiSecret)
+ .callback("http://www.tradestation.com")
+ .build();
+
+ Verifier verifier = getAuthorizationUrl(service);
+ Token accessToken = getAccessToken(service, verifier);
+ getProtectedResource(accessToken, protectedResource);
+ }
+
+ private static void getProtectedResource(Token accessToken, String protectedResource) {
+ // Now let's go and ask for a protected resource!
+ System.out.println("Now we're going to access a protected resource...");
+ OAuthRequest request = new OAuthRequest(Verb.GET, protectedResource + "?oauth_token=" + accessToken.getToken());
+ Response response = request.send();
+ System.out.println("Got it! Lets see what we found...");
+ System.out.println();
+ System.out.println(response.getCode());
+ System.out.println(response.getBody());
+
+ System.out.println();
+ System.out.println("Thats it man! Go and build something awesome with Scribe! :)");
+ }
+
+ private static Token getAccessToken(OAuthService service, Verifier verifier) {
+ // Trade the Request Token and Verifier for the Access Token
+ System.out.println("Trading the Request Token for an Access Token...");
+ Token accessToken = service.getAccessToken(EMPTY_TOKEN, verifier);
+ System.out.println("Got the Access Token!");
+ System.out.println("(if your curious it looks like this: " + accessToken + " )");
+ System.out.println();
+ return accessToken;
+ }
+
+ private static Verifier getAuthorizationUrl(OAuthService service) {
+ // Obtain the Authorization URL
+ Scanner in = new Scanner(System.in);
+ System.out.println("Fetching the Authorization URL...");
+ String authorizationUrl = service.getAuthorizationUrl(EMPTY_TOKEN);
+ System.out.println("Got the Authorization URL!");
+ System.out.println("Now go and authorize Scribe here:");
+ System.out.println(authorizationUrl);
+ System.out.println("And paste the authorization code here");
+ System.out.print(">>");
+ Verifier verifier = new Verifier(in.nextLine());
+ in.close();
+ System.out.println();
+ return verifier;
+ }
+
+}
Something went wrong with that request. Please try again.