Go to top ↑
Menu
JSON API iOS SDK Android SDK

Linking a broker with OAuth

First the user must be prompted to choose which broker they wish to link. To retrieve the list of available brokers:

linkedBrokerManager.getAvailableBrokers(new TradeItCallback<List<TradeItAvailableBrokersResponse.Broker>>() {
    @Override
    public void onSuccess(List<Broker> brokerList) {
        // a list of broker is returned 
    }
    
    @Override
    public void onError(TradeItErrorResult error) {
        // an error occured
    }
});

Once a broker has been selected, link the user’s broker account by sending the user to the OAuth login URL.

// get the oauthURL
linkedBrokerManager.getOAuthLoginPopupUrl(
    "Dummy",
    "yourScheme://yourHost", // This URL is a deep link into your app that will be used to complete OAuth. More info below...
    new TradeItCallback<String>() {
        @Override
        public void onSuccess(String oAuthUrl) {
            // launch the OAuth page by loading the url in a webview 
        }

        @Override
        public void onError(TradeItErrorResult error) {
            // an error occured 
        }
    }
);

The second parameter is an Android deep link URL that should redirect back into your app once the user has completed their OAuth login. Android custom URI schemes are handled in the AndroidManifest.xml file as intents. Here is an example activity:

<activity xmlns:android="http://schemas.android.com/apk/res/android"
          android:name="myapp.handleOAuth"
          android:label="MyApp">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="yourScheme"
              android:host="yourHost" />
    </intent-filter>
</activity>

For more info on configuring deep linking see the documentation: https://developer.android.com/training/app-indexing/deep-linking.html

If successful, the OAuthFlow will redirect to your deep link URL and append the oAuthVerifier token as a query parameter: yourScheme://yourHost?oAuthVerifier=370676d4-f584-4d71-8c37-432aa8948059

The oAuthVerifier can be parsed out like this:

String oAuthVerifier = intent.getData().getQueryParameter("oAuthVerifier");

The last step is to complete the broker linking by submitting the oAuthVerifier.

linkedBrokerManager.linkBrokerWithOauthVerifier("MyAccountLabel", oAuthVerifier, new TradeItCallback<TradeItLinkedBroker>() {
    @Override
    public void onSuccess(TradeItLinkedBroker linkedBroker) {
        // successfully linked broker
    }

    @Override
    public void onError(TradeItErrorResult error) {
        // an error occured
    }
});

This will result in the creation of a TradeItLinkedBroker object. The linked broker object is automatically persisted in the Android secure keystore and added to the list of linked brokers on the linked broker manager:

List<TradeItLinkedBroker> linkedBrokers = linkedBrokerManager.getLinkedBrokers();

When the user restarts the app, the list of linked brokers is automatically repopulated upon initialization and configuration of the TradeItSDK.