Today we will test the API GetAccount:
https://developer.tdameritrade.com/account-access/apis/get/accounts/%7BaccountId%7D-0
Here are the test codes:
get_auth.py
import urllib.parse
client_id = "XXX"
print("client_id", client_id + "@AMER.OAUTHAP")
print("")
base_url = "https://auth.tdameritrade.com/auth?response_type=code&redirect_uri={redirect_uri}&client_id={client_id}%40AMER.OAUTHAP"
redirect_uri = "https://td.miaowa.pro/getauth.html"
redirect_uri = urllib.parse.quote(redirect_uri)
# step 1: the the auth_url
# this url works ok
print("STEP 1: click the url to authenticate")
print(base_url.format(redirect_uri=redirect_uri, client_id=client_id))
print("")
# step 2: get the request token
# after authentication with you TD account
redirect_url = """
https://td.miaowa.pro/getauth.html?code=XXX
"""
code = urllib.parse.unquote(redirect_url[redirect_url.find("code=") + 5:])
print("STEP 2: copy request token below")
print(code)
# step 3: get the access token
get_access_token_url = "https://developer.tdameritrade.com/authentication/apis/post/token-0"
print("STEP 3: parse it then click the following url to get access token")
print(get_access_token_url)
print("")
# step 4: copy response from page https://developer.tdameritrade.com/authentication/apis/post/token-0
get_auth_resp = {
"access_token": "XXX",
"refresh_token": "XXX",
"scope": "PlaceTrades AccountAccess MoveMoney",
"expires_in": 1800,
"refresh_token_expires_in": 7776000,
"token_type": "Bearer"
}
print("STEP 4: copy authorization value for test apis on https://developer.tdameritrade.com/account-access/apis")
print("Bearer", urllib.parse.unquote(get_auth_resp["access_token"]))
get_account.py
import requests
get_auth_resp = {
"access_token": "XXX",
"refresh_token": "XXX",
"scope": "PlaceTrades AccountAccess MoveMoney",
"expires_in": 1800,
"refresh_token_expires_in": 7776000,
"token_type": "Bearer"
}
access_token = get_auth_resp["access_token"]
get_account_url = "https://api.tdameritrade.com/v1/accounts/XXX"
headers = {
"Accept": "*/*",
"Accept-Encoding": "gzip",
"Host": "api.tdameritrade.com",
"User-Agent": "client.miaowa.pro",
"Authorization": "Bearer " + access_token
}
print(headers)
get_account_resp = requests.get(get_account_url, headers=headers)
print(get_account_resp.status_code)
print(get_account_resp.text)
Great! we got the result of get_account.py
200
{
"securitiesAccount" : {
"type" : "MARGIN",
"accountId" : "XXX",
"roundTrips" : 0,
"isDayTrader" : false,
"isClosingOnlyRestricted" : false,
"initialBalances" : {
"accruedInterest" : 0.0,
"availableFundsNonMarginableTrade" : 2149.95,
"bondValue" : 0.0,
"buyingPower" : 4299.9,
"cashBalance" : 2149.95,
"cashAvailableForTrading" : 0.0,
"cashReceipts" : 0.0,
"dayTradingBuyingPower" : 0.0,
"dayTradingBuyingPowerCall" : 0.0,
"dayTradingEquityCall" : 0.0,
"equity" : 2149.95,
"equityPercentage" : 100.0,
"liquidationValue" : 2149.95,
"longMarginValue" : 0.0,
"longOptionMarketValue" : 0.0,
"longStockValue" : 0.0,
"maintenanceCall" : 0.0,
"maintenanceRequirement" : 0.0,
"margin" : 2149.95,
"marginEquity" : 2149.95,
"moneyMarketFund" : 0.0,
"mutualFundValue" : 0.0,
"regTCall" : 0.0,
"shortMarginValue" : 0.0,
"shortOptionMarketValue" : 0.0,
"shortStockValue" : 0.0,
"totalCash" : 2149.95,
"isInCall" : false,
"pendingDeposits" : 0.0,
"marginBalance" : 0.0,
"shortBalance" : 0.0,
"accountValue" : 2149.95
},
"currentBalances" : {
"accruedInterest" : 0.0,
"cashBalance" : 2149.95,
"cashReceipts" : 0.0,
"longOptionMarketValue" : 0.0,
"liquidationValue" : 2149.95,
"longMarketValue" : 0.0,
"moneyMarketFund" : 0.0,
"savings" : 0.0,
"shortMarketValue" : 0.0,
"pendingDeposits" : 0.0,
"availableFunds" : 2149.95,
"availableFundsNonMarginableTrade" : 2149.95,
"buyingPower" : 4299.9,
"buyingPowerNonMarginableTrade" : 2149.95,
"dayTradingBuyingPower" : 0.0,
"equity" : 2149.95,
"equityPercentage" : 100.0,
"longMarginValue" : 0.0,
"maintenanceCall" : 0.0,
"maintenanceRequirement" : 0.0,
"marginBalance" : 0.0,
"regTCall" : 0.0,
"shortBalance" : 0.0,
"shortMarginValue" : 0.0,
"shortOptionMarketValue" : 0.0,
"sma" : 2149.95,
"mutualFundValue" : 0.0,
"bondValue" : 0.0
},
"projectedBalances" : {
"availableFunds" : 2149.95,
"availableFundsNonMarginableTrade" : 2149.95,
"buyingPower" : 4299.9,
"dayTradingBuyingPower" : 0.0,
"dayTradingBuyingPowerCall" : 0.0,
"maintenanceCall" : 0.0,
"regTCall" : 0.0,
"isInCall" : false,
"stockBuyingPower" : 4299.9
}
}
}