jessecooper / pyetrade

Python E-Trade API Wrapper
GNU General Public License v3.0
209 stars 97 forks source link

How can I use pytrade in a script? #16

Closed andronat closed 4 years ago

andronat commented 4 years ago

I'm currently following the example on the README file here but when I rerun my script with my newly acquired verifier_code, I get an error:

Traceback (most recent call last):
  File "bot.py", line 11, in <module>
    tokens = oauth.get_access_token(verifier_code)
  File "/usr/local/lib/python3.7/site-packages/pyetrade/authorization.py", line 146, in get_access_token
    self.access_token = self.session.fetch_access_token(self.access_token_url)
  File "/Users/tasos/Library/Python/3.7/lib/python/site-packages/requests_oauthlib/oauth1_session.py", line 320, in fetch_access_token
    token = self._fetch_token(url, **request_kwargs)
  File "/Users/tasos/Library/Python/3.7/lib/python/site-packages/requests_oauthlib/oauth1_session.py", line 368, in _fetch_token
    raise TokenRequestDenied(error % (r.status_code, r.text), r)
requests_oauthlib.oauth1_session.TokenRequestDenied: Token request failed with code 401, response was '<html><head><title> - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 401 - oauth_problem=token_rejected</h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u>oauth_problem=token_rejected</u></p><p><b>description</b> <u>This request requires HTTP authentication (oauth_problem=token_rejected).</u></p><HR size="1" noshade="noshade"><h3></h3></body></html>'.

I also tried to run the following skipping the oauth.get_request_token() but setting verifier_code from what I got online in a previous execution:

consumer_key = "<my_key>"
consumer_secret = "<my_secret>"
verifier_code = "<what I got from a previous execution>"

oauth = pyetrade.ETradeOAuth(consumer_key, consumer_secret)
# oauth.get_request_token()
# Follow url and get verification code

tokens = oauth.get_access_token(verifier_code)
accounts = pyetrade.ETradeAccounts(
    consumer_key,
    consumer_secret,
    tokens['oauth_token'],
    tokens['oauth_token_secret']
)
accounts.list_accounts()

but I got the following error:

Traceback (most recent call last):
  File "bot.py", line 11, in <module>
    tokens = oauth.get_access_token(verifier_code)
  File "/usr/local/lib/python3.7/site-packages/pyetrade/authorization.py", line 144, in get_access_token
    self.session._client.client.verifier = verifier
AttributeError: 'ETradeOAuth' object has no attribute 'session'

Any suggestions on how can I make this work as a script?

jessecooper commented 4 years ago

Please refer to this past issue and see if this fixes it: https://github.com/jessecooper/pyetrade/issues/4 It seems similar