def login_oauth2(self, username, password, mfa_code=None): ''' Login using username and password ''' data = { "grant_type": "password", "scope": "internal", "client_id": CLIENT_ID, "expires_in": 86400, "password": password, "username": username } if mfa_code is not None: data['mfa_code'] = mfa_code url = "https://api.robinhood.com/oauth2/token/" res = self.post(url, payload=data, retry=False) if res is None: if mfa_code is None: raise AuthenticationError("Client.login_oauth2(). Could not authenticate. Check username and password.") else: raise AuthenticationError("Client.login_oauth2(). Could not authenticate. Check username and password, and enter a valid MFA code.") elif res.get('mfa_required') is True: raise AuthenticationError("Client.login_oauth2(). Could not authenticate. MFA is required.") self.access_token = res["access_token"] self.refresh_token = res["refresh_token"] self.mfa_code = res["mfa_code"] self.scope = res["scope"] self.__set_account_info() return self.authenticated
def __set_account_info(self): account_urls = Account.all_urls(self) if len(account_urls) > 1: raise NotImplementedError("fast_arrow 'currently' does not handle multiple account authentication.") elif len(account_urls) == 0: raise AuthenticationError("fast_arrow expected at least 1 account.") else: self.account_url = account_urls[0] self.account_id = get_last_path(self.account_url) self.authenticated = True
def login_oauth2(self, username, password, device_token=None, mfa_code=None, cur_path=None): ''' Login using username and password ''' data = { "grant_type": "password", "scope": "internal", "client_id": CLIENT_ID, "expires_in": 86400, "password": password, "username": username, "device_token": device_token } if mfa_code is not None: data['mfa_code'] = mfa_code url = "https://api.robinhood.com/oauth2/token/" res = self.post(url, payload=data, retry=False) if res is None: if mfa_code is None: msg = ("Client.login_oauth2(). Could not authenticate. Check " + "username and password.") raise AuthenticationError(msg) else: msg = ("Client.login_oauth2(). Could not authenticate. Check" + "username and password, and enter a valid MFA code.") raise AuthenticationError(msg) elif res.get('mfa_required') is True: msg = "Client.login_oauth2(). Couldn't authenticate. MFA required." raise AuthenticationError(msg) self.access_token = res["access_token"] self.refresh_token = res["refresh_token"] self.mfa_code = res["mfa_code"] self.scope = res["scope"] self.__set_account_info() if cur_path is not None: export_json ({ 'access_token': self.access_token, 'refresh_token': self.refresh_token, 'auth': 'Bearer ' + self.auth_token }, '../data/login_data.json', path=cur_path, indent=4) return self.authenticated
def logout_oauth2(self): ''' Logout for given Oauth2 bearer token ''' url = "https://api.robinhood.com/oauth2/revoke_token/" data = { "client_id": CLIENT_ID, "token": self.refresh_token, } res = self.post(url, payload=data) if res == None: self.account_id = None self.account_url = None self.access_token = None self.refresh_token = None self.mfa_code = None self.scope = None self.authenticated = False return True else: raise AuthenticationError("fast_arrow could not log out.")