joshfraser / robinhood-to-csv

Python script to export Robinhood trades to a CSV file
MIT License
259 stars 109 forks source link

Fix blank slate crash #42

Closed martinmelin closed 5 years ago

martinmelin commented 5 years ago

First off, thanks for putting this script together! Will save me a lot of time.

When running csv-export.py without any prior configuration, I got a KeyError:

$ python csv-export.py
Traceback (most recent call last):
  File "csv-export.py", line 53, in <module>
    if logged_in != True and logged_in.get('non_field_errors') == None and logged_in['mfa_required'] == True:
KeyError: 'mfa_required'

This is because os.getenv returns None as a default value, overwriting the "" value we had from argparse, meaning we never get to the interactive username/password prompt and instead call robinhood.login with None as username and password. login returns the Robinhood response to this which is:

{u'detail': u'Unable to log in with provided credentials.'}

which in turn leads us to throw a KeyError because we assume there is a mfa_required key if there are no non_field_errors in the RH response.

This change ensures that we prompt for username/password when they're not provided in arguments or envvars, and stops the KeyError from occurring when the RH response doesn't contain a mfa_required key.