• Invest: 1-855-880-2559
  • Open 24/7

Ally Invest API

+

Java:

This short example will show you how to authorize your own account against the API and make requests. Note that we aren't following a full OAuth flow here because as a developer you already have access to your own access token and secret.

This example makes use of Scribe OAuth library for Java which requires that we define a TradeKingApi class that extends the DefaultApi10a class provided by Scribe.

package com.tradeking;
import org.scribe.builder.api.DefaultApi10a;
import org.scribe.model.Token;
public class TradeKingApi extends DefaultApi10a
{
  private static final String AUTHORIZE_URL = "https://developers.tradeking.com/oauth/authorize?oauth_token=%s";
  private static final String REQUEST_TOKEN_RESOURCE = "https://developers.tradeking.com/oauth/request_token";
  private static final String ACCESS_TOKEN_RESOURCE = "https://developers.tradeking.com/oauth/access_token";
  
  @Override
  public String getAccessTokenEndpoint()
  {
    return ACCESS_TOKEN_RESOURCE;
  }
  @Override
  public String getRequestTokenEndpoint()
  {
    return REQUEST_TOKEN_RESOURCE;
  }
  @Override
  public String getAuthorizationUrl(Token requestToken)
  {
    return String.format(AUTHORIZE_URL, requestToken.getToken());
  }
}

We can then use the TradeKingApi class to instantiate an OAuthService and Token to make requests against the API.

package com.tradeking;
import org.scribe.builder.*;
import org.scribe.model.*;
import org.scribe.oauth.*;
public class TradeKingClient
{
	private static final String CONSUMER_KEY = "0cc175b9c0f1b6a831c399e269772661";
	private static final String CONSUMER_SECRET = "ff2513194e75315625628304c9eb66e8";
	private static final String OAUTH_TOKEN = "150a96573adf12b21dab621e85497e6e";
	private static final String OAUTH_TOKEN_SECRET = "5c7b57d450a71d378a5eda991f809e56";
	
	private static final String PROTECTED_RESOURCE_URL = "https://api.tradeking.com/v1/member/profile.json";
  
	public static void main(String[] args)
	{
		OAuthService service = new ServiceBuilder()
                                .provider(TradeKingApi.class)
                                .apiKey(CONSUMER_KEY)
                                .apiSecret(CONSUMER_SECRET)
                                .build();
		Token accessToken = new Token(OAUTH_TOKEN, OAUTH_TOKEN_SECRET);
    
		// Now let's go and ask for a protected resource!
		OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
		service.signRequest(accessToken, request);
		Response response = request.send();
		System.out.println(response.getBody());
	}
}
Disclaimer:

Any mention of actual symbols are to be used for coding purposes only and do not imply a recommendation or solicitation to buy or sell a particular security or to engage in any particular investment strategy.

At the time of publication and in the preceding month, Ally Invest did not have ownership greater than 1% in any stocks mentioned here and does not have any other actual, material conflict of interest known at the time of publication.

Ally Invest did not receive compensation from a public offering or from investment banking services related to any companies mentioned here within the past 12 months, or expects to receive any in the next 3 months. Ally Invest did not engage in market making in the securities mentioned here.

Ally Invest Securities' background can be found at FINRA's BrokerCheck. Options involve risk and are not suitable for all investors.

Review the Characteristics and Risks of Standardized Options brochure (PDF) before you begin trading options. Options investors may lose the entire amount of their investment in a relatively short period of time.