Esempio n. 1
0
    def login(self, username, password, mfa_code=None):
        """save and test login info for Robinhood accounts

        Args:
            username (str): username
            password (str): password

        Returns:
            (bool): received valid auth token

        """
        self.username = username
        self.password = password
        payload = {'password': self.password, 'username': self.username}

        if mfa_code:
            payload['mfa_code'] = mfa_code

        try:
            res = self.session.post(self.endpoints['login'], data=payload)
            res.raise_for_status()
            data = res.json()
        except requests.exceptions.HTTPError:
            raise RH_exception.LoginFailed()

        if 'mfa_required' in data.keys():  #pragma: no cover
            raise RH_exception.TwoFactorRequired(
            )  #requires a second call to enable 2FA

        if 'token' in data.keys():
            self.auth_token = data['token']
            self.headers['Authorization'] = 'Token ' + self.auth_token
            return True

        return False
Esempio n. 2
0
    def login(
            self,
            username,
            password,
            mfa_code=None
        ):
        """save and test login info for Robinhood accounts

        Args:
            username (str): username
            password (str): password

        Returns:
            (bool): received valid auth token

        """
        self.username = username
        self.password = password
        payload = {
            'password': self.password,
            'username': self.username,
            'scope': 'internal',
            'grant_type': 'password',
            'client_id': 'c82SH0WZOsabOXGP2sxqcj34FxkvfnWRZBKlBjFS',
            'expires_in': 86400
        }

        if mfa_code:
            payload['mfa_code'] = mfa_code

        try:
            res = self.session.post(
                self.endpoints['login'],
                data=payload
            )
            res.raise_for_status()
            data = res.json()
        except requests.exceptions.HTTPError:

            raise RH_exception.LoginFailed()

        if 'mfa_required' in data.keys():           #pragma: no cover
            raise RH_exception.TwoFactorRequired()  #requires a second call to enable 2FA

        if 'access_token' in data.keys():
            self.auth_token = data['access_token']
            self.headers['Authorization'] = 'Bearer ' + self.auth_token
            return True

        return False
    def login(self, username, password, mfa_code=None):

        self.username = username
        self.password = password

        # try getting access_token with new API
        l = r.login(username, password)
        if 'access_token' in l.keys():
            self.oauth_token = l['access_token']
            self.headers['Authorization'] = 'Bearer ' + self.oauth_token
            print(l)
            return True
        # /try getting access_token with new API

        payload = {
            'client_id': 'c82SH0WZOsabOXGP2sxqcj34FxkvfnWRZBKlBjFS',
            'expires_in': 86400,
            'grant_type': 'password',
            'password': self.password,
            'scope': 'internal',
            'username': self.username
        }

        if mfa_code:
            payload['mfa_code'] = mfa_code

        try:
            res = self.session.post('https://api.robinhood.com/oauth2/token/',
                                    data=payload,
                                    timeout=300)
            res.raise_for_status()
            data = res.json()
        except requests.exceptions.HTTPError:
            raise RH_exception.LoginFailed()

        if 'mfa_required' in data.keys():  # pragma: no cover
            raise RH_exception.TwoFactorRequired(
            )  # requires a second call to enable 2FA

        if 'access_token' in data.keys():
            self.oauth_token = data['access_token']
            self.headers['Authorization'] = 'Bearer ' + self.oauth_token
            return True

        return False