Skip to content

Linking a broker with OAuth

alex [dot] kramer [at] g_m_a_i_l [dot] com edited this page Jan 21, 2019 · 20 revisions

Launching OAuth

After initializing TradeItSDK, use the linkedBrokerManager instance to get the URL for the OAuth login popup. The oAuthCallbackUrl should be a deep link back into your app and will be used to pass the OAuthVerifier token back into the app. On success, launch the resulting URL in the system browser or a UIWebView/SFSafariViewController and the user will be presented with an OAuth login page.

TradeItSDK.linkedBrokerManager.getOAuthLoginPopupUrl(
    withBroker: "dummy",
    oAuthCallbackUrl: "tradeItExampleApp://completeOAuth",
    onSuccess: { url in
        // Do this OR load URL in a UIWebView/SFSafariViewController
        UIApplication.shared.openURL(NSURL(string:url) as! URL)
    },
    onFailure: { errorResult in
        AlertManager().showError(
            errorResult,
            onViewController: self
        )
    }
)

OAuth Completion

A successful OAuth login results in the browser redirecting to the provided callback with an appended query string including the oAuthVerifier. Example:

tradeItExampleApp://completeOAuth?oAuthVerifier=123-456-789-000

This should be set up to deep link back into your app. If you already have deep linking in your app, make sure to use a unique host/path for the Trade.it OAuth flow. Deep linking is handled in AppDelegate.

Complete flow using TradeIt OAuth screens

You need to pass the callback URL and the top most view controller to complete the OAuth flow. For example:

// In AppDelegate
func application(
    _ application: UIApplication,
    open url: URL,
    sourceApplication: String?,
    annotation: Any
) -> Bool {
    // Get the top view controller to pass to the launcher
    TradeItSDK.launcher.handleOAuthCallback(
        onTopmostViewController: topViewController,
        oAuthCallbackUrl: url
    )
}

Complete flow using custom screens

Extract the OAuth verifier token and pass it in to the SDK to complete:

TradeItSDK.linkedBrokerManager.completeOAuth(
    withOAuthVerifier: oAuthVerifier,
    onSuccess: { linkedBroker in
        print("=====> OAuth successful for \(linkedBroker.brokerName)!")
    },
    onFailure: { errorResult in
        print("=====> ERROR: OAuth failed! \(errorResult.errorCode): \(errorResult.shortMessage): \(errorResult.longMessages?.first)")
    }
)