Sample code for 30+ languages & platforms
Objective-C Requires Chilkat v11.0.0+

Schwab: List authorized accounts and select one

See more Schwab Examples

Lists masked account numbers. Run once to see their endings, then enter the last four digits of the intended account. A unique match saves Schwab’s hashValue in a separate secret for the account-specific examples. If two accounts share an ending, use the full-account input from your account picker. A failed match leaves the previous selection in place.

Use the access token saved by desktop authorization. Use the same Chilkat Secrets store as the OAuth example.

Schwab API specification

Chilkat Objective-C Downloads

Objective-C
#import <CkoSecrets.h>
#import <CkoJsonObject.h>
#import <NSString.h>
#import <CkoHttp.h>
#import <CkoHttpResponse.h>
#import <CkoJsonArray.h>
#import <CkoStringBuilder.h>

BOOL success = NO;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
success = NO;

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];
success = [vault GetSecretJson: entry jsonOut: tokens];
if (success != YES) {
    NSLog(@"%@",@"Could not read the token record. Check the store and complete desktop authorization first.");
    return;
}

NSString *accessToken = [tokens StringOf: @"access_token"];
if (accessToken == @"") {
    NSLog(@"%@",@"Run the Schwab authorization or refresh example to save an access token.");
    return;
}

CkoHttp *http = [[CkoHttp alloc] init];
http.RequireSslCertVerify = YES;
http.AuthToken = accessToken;

//  Supply one of these from your account picker. Leave both empty to list only.
NSString *selectedEnding = @"";
NSString *selectedFullNumber = @"";
CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
success = [http HttpNoBody: @"GET" url: @"https://api.schwabapi.com/trader/v1/accounts/accountNumbers" response: resp];
if ((success != YES) || ([resp.StatusCode intValue] != 200)) {
    if ([resp.StatusCode intValue] == 0) {
        NSLog(@"%@",@"No HTTP response. Check connectivity.");
    }
    else {
        NSLog(@"%@%d",@"Schwab returned HTTP ",[resp.StatusCode intValue]);
        if ([resp.StatusCode intValue] == 401) {
            NSLog(@"%@",@"Refresh the access token or repeat desktop authorization.");
        }

    }

    return;
}

CkoJsonArray *accounts = [[CkoJsonArray alloc] init];
success = [accounts Load: resp.BodyStr];
if (success != YES) {
    NSLog(@"%@",@"Schwab returned unexpected JSON.");
    return;
}

CkoJsonObject *row = [[CkoJsonObject alloc] init];
CkoStringBuilder *number = [[CkoStringBuilder alloc] init];
int matches = 0;
int i = 0;
NSString *selectedHash = @"";
while ((i < [accounts.Size intValue])) {
    [accounts ObjectAt2: [NSNumber numberWithInt: i] jsonObj: row];
    NSString *accountNumber = [row StringOf: @"accountNumber"];
    [number SetString: accountNumber];
    if ([number.Length intValue] >= 4) {
        int start = [number.Length intValue] - 4;
        NSString *ending = [number GetRange: [NSNumber numberWithInt: start] numChars: [NSNumber numberWithInt: 4] removeFlag: NO];
        NSLog(@"%@%@",@"Account ending in ",ending);
        BOOL matched = NO;
        if (selectedFullNumber != @"") {
            if (accountNumber == selectedFullNumber) {
                matched = YES;
            }

        }
        else {
            if (ending == selectedEnding) {
                matched = YES;
            }

        }

        if (matched == YES) {
            matches = matches + 1;
            selectedHash = [row StringOf: @"hashValue"];
        }

    }

    i = i + 1;
}

if ((matches != 1) || (selectedHash == @"")) {
    NSLog(@"%@",@"Select exactly one account. For duplicate endings use the full-account input.");
    return;
}

[entry UpdateString: @"service" value: @"SelectedAccount"];
CkoJsonObject *selection = [[CkoJsonObject alloc] init];
[selection UpdateString: @"hashValue" value: selectedHash];
success = [vault UpdateSecretJson: entry secret: selection];
if (success != YES) {
    NSLog(@"%@",@"Could not save the selected account.");
    return;
}

NSLog(@"%@",@"Selected account saved.");