Class OAuthBuilder


  • public class OAuthBuilder
    extends Object
    Builder for constructing an OAuth client

    clientId is the only required parameter. Optionally set a custom callback port before calling build(java.util.function.Consumer<java.lang.String>).

    The builder will attempt to load an existing token from ~/.longbridge-openapi/tokens/<clientId>. If no valid token is found, the full browser-based authorization flow is started and onOpenUrl is called with the authorization URL. The resulting token is persisted for future use.

    
     OAuthBuilder builder = new OAuthBuilder("your-client-id");
     builder.setCallbackPort(8080);  // optional, default 60355
     OAuth oauth = builder.build(url -> {
         System.out.println("Open this URL: " + url);
     }).get();
     try {
         Config config = Config.fromOAuth(oauth);
     } finally {
         oauth.close();
     }
     
    • Constructor Detail

      • OAuthBuilder

        public OAuthBuilder​(String clientId)
        Create a new OAuthBuilder with the given client ID.
        Parameters:
        clientId - OAuth 2.0 client ID from the Longbridge developer portal
    • Method Detail

      • setCallbackPort

        public OAuthBuilder setCallbackPort​(int port)
        Set the local callback server port.

        Must match one of the redirect URIs registered for the client. Defaults to 60355 if not set (or set to 0).

        Parameters:
        port - TCP port for the local callback server
        Returns:
        this builder
      • build

        public CompletableFuture<OAuth> build​(Consumer<String> onOpenUrl)
        Asynchronously build the OAuth client.

        If a valid token already exists on disk it is loaded directly; otherwise onOpenUrl is invoked with the authorization URL and the full browser-based flow is started.

        Parameters:
        onOpenUrl - Called with the authorization URL; open it in a browser or print it to the console
        Returns:
        CompletableFuture that resolves to a new OAuth handle