Go Requires Chilkat v11.0.0+
Go
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.
Chilkat Go Downloads
success := false
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false
vault := chilkat.NewSecrets()
vault.SetLocation("local_manager")
vault.SetAutoSplit(true)
entry := chilkat.NewJsonObject()
entry.UpdateString("appName","SchwabTrader")
entry.UpdateString("service","OAuth2")
entry.UpdateString("username","personal-desktop")
tokens := chilkat.NewJsonObject()
success = vault.GetSecretJson(entry,tokens)
if success != true {
fmt.Println("Could not read the token record. Check the store and complete desktop authorization first.")
vault.DisposeSecrets()
entry.DisposeJsonObject()
tokens.DisposeJsonObject()
return
}
accessToken := tokens.StringOf("access_token")
if *accessToken == "" {
fmt.Println("Run the Schwab authorization or refresh example to save an access token.")
vault.DisposeSecrets()
entry.DisposeJsonObject()
tokens.DisposeJsonObject()
return
}
http := chilkat.NewHttp()
http.SetRequireSslCertVerify(true)
http.SetAuthToken(*accessToken)
// Supply one of these from your account picker. Leave both empty to list only.
selectedEnding := ""
selectedFullNumber := ""
resp := chilkat.NewHttpResponse()
success = http.HttpNoBody("GET","https://api.schwabapi.com/trader/v1/accounts/accountNumbers",resp)
if (success != true) || (resp.StatusCode() != 200) {
if resp.StatusCode() == 0 {
fmt.Println("No HTTP response. Check connectivity.")
} else {
fmt.Println("Schwab returned HTTP ", resp.StatusCode())
if resp.StatusCode() == 401 {
fmt.Println("Refresh the access token or repeat desktop authorization.")
}
}
vault.DisposeSecrets()
entry.DisposeJsonObject()
tokens.DisposeJsonObject()
http.DisposeHttp()
resp.DisposeHttpResponse()
return
}
accounts := chilkat.NewJsonArray()
success = accounts.Load(resp.BodyStr())
if success != true {
fmt.Println("Schwab returned unexpected JSON.")
vault.DisposeSecrets()
entry.DisposeJsonObject()
tokens.DisposeJsonObject()
http.DisposeHttp()
resp.DisposeHttpResponse()
accounts.DisposeJsonArray()
return
}
row := chilkat.NewJsonObject()
number := chilkat.NewStringBuilder()
matches := 0
i := 0
var selectedHash *string = new(string)
*selectedHash = ""
for (i < accounts.Size()) {
accounts.ObjectAt2(i,row)
accountNumber := row.StringOf("accountNumber")
number.SetString(*accountNumber)
if number.Length() >= 4 {
start := number.Length() - 4
ending := number.GetRange(start,4,false)
fmt.Println("Account ending in ", *ending)
matched := false
if selectedFullNumber != "" {
if *accountNumber == selectedFullNumber {
matched = true
}
} else {
if *ending == selectedEnding {
matched = true
}
}
if matched == true {
matches = matches + 1
selectedHash = row.StringOf("hashValue")
}
}
i = i + 1
}
if (matches != 1) || (*selectedHash == "") {
fmt.Println("Select exactly one account. For duplicate endings use the full-account input.")
vault.DisposeSecrets()
entry.DisposeJsonObject()
tokens.DisposeJsonObject()
http.DisposeHttp()
resp.DisposeHttpResponse()
accounts.DisposeJsonArray()
row.DisposeJsonObject()
number.DisposeStringBuilder()
return
}
entry.UpdateString("service","SelectedAccount")
selection := chilkat.NewJsonObject()
selection.UpdateString("hashValue",*selectedHash)
success = vault.UpdateSecretJson(entry,selection)
if success != true {
fmt.Println("Could not save the selected account.")
vault.DisposeSecrets()
entry.DisposeJsonObject()
tokens.DisposeJsonObject()
http.DisposeHttp()
resp.DisposeHttpResponse()
accounts.DisposeJsonArray()
row.DisposeJsonObject()
number.DisposeStringBuilder()
selection.DisposeJsonObject()
return
}
fmt.Println("Selected account saved.")
vault.DisposeSecrets()
entry.DisposeJsonObject()
tokens.DisposeJsonObject()
http.DisposeHttp()
resp.DisposeHttpResponse()
accounts.DisposeJsonArray()
row.DisposeJsonObject()
number.DisposeStringBuilder()
selection.DisposeJsonObject()