Skip to content

Commit

Permalink
working save / auto-refresh of access token
Browse files Browse the repository at this point in the history
  • Loading branch information
anilshanbhag committed May 25, 2019
1 parent c877332 commit d5f3b2d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
7 changes: 3 additions & 4 deletions Robinhood/Robinhood.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,16 @@ def relogin_oauth2(self):
'''
url = "https://api.robinhood.com/oauth2/token/"
data = {
"client_id": self.client_id,
"device_token": self.device_token,
"grant_type": "refresh_token",
"refresh_token": self.refresh_token,
"scope": "internal",
"client_id": self.client_id,
"expires_in": 86400,
}
print(data)
res = self.session.post(url, data=data)
print(res)
res = res.json()
self.access_token = res["access_token"]
self.auth_token = res["access_token"]
self.refresh_token = res["refresh_token"]
self.mfa_code = res["mfa_code"]
self.scope = res["scope"]
Expand Down
15 changes: 10 additions & 5 deletions shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ class RobinhoodShell(cmd.Cmd):
# List of stocks in watchlist
watchlist = []

def _save_auth_data(self):
auth_data = {}
auth_data['device_token'] = self.trader.device_token
auth_data['auth_token'] = self.trader.auth_token
auth_data['refresh_token'] = self.trader.refresh_token
open(self.auth_file, 'w').write(json.dumps(auth_data))

def __init__(self):
cmd.Cmd.__init__(self)
self.trader = Robinhood()
Expand All @@ -60,6 +67,8 @@ def __init__(self):
self.trader.device_token = auth_data['device_token']
self.trader.auth_token = auth_data['auth_token']
self.trader.refresh_token = auth_data['refresh_token']
self.trader.relogin_oauth2()
self._save_auth_data()
self.trader.headers['Authorization'] = 'Bearer ' + self.trader.auth_token
except:
challenge_type = 'email'
Expand Down Expand Up @@ -529,11 +538,7 @@ def do_qq(self, arg):
def do_bye(self, arg):
open(self.instruments_cache_file, 'w').write(json.dumps(self.instruments_cache))
open(self.watchlist_file, 'w').write(json.dumps(self.watchlist))
auth_data = {}
auth_data['device_token'] = self.trader.device_token
auth_data['auth_token'] = self.trader.auth_token
auth_data['refresh_token'] = self.trader.refresh_token
open(self.auth_file, 'w').write(json.dumps(auth_data))
self._save_auth_data()
return True

# ------ utils --------
Expand Down

0 comments on commit d5f3b2d

Please sign in to comment.