How to Add OAuth AI Access to a Desktop App with PKCE
A practical OAuth 2.0 and PKCE flow for desktop AI apps: authorization, state validation, secure token exchange, scopes and usage reconciliation.
HeFu · Published 2026-11-21 · Updated 2026-11-28
4 min read
A desktop app can let users connect their HeFu account without embedding an API key or client secret. Use OAuth 2.0 authorization code flow with S256 PKCE, a loopback callback, and a random state value. The user authorizes the app, and the app calls the agreed capabilities with the resulting access token.
Choose the Billing Mode Before Writing the Flow
HeFu's current app-integration flow supports two modes. In end-user mode, each user authorizes your app and pays for model usage from their own wallet. In application-paid mode, your server uses application credentials and the application owner's main-account wallet pays. These are different trust and billing models; do not mix their credentials or callback behavior.
For a public desktop client with user-paid usage, use authorization code + PKCE. Do not embed a client secret in the application package. Server-side application-paid integrations belong in your backend, not in the desktop client.
Create the App and Request Minimal Scopes
Register the app with its details, loopback callback URL, billing mode, and requested scopes. Apps require review, and users explicitly authorize the requested capabilities.
Start with the minimum scope your feature needs. The current chat-oriented public-client demo defaults to ext_api. Additional scopes such as balance, usage, or model-list access should be requested only after approval and only when your interface actually uses them.
Use the current app-integration page and OpenAPI contract for the authoritative registration and scope details.
Build the Authorization Request
Generate a random PKCE verifier and derive its SHA-256 challenge using base64url encoding without padding. Also generate an unrelated random state value. Keep both in the app's protected session until the callback arrives.
The browser authorization page is:
https://hefu.hk/oauth/authorize
Include the client ID, loopback redirect URI, response_type=code, requested scope, state, challenge, and code_challenge_method=S256. Do not use the similarly named POST API path as the browser page.
The GitHub OAuth + PKCE example shows the complete request in Python.
Validate the Callback Before Exchanging the Code
After the user approves, the platform redirects to your loopback callback with an authorization code and state. Parse the callback URL and compare the returned state with the value stored before authorization. A mismatch must stop the flow; do not exchange the code.
Then send the authorization code, original redirect URI, client ID, and original PKCE verifier to the token endpoint:
https://api.hefu.hk/api/v1/oauth/token
The verifier proves possession of the original authorization request. No client secret is required for this public-client flow. The token endpoint and browser authorization page are intentionally different URLs.
Call the API and Respect User Control
Use the access token as a bearer token against the documented /api/v1/ext endpoints, such as https://api.hefu.hk/api/v1/ext/chat/completions for chat. Keep tokens in protected storage, honor expiry, and use the documented refresh-token grant when needed.
Tell users when their wallet is being used, show relevant usage where available, and provide a way to revoke authorization. Actual usage and charges should be reconciled against the console's call records and wallet details, including applicable discounts.
For a working local session, state check, and code exchange, clone the HeFu API examples repository and follow app-integration/README.md. The final example chat request is real and billable to the authorizing user; do not run it as an unattended test.
FAQ
Why should a public desktop client use PKCE?
A desktop app cannot safely embed a client secret. S256 PKCE binds the authorization request to a verifier held by the app, so the authorization code alone is not enough to complete the exchange.
What is state used for?
State is a random value that binds the callback to the authorization request initiated by the user. Validate it before exchanging the authorization code.
Who pays for model usage?
In end-user authorization mode, the authorizing user's HeFu wallet pays. In application-paid server-to-server mode, the application owner's main-account wallet pays; choose the mode when registering the app.
Which scope should my app request first?
Start with the minimum required scope. For the current chat-oriented public-client demo, the default is ext_api. Request additional read scopes only when the app has been approved for them and actually needs the data.
FAQ
Why should a public desktop client use PKCE?
A desktop app cannot safely embed a client secret. S256 PKCE binds the authorization request to a verifier held by the app, so the authorization code alone is not enough to complete the exchange.
What is state used for?
State is a random value that binds the callback to the authorization request initiated by the user. Validate it before exchanging the authorization code.
Who pays for model usage?
In end-user authorization mode, the authorizing user's HeFu wallet pays. In application-paid server-to-server mode, the application owner's main-account wallet pays; choose the mode when registering the app.
Which scope should my app request first?
Start with the minimum required scope. For the current chat-oriented public-client demo, the default is ext_api. Request additional read scopes only when the app has been approved for them and actually needs the data.