robinhood-unofficial / pyrh

Python Framework to make trades with the unofficial Robinhood API
https://pyrh.readthedocs.io/en/latest/
MIT License
1.78k stars 603 forks source link

Login failures / troubles getting started #248

Closed JJSax closed 4 years ago

JJSax commented 4 years ago

Hi, I'm having difficulties getting started. My code is almost identical to your quickstart code, the only difference being I have my username and password in another file. After I put in the 2FA code that was sent to my phone, it gives me this error. If I intentionally put in the wrong password, it doesn't send me the code, so my login credentials are right.

Traceback (most recent call last):
  File "C:\Users\jauge\PycharmProjects\RHood\venv\lib\site-packages\pyrh\robinhood.py", line 215, in login
    res2.raise_for_status()
  File "C:\Users\jauge\PycharmProjects\RHood\venv\lib\site-packages\requests\models.py", line 941, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://api.robinhood.com/challenge//respond/
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2020.1\plugins\python-ce\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2020.1\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/jauge/PycharmProjects/RHood/main.py", line 8, in <module>
    rh.login(acct_config.USERNAME, acct_config.PASSWORD)
  File "C:\Users\jauge\PycharmProjects\RHood\venv\lib\site-packages\pyrh\robinhood.py", line 228, in login
    raise RH_exception.LoginFailed()
pyrh.exceptions.LoginFailed

I apologize if this isn't the best place to submit this. I just don't know what I'm doing wrong. Thank you for your time.

edit: I forgot to put in my code.

import acct_config
from pyrh import Robinhood

rh = Robinhood()
rh.login(username=acct_config.USERNAME, password=acct_config.PASSWORD)
rh.print_quote("AAPL")
samueleishion commented 4 years ago

@JJSax this error is based on the url the api is trying to call. notice on line 6, above, the url has 2 slashes: https://api.robinhood.com/challenge//respond/... it seems like it's not passing in a challenge_id on login: https://github.com/robinhood-unofficial/pyrh/blob/master/pyrh/urls.py#L64

Have you tried setting up 2FA from the robinhood app instead?

JJSax commented 4 years ago

Have you tried setting up 2FA from the robinhood app instead?

I actually set my whole account up with the app, including the 2FA. Do you think I should redo the 2FA on my app again?

7heaxe commented 4 years ago

Hello I figured this out; this issue tilted me as well.

You need to:

  1. Turn off SMS, etc. 2FA (when you turn this off, you will get email codes to verify log-ins).
  2. Start running the program code.
  3. Check your email, and copy and paste the email code into the terminal prompt and hit Enter.
JJSax commented 4 years ago

Hello I figured this out; this issue tilted me as well.

You need to:

  1. Turn off SMS, etc. 2FA (when you turn this off, you will get email codes to verify log-ins).
  2. Start running the program code.
  3. Check your email, and copy and paste the email code into the terminal prompt and hit Enter.

Thanks for the reply! I tried your steps, and I'm not getting the email code at all.

7heaxe commented 4 years ago

Thanks for the reply! I tried your steps, and I'm not getting the email code at all.

That is very strange. See this: https://robinhood.com/us/en/support/articles/verifying-its-you/

What if I’m not getting my verification emails?

Make sure the verification emails aren’t being routed to your spam folder. Make sure your email address is entered correctly. Here’s how you can update your email address.

By turning off 2FA, when you try to log on from a new device, or in this case, from your program when it tries to log you on, Robinhood should send you an email code.

samueleishion commented 4 years ago

@JJSax I think I just disabled it and re-enabled it for it to work. It may automatically set the authentication method to SMS once enabled. Do you get the "Input mfa code:" input message when you run your code? (https://github.com/robinhood-unofficial/pyrh/blob/master/pyrh/models/sessionmanager.py#L395)

JJSax commented 4 years ago

@JJSax I think I just disabled it and re-enabled it for it to work. It may automatically set the authentication method to SMS once enabled. Do you get the "Input mfa code:" input message when you run your code?

I do get that message each time. I tried toggling SMS off and back on, and I still get the error while using SMS. When I get it through email, it goes through as I would expect and prints out share price for AAPL.

I figured out why my email was having issues. I forgot I made a new folder for my investments in Gmail and that's where they were going. I checked spam and inbox thinking I was being smart. lol.

I do notice that it requires the 2FA code every time I run the code though. Is that normal? Is there any way to only have to do it once?

7heaxe commented 4 years ago

I do get that message each time. I tried toggling SMS off and back on, and I still get the error while using SMS. When I get it through email, it goes through as I would expect and prints out share price for AAPL.

I figured out why my email was having issues. I forgot I made a new folder for my investments in Gmail and that's where they were going. I checked spam and inbox thinking I was being smart. lol.

I do notice that it requires the 2FA code every time I run the code though. Is that normal? Is there any way to only have to do it once?

I think you need to keep SMS authentication and the likes off to continue getting email code notifications. Keeping it off means you will only get email codes and not SMS codes.

Logically, I think we do need to paste the email code every time we run it since the program is requesting access every time. This is honestly very annoying since I am dealing with my bot trading 1 stock company at a time, and if I want to automate trading with more than 1 company, I will need to paste the code n times for n companies.

JJSax commented 4 years ago

Logically, I think we do need to paste the email code every time we run it since the program is requesting access every time. This is honestly very annoying since I am dealing with my bot trading 1 stock company at a time, and if I want to automate trading with more than 1 company, I will need to paste the code n times for n companies.

I found a way to prevent having to put the code in several times. Robinhood was thinking the device was a new device each time because pyrh is generating a new device_token each time, and not saving your generated device token. I guarantee there is a better way to do it, and I cannot guarantee this method is a secure way of doing this. This worked for me and I don't have to do the 2FA each time. If anyone knows a better/more secure way let me know.

Add this print line to get your device_token

if self.device_token == "": #this is around line 146
    self.GenerateDeviceToken()
    print(self.device_token) #you only need this once

After that go to line 65 or so and make your device_token what it prints out.

self.session.headers = self.headers
self.device_token = "" #put the string that prints out here.  I redacted mine for my security.
self.challenge_id = ""
stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] commented 4 years ago

Closing this issue automatically because it has not had any activity since it has been marked as stale. If you think it is still relevant and should be addressed, feel free to open a new one.