oauth

package
v0.20.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 10, 2026 License: Apache-2.0, MIT Imports: 10 Imported by: 0

Documentation

Overview

Package oauth provides OAuth 2.0 authentication support for Longbridge OpenAPI.

Use OAuth by setting it on config (like the Rust SDK). The token is stored internally and in ~/.longbridge-openapi/tokens/<client_id>; it is refreshed automatically when expired. Do not use or expose OAuthToken.

Example

o := oauth.New("your-client-id").
    OnOpenURL(func(url string) { fmt.Println("Please visit:", url) })
if err := o.Build(context.Background()); err != nil {
    log.Fatal(err)
}
cfg, _ := config.New(config.WithOAuthClient(o))
tctx, _ := trade.NewFromCfg(cfg)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type OAuth

type OAuth struct {
	// contains filtered or unexported fields
}

OAuth is the OAuth 2.0 client for Longbridge OpenAPI. Use New and Build, then pass to config via config.WithOAuthClient(o). AccessToken(ctx) is used by the HTTP client to get a valid token (auto-refresh).

func New

func New(clientID string) *OAuth

New creates a new OAuth client with the given client ID.

func NewWithBaseURL

func NewWithBaseURL(clientID, baseURL string) *OAuth

NewWithBaseURL creates a new OAuth client with a custom base URL (e.g. for tests).

func (*OAuth) AccessToken

func (o *OAuth) AccessToken(ctx context.Context) (string, error)

AccessToken returns a valid access token, refreshing it if expired or soon to expire. The HTTP client calls this when OAuth is set on config.

func (*OAuth) Build

func (o *OAuth) Build(ctx context.Context) error

Build loads a token from ~/.longbridge-openapi/tokens/<client_id> or runs the authorization flow, then stores the token in memory and on disk. Call this once before passing the OAuth to config. If a token exists and is expired, it is refreshed automatically.

func (*OAuth) ClientID

func (o *OAuth) ClientID() string

ClientID returns the OAuth 2.0 client ID.

func (*OAuth) OnOpenURL

func (o *OAuth) OnOpenURL(f func(string)) *OAuth

OnOpenURL sets a callback for the authorization URL (e.g. open in browser or print).

func (*OAuth) WithCallbackPort

func (o *OAuth) WithCallbackPort(port int) *OAuth

WithCallbackPort sets the local callback port (default: 60355).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL