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 Objective-C Downloads
#import <CkoOAuth2.h>
#import <NSString.h>
#import <CkoJsonObject.h>
#import <CkoSecrets.h>
#import <CkoDateTime.h>
BOOL success = NO;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = NO;
CkoOAuth2 *oauth = [[CkoOAuth2 alloc] init];
oauth.AuthorizationEndpoint = @"https://api.schwabapi.com/v1/oauth/authorize";
oauth.TokenEndpoint = @"https://api.schwabapi.com/v1/oauth/token";
oauth.ClientId = @"APP_KEY";
oauth.ClientSecret = @"APP_SECRET";
oauth.UseBasicAuth = YES;
oauth.CodeChallenge = NO;
// The registered HTTPS callback forwards to this desktop's HTTP listener.
oauth.AppCallbackUrl = @"https://127.0.0.1:8182/";
oauth.LocalHost = @"127.0.0.1";
oauth.ListenPort = [NSNumber numberWithInt:3017];
NSString *url = [oauth StartAuth];
if (oauth.LastMethodSuccess != YES) {
NSLog(@"%@",@"Could not start authorization. Check the callback settings and free the listener port.");
return;
}
success = [oauth LaunchBrowser: url];
if (success != YES) {
[oauth Cancel];
NSLog(@"%@",@"Could not open the system browser.");
return;
}
// Allow time for sign-in, MFA, and account selection.
int waitedMs = 0;
while ((waitedMs < 300000) && ([oauth.AuthFlowState intValue] < 3)) {
[oauth SleepMs: [NSNumber numberWithInt: 100]];
waitedMs = waitedMs + 100;
}
int state = [oauth.AuthFlowState intValue];
if (state < 3) {
[oauth Cancel];
NSLog(@"%@",@"Authorization timed out. Check the HTTPS callback and try again.");
return;
}
if (state == 4) {
NSLog(@"%@",@"Authorization was declined.");
return;
}
if (state != 3) {
NSLog(@"%@",@"Authorization failed. Check app approval, credentials, and the exact callback URL.");
return;
}
CkoJsonObject *reply = [[CkoJsonObject alloc] init];
success = [reply Load: oauth.AccessTokenResponse];
if (success != YES) {
NSLog(@"%@",@"Unexpected token response.");
return;
}
if ([reply HasMember: @"access_token"] != YES) {
NSLog(@"%@",@"The token response contained no access token.");
return;
}
NSString *access = [reply StringOf: @"access_token"];
int lifetime = [[reply IntOf: @"expires_in"] intValue];
if ((access == @"") || (lifetime <= 0)) {
NSLog(@"%@",@"The token response lacks a usable access token or lifetime.");
return;
}
if ([reply HasMember: @"refresh_token"] != YES) {
NSLog(@"%@",@"No refresh token in the authorization response.");
return;
}
NSString *refresh = [reply StringOf: @"refresh_token"];
if (refresh == @"") {
NSLog(@"%@",@"The refresh token is empty.");
return;
}
CkoSecrets *vault = [[CkoSecrets alloc] init];
vault.Location = @"local_manager";
vault.AutoSplit = YES;
CkoJsonObject *entry = [[CkoJsonObject alloc] init];
[entry UpdateString: @"appName" value: @"SchwabTrader"];
[entry UpdateString: @"service" value: @"OAuth2"];
[entry UpdateString: @"username" value: @"personal-desktop"];
CkoJsonObject *tokens = [[CkoJsonObject alloc] init];
[tokens UpdateString: @"refresh_token" value: refresh];
CkoDateTime *now = [[CkoDateTime alloc] init];
[now SetFromCurrentSystemTime];
[tokens UpdateString: @"authorized_at_utc" value: [now GetAsTimestamp: NO]];
[tokens UpdateString: @"obtained_at_utc" value: [now GetAsTimestamp: NO]];
[tokens UpdateString: @"access_token" value: access];
[tokens UpdateInt: @"expires_in" value: [NSNumber numberWithInt: lifetime]];
success = [vault UpdateSecretJson: entry secret: tokens];
if (success != YES) {
NSLog(@"%@",@"Could not save the new credentials. Check the configured secret store.");
return;
}
NSLog(@"%@%d",@"Saved credentials. Access token lifetime in seconds: ",lifetime);