Comments (11)
I just added the requirements.txt
file so you should be able to install the dependencies that are required in the library. I would recommend that if you currently have the old TD library install on your system, that you either uninstall it or create virtual environment that you can control which packages are there.
from td-ameritrade-api.
The account_number
is just your TD Ameritrade account number that you see when you login to your account. Naturally, if you want to access account information, you'll need your TD Ameritrade account number. So any of the endpoints where you need to provide the account number, you need to pass it through.
from td-ameritrade-api.
Their api will let you list the accounts you have along with their IDs.
As to the manual login, it will be required the first time and then once every 90 days unless you refresh the refresh_token.
First you generate an authorization code that is routed to your redirect URI.
Then you use that auth code to generate a refresh token.
Then you use that refresh token to generate an access token (bearer). Access tokens authenticate the calls and expire in 30 minutes.
Refresh tokens last 90 days and can be refreshed without a manual login. More than that is blocked by a defect in oauth2 itself not this library.
You can blame corporate architects padding their resumes with security theatre at the expense of design coherency and user experience for that. Can't be fixed unless they drop oAuth on the server side.
Sorry for the frequent edits; I am drunk.
from td-ameritrade-api.
Hi areed1192, I removed the tdameritrade
module and I did it from the beginning:
pip install -r requirements.txt
pip install .
everything went fine and then I tried:
python sample_code.py
(sample_code.py is the script you posted in the README.md into the Usage section)
Traceback (most recent call last):
File "sample_code.py", line 14, in <module>
client_id = config.get('main', 'client_id')
File ".pyenv/versions/3.8.8/lib/python3.8/configparser.py", line 781, in get
d = self._unify_values(section, vars)
File ".pyenv/versions/3.8.8/lib/python3.8/configparser.py", line 1149, in _unify_values
raise NoSectionError(section) from None
configparser.NoSectionError: No section: 'main'
Any idea? This is my:
$ pip freeze
certifi==2020.12.5
cffi==1.14.5
chardet==3.0.4
click==7.1.2
cryptography==3.4.7
dataclasses==0.6
Flask==1.1.2
idna==2.10
itsdangerous==1.1.0
Jinja2==2.11.3
MarkupSafe==1.1.1
oauthlib==3.1.0
pycparser==2.20
pyOpenSSL==20.0.1
requests==2.24.0
requests-oauthlib==1.3.0
six==1.15.0
td-ameritrade-api @ file:///.../TDAmeritrade_developer_acct/API_TD_Ameritrade_sigma_coding/td-ameritrade-api-master
urllib3==1.25.11
websockets==8.1
Werkzeug==1.0.1
from td-ameritrade-api.
The issue is you don't have a config file that stores all your credentials like your client ID and redirect URI. Here's an example of how to create a config file.
from configparser import ConfigParser
# Initialize the Parser.
config = ConfigParser()
# Add the Section.
config.add_section('main')
# Set the Values.
config.set('main', 'client_id', 'YOUR_CLIENT_ID')
config.set('main', 'redirect_uri', 'YOUR_REDIRECT_URI')
config.set('main', 'account_number', 'YOUR_ACCOUNT_NUMBER')
# Write the file.
with open(file='config/config.ini', mode='w+') as f:
config.write(f)
This will create a config.ini
file in a folder called config
if it exists. You need to make sure that the config folder exists before running this code.
from td-ameritrade-api.
from td-ameritrade-api.
All right @areed1192 . So it seems that one will have to log in manually since there is no relation between the ACCOUNT_NUMBER and the CONSUMER_KEY. Do you know if there is there any way to pass credentials automatically.
Otherwise one cannot have a fully automatic tradingbot working against TD ameritrade.
from td-ameritrade-api.
@alelasantillan Just to reiterate the same topic, the oAuth
process must be done every 90 days regardless of if we want to or not. Now there are ways to automate it using selenium, but I will not be including that functionality into the library since selenium requires different versions of the driver depending on the browser.
Also, I might be speaking to early, but I think something you could try is to refresh the refresh_token
before the 90-day expiration date and see if that allows you to bypass the full oAuth
process.
from td-ameritrade-api.
Thank you @areed1192 and @cmpunches!
Now I've created the config dir, and run the create_config code above. It created the config/config.ini files which contains:
1 [main]
2 client_id = YOUR_CLIENT_ID
3 redirect_uri = YOUR_REDIRECT_URI
4 account_number = YOUR_ACCOUNT_NUMBER
But when I run:
python sample_code.py
(sample_code.py is the script you posted in the README.md into the Usage section)
I get:
with open(file=file_path, mode='r') as token_file:
FileNotFoundError: [Errno 2] No such file or directory: 'config/td_credentials.json'
So I renamed the config.ini file to td_credentials.json and, logically, the script chimes about the non-jason format.
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 2 (char 1)
So I changed the format to read:
1 {
2 "main":[
3 {
4 "client_id" : "YOUR_CLIENT_ID",
5 "redirect_uri" : "YOUR_REDIRECT_URI",
6 "account_number" : "YOUR_ACCOUNT_NUMBER"
7 }
8 ]
9 }
And replaced the client_id by the TD's consumer key, the http://localhost and the tdameritrade.com acct number and I run:
python sample_code.py
And I get (in terminal):
Refresh Token Expired, initiating oAuth workflow.
Please Paste the Authorization Code Here:
And also a blank window in my web browser opened with this address:
https://auth.tdameritrade.com/%27http://localhost'?error=invalid_client&error_description=invalid_client
Any idea what this authorization code is?
I've tried with the main account password, with the developer account password, with the consumer key unsuccessfully.
I've also tried to pass the refresh_token that expires in 90 days counting from today. But I've got this error:
Please Paste the Authorization Code Here: code='unWG5t55....'
line 459, in grab_authorization_code
self.authorization_code = parse_code['code'][0]
KeyError: 'code'
And also I've tried without the words code='
with same results.
from td-ameritrade-api.
Can you run the code in this file. It should run the oAuth process for you, let me know if you run into any issues with this file. Also, remember to check the file paths and that they all match.
from td-ameritrade-api.
Hi @areed1192 !!!
thank you very much. I was able to get the refresh and auth tokens. I post them below.
I am now wondering if all this json output should be put in the credential_file='config/td_credentials.json' as is, or if only access_token and refresh_token needs to be included.
Also I wonder if case of access_token and resfresh_token should be changed to uppercase since it seems that the file I have contains (real codes has been changed by gibberish):
{"access_token": "<ACCESS_TOKEN>",
"refresh_token": "<REFRESH_TOKEN>",
"scope": "PlaceTrades AccountAccess MoveMoney",
"expires_in": 1800,
"refresh_token_expires_in": 7776000,
"token_type": "Bearer"
}
Using these contents in file config/td_credentials.json I've got this error when running the sample_code.py (it contains the code copied from the README.md of https://github.com/areed1192/td-ameritrade-api/):
python sample_code.py
Traceback (most recent call last):
File "sample_code.py", line 25, in <module>
td_client = TdAmeritradeClient(
File ".../td-ameritrade-api-master/td/client.py", line 26, in __init__
self.td_session = TdAmeritradeSession(td_client=self)
File ".../td-ameritrade-api-master/td/session.py", line 44, in __init__
logging.basicConfig(
File "/Users/alejandrosantillaniturres/.pyenv/versions/3.8.8/lib/python3.8/logging/__init__.py", line 2005, in basicConfig
raise ValueError('Unrecognised argument(s): %s' % keys)
ValueError: Unrecognised argument(s): encoding
While using this json into the td_credentials.json:
1 {
2 "main":[
3 {
4 "client_id" : "<CLIENT_ID>",
5 "redirect_uri" : "http://localhost",
6 "account_number" : "<ACCOUNT_NUMBER?>"
7 }
8 ]
9 }
I've got this message:
python sample_code.py
Refresh Token Expired, initiating oAuth workflow.
Please Paste the Authorization Code Here:
Can you post the correct format of the td_credentials.json file?
from td-ameritrade-api.
Related Issues (18)
- Not saving new credentials file to path when generating new refresh_token HOT 1
- start up issue HOT 1
- options strangle orders with trigger? and should I just switch to a better api broker like IB or Etrade? HOT 1
- pip install failed HOT 1
- order_service.place_order() does not return any reponse HOT 1
- configparser.NoSectionError: No section: 'main' HOT 2
- exchange_code_for_token HOT 22
- KeyError: 'code' after I paste the authorization code HOT 1
- http exception when getting access_token with refresh_token
- A Few Suggestions
- start_date of get_price_history does not actually change anything HOT 5
- Session HTTPError
- Error with Old HTTP: closewrite error
- why do screeshots refer to "sigma coding" having access to account in Step 4 - Accept the Terms: HOT 1
- Transition to Schwab's Trader API HOT 5
- Unable to pass in Enum to get_market_hours in rest/market_hours.py HOT 3
- cannot import name 'TDClient' from 'td.client' HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from td-ameritrade-api.