Package com.longbridge
Class OAuthBuilder
- java.lang.Object
-
- com.longbridge.OAuthBuilder
-
public class OAuthBuilder extends Object
Builder for constructing anOAuthclientclientIdis the only required parameter. Optionally set a custom callback port before callingbuild(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 andonOpenUrlis 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 Summary
Constructors Constructor Description OAuthBuilder(String clientId)Create a newOAuthBuilderwith the given client ID.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description CompletableFuture<OAuth>build(Consumer<String> onOpenUrl)Asynchronously build theOAuthclient.OAuthBuildersetCallbackPort(int port)Set the local callback server port.
-
-
-
Constructor Detail
-
OAuthBuilder
public OAuthBuilder(String clientId)
Create a newOAuthBuilderwith 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
60355if not set (or set to0).- Parameters:
port- TCP port for the local callback server- Returns:
- this builder
-
build
public CompletableFuture<OAuth> build(Consumer<String> onOpenUrl)
Asynchronously build theOAuthclient.If a valid token already exists on disk it is loaded directly; otherwise
onOpenUrlis 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
OAuthhandle
-
-