Schwab OAuth 2.0: Authorize a desktop application
See more Schwab Examples
Use a Schwab Trader API app approved for your own desktop application. Register https://127.0.0.1:8182/ as its callback and enter exactly that URL in the code, including the trailing slash. Load your app key and secret from protected configuration. The browser handles Schwab sign-in, account selection, and consent.
Schwab requires HTTPS. Chilkat’s built-in OAuth listener speaks HTTP. For this example, run a loopback HTTPS reverse proxy on port 8182 that forwards the original path and query string to http://127.0.0.1:3017. Use a certificate trusted by the browser for 127.0.0.1. For example, start Caddy with the following Caddyfile and trust its local CA on the desktop:
https://127.0.0.1:8182 {
bind 127.0.0.1
tls internal
reverse_proxy 127.0.0.1:3017
}Keep callback query strings out of proxy logs. If you already use an application-controlled HTTPS callback relay, register its exact URL instead and forward the authorization response to the initiating desktop. After consent, use Refresh a Schwab access token during the seven-day refresh-token lifetime.
These examples use the current user’s Windows Credential Manager or Apple Keychain. On Linux or Android, configure a persistent Chilkat Secrets backend for both examples before running them. The local manager on those platforms uses process memory. Use the same secret name for the same app and Schwab authorization. Give each separate authorization its own name.
Schwab Trader API OAuth guide · Register a Schwab app
Chilkat Unicode C Downloads
#include <C_CkOAuth2W.h>
#include <C_CkJsonObjectW.h>
#include <C_CkSecretsW.h>
#include <C_CkDateTimeW.h>
void ChilkatSample(void)
{
BOOL success;
HCkOAuth2W oauth;
const wchar_t *url;
int waitedMs;
int state;
HCkJsonObjectW reply;
const wchar_t *access;
int lifetime;
const wchar_t *refresh;
HCkSecretsW vault;
HCkJsonObjectW entry;
HCkJsonObjectW tokens;
HCkDateTimeW now;
success = FALSE;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = FALSE;
oauth = CkOAuth2W_Create();
CkOAuth2W_putAuthorizationEndpoint(oauth,L"https://api.schwabapi.com/v1/oauth/authorize");
CkOAuth2W_putTokenEndpoint(oauth,L"https://api.schwabapi.com/v1/oauth/token");
CkOAuth2W_putClientId(oauth,L"APP_KEY");
CkOAuth2W_putClientSecret(oauth,L"APP_SECRET");
CkOAuth2W_putUseBasicAuth(oauth,TRUE);
CkOAuth2W_putCodeChallenge(oauth,FALSE);
// The registered HTTPS callback forwards to this desktop's HTTP listener.
CkOAuth2W_putAppCallbackUrl(oauth,L"https://127.0.0.1:8182/");
CkOAuth2W_putLocalHost(oauth,L"127.0.0.1");
CkOAuth2W_putListenPort(oauth,3017);
url = CkOAuth2W_startAuth(oauth);
if (CkOAuth2W_getLastMethodSuccess(oauth) != TRUE) {
wprintf(L"Could not start authorization. Check the callback settings and free the listener port.\n");
CkOAuth2W_Dispose(oauth);
return;
}
success = CkOAuth2W_LaunchBrowser(oauth,url);
if (success != TRUE) {
CkOAuth2W_Cancel(oauth);
wprintf(L"Could not open the system browser.\n");
CkOAuth2W_Dispose(oauth);
return;
}
// Allow time for sign-in, MFA, and account selection.
waitedMs = 0;
while ((waitedMs < 300000) && (CkOAuth2W_getAuthFlowState(oauth) < 3)) {
CkOAuth2W_SleepMs(oauth,100);
waitedMs = waitedMs + 100;
}
state = CkOAuth2W_getAuthFlowState(oauth);
if (state < 3) {
CkOAuth2W_Cancel(oauth);
wprintf(L"Authorization timed out. Check the HTTPS callback and try again.\n");
CkOAuth2W_Dispose(oauth);
return;
}
if (state == 4) {
wprintf(L"Authorization was declined.\n");
CkOAuth2W_Dispose(oauth);
return;
}
if (state != 3) {
wprintf(L"Authorization failed. Check app approval, credentials, and the exact callback URL.\n");
CkOAuth2W_Dispose(oauth);
return;
}
reply = CkJsonObjectW_Create();
success = CkJsonObjectW_Load(reply,CkOAuth2W_accessTokenResponse(oauth));
if (success != TRUE) {
wprintf(L"Unexpected token response.\n");
CkOAuth2W_Dispose(oauth);
CkJsonObjectW_Dispose(reply);
return;
}
if (CkJsonObjectW_HasMember(reply,L"access_token") != TRUE) {
wprintf(L"The token response contained no access token.\n");
CkOAuth2W_Dispose(oauth);
CkJsonObjectW_Dispose(reply);
return;
}
access = CkJsonObjectW_stringOf(reply,L"access_token");
lifetime = CkJsonObjectW_IntOf(reply,L"expires_in");
if ((access == L"") || (lifetime <= 0)) {
wprintf(L"The token response lacks a usable access token or lifetime.\n");
CkOAuth2W_Dispose(oauth);
CkJsonObjectW_Dispose(reply);
return;
}
if (CkJsonObjectW_HasMember(reply,L"refresh_token") != TRUE) {
wprintf(L"No refresh token in the authorization response.\n");
CkOAuth2W_Dispose(oauth);
CkJsonObjectW_Dispose(reply);
return;
}
refresh = CkJsonObjectW_stringOf(reply,L"refresh_token");
if (refresh == L"") {
wprintf(L"The refresh token is empty.\n");
CkOAuth2W_Dispose(oauth);
CkJsonObjectW_Dispose(reply);
return;
}
vault = CkSecretsW_Create();
CkSecretsW_putLocation(vault,L"local_manager");
CkSecretsW_putAutoSplit(vault,TRUE);
entry = CkJsonObjectW_Create();
CkJsonObjectW_UpdateString(entry,L"appName",L"SchwabTrader");
CkJsonObjectW_UpdateString(entry,L"service",L"OAuth2");
CkJsonObjectW_UpdateString(entry,L"username",L"personal-desktop");
tokens = CkJsonObjectW_Create();
CkJsonObjectW_UpdateString(tokens,L"refresh_token",refresh);
now = CkDateTimeW_Create();
CkDateTimeW_SetFromCurrentSystemTime(now);
CkJsonObjectW_UpdateString(tokens,L"authorized_at_utc",CkDateTimeW_getAsTimestamp(now,FALSE));
CkJsonObjectW_UpdateString(tokens,L"obtained_at_utc",CkDateTimeW_getAsTimestamp(now,FALSE));
CkJsonObjectW_UpdateString(tokens,L"access_token",access);
CkJsonObjectW_UpdateInt(tokens,L"expires_in",lifetime);
success = CkSecretsW_UpdateSecretJson(vault,entry,tokens);
if (success != TRUE) {
wprintf(L"Could not save the new credentials. Check the configured secret store.\n");
CkOAuth2W_Dispose(oauth);
CkJsonObjectW_Dispose(reply);
CkSecretsW_Dispose(vault);
CkJsonObjectW_Dispose(entry);
CkJsonObjectW_Dispose(tokens);
CkDateTimeW_Dispose(now);
return;
}
wprintf(L"Saved credentials. Access token lifetime in seconds: %d\n",lifetime);
CkOAuth2W_Dispose(oauth);
CkJsonObjectW_Dispose(reply);
CkSecretsW_Dispose(vault);
CkJsonObjectW_Dispose(entry);
CkJsonObjectW_Dispose(tokens);
CkDateTimeW_Dispose(now);
}