ValvePython / steam

☁️ Python package for interacting with Steam
http://steam.readthedocs.io
MIT License
1.11k stars 148 forks source link

SteamAuthenticator throwing "Failed to get a web session" when trying to add 2FA to account #146

Closed Impulse-PW closed 6 years ago

Impulse-PW commented 6 years ago

Here's the code that I have to attempt this:

from steam import SteamClient
from steam.enums.emsg import EMsg
from steam.guard import SteamAuthenticator

#Create our steamclient instance
client = SteamClient()

@client.on("logged_on")
def handle_after_logon():
    print("You are now Logged in.")
    #Setup Autenticator for our steamclient instance "client"
    sa = SteamAuthenticator(medium=client)
    print(sa.has_phone_number())
    sa.add_phone_number("myphone with area code")
    sa.add()    # SMS code will be send to account's phone number
    sa.secrets  # dict with authenticator secrets, make sure you save them
    #We're gonna need these
    print(sa.secrets)
    sa.finalize(str(input("SMS CODE: ")))  # activate the authenticator
    sa.get_code()  # generate 2FA code for login
    sa.remove()  # removes the authenticator from the account

try:
    #Login to our steamclient instance
    client.cli_login("myusername","mypassword")

    #client.on("loggon_on") doesn't trigger without this
    client.run_forever()

#Allow us to logout using keyboard interrupt
except KeyboardInterrupt:
    if client.connected:
        client.logout()

Which in turn throws:

Traceback (most recent call last):
  File "C:\Users\me\AppData\Local\Programs\Python\Python36-32\lib\site-packages\gevent\greenlet.py", line 536, in run
    result = self._run(*self.args, **self.kwargs)
  File "C:\Users\me\steamguard\testing.py", line 17, in handle_after_logon
    print(sa.has_phone_number())
  File "C:\Users\me\AppData\Local\Programs\Python\Python36-32\lib\site-packages\steam\guard.py", line 330, in has_phone_number
    sess = self._get_web_session()
  File "C:\Users\me\AppData\Local\Programs\Python\Python36-32\lib\site-packages\steam\guard.py", line 263, in _get_web_session
    raise RuntimeError("Failed to get a web session. Try again in a few minutes")
RuntimeError: Failed to get a web session. Try again in a few minutes
Fri Mar 30 16:37:28 2018 <Greenlet at 0x70e3580: handle_after_logon> failed with RuntimeError

I believe this is a bug, I've read documentation best I can, I hope it's not a silly mistake by me.

rossengeorgiev commented 6 years ago

I wouldn't structure the code like that, but it should work. I suspect the client instance is not connected when it tried to request a web session. There is not enough info to tell for sure. It would be to enable logging messages and see whats going on.

Impulse-PW commented 6 years ago

Haha yes, I'm aware that the code's kinda weird. I've been playing with it for a while and took some weird turns.

I tracked my way through the code and it looks like steam\client\builtins\web.py is returning None on line 77 where it says:

if cookies is None:
    return None 

I checked the code in get_web_session_cookies() and it turns out it's throwing this:

get_web_session_cookies error: maximum recursion depth exceeded while calling a Python object

This happens on line 48 where it says

resp = webapi.post('ISteamUserAuth', 'AuthenticateUser', 1, params=data)

Impulse-PW commented 6 years ago

Here's the traceback if you'd like to have a look:

Traceback (most recent call last):
  File "C:\Users\piero\AppData\Local\Programs\Python\Python36-32\lib\site-packages\gevent\greenlet.py", line 536, in run
    result = self._run(*self.args, **self.kwargs)
  File "C:\Users\piero\Desktop\programming\steamguard\steampython.py", line 28, in handle_after_logon
    print(sa.has_phone_number())
  File "C:\Users\piero\AppData\Local\Programs\Python\Python36-32\lib\site-packages\steam\guard.py", line 331, in has_phone_number
    sess = self._get_web_session()
  File "C:\Users\piero\AppData\Local\Programs\Python\Python36-32\lib\site-packages\steam\guard.py", line 260, in _get_web_session
    sess = self.medium.get_web_session()
  File "C:\Users\piero\AppData\Local\Programs\Python\Python36-32\lib\site-packages\steam\client\builtins\web.py", line 81, in get_web_session
    cookies = self.get_web_session_cookies()
  File "C:\Users\piero\AppData\Local\Programs\Python\Python36-32\lib\site-packages\steam\client\builtins\web.py", line 53, in get_web_session_cookies
    resp = webapi.post('ISteamUserAuth', 'AuthenticateUser', 1, params=data)
  File "C:\Users\piero\AppData\Local\Programs\Python\Python36-32\lib\site-packages\steam\webapi.py", line 494, in post
    return webapi_request(url, 'POST', caller=caller, session=session, params=params)
  File "C:\Users\piero\AppData\Local\Programs\Python\Python36-32\lib\site-packages\steam\webapi.py", line 426, in webapi_request
    resp = f(url, stream=False, timeout=onetime['http_timeout'], **kwargs)
  File "C:\Users\piero\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\sessions.py", line 555, in post
    return self.request('POST', url, data=data, json=json, **kwargs)
  File "C:\Users\piero\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\sessions.py", line 508, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\piero\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\sessions.py", line 618, in send
    r = adapter.send(request, **kwargs)
  File "C:\Users\piero\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\adapters.py", line 440, in send
    timeout=timeout
  File "C:\Users\piero\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connectionpool.py", line 601, in urlopen
    chunked=chunked)
  File "C:\Users\piero\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connectionpool.py", line 346, in _make_request
    self._validate_conn(conn)
  File "C:\Users\piero\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connectionpool.py", line 850, in _validate_conn
    conn.connect()
  File "C:\Users\piero\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connection.py", line 314, in connect
    cert_reqs=resolve_cert_reqs(self.cert_reqs),
  File "C:\Users\piero\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\util\ssl_.py", line 269, in create_urllib3_context
    context.options |= options
  File "C:\Users\piero\AppData\Local\Programs\Python\Python36-32\lib\ssl.py", line 459, in options
    super(SSLContext, SSLContext).options.__set__(self, value)
  File "C:\Users\piero\AppData\Local\Programs\Python\Python36-32\lib\ssl.py", line 459, in options
    super(SSLContext, SSLContext).options.__set__(self, value)
  File "C:\Users\piero\AppData\Local\Programs\Python\Python36-32\lib\ssl.py", line 459, in options
    super(SSLContext, SSLContext).options.__set__(self, value)
  [Previous line repeated 311 more times]
RecursionError: maximum recursion depth exceeded while calling a Python object

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\piero\AppData\Local\Programs\Python\Python36-32\lib\site-packages\gevent\hub.py", line 866, in switch
    switch(value)
  File "C:\Users\piero\AppData\Local\Programs\Python\Python36-32\lib\site-packages\gevent\greenlet.py", line 538, in run
    self._report_error(sys.exc_info())
  File "C:\Users\piero\AppData\Local\Programs\Python\Python36-32\lib\site-packages\gevent\greenlet.py", line 520, in _report_error
    self._exc_info = exc_info[0], exc_info[1], dump_traceback(exc_info[2])
  File "C:\Users\piero\AppData\Local\Programs\Python\Python36-32\lib\site-packages\gevent\_tblib.py", line 370, in g
    return f(a)
  File "C:\Users\piero\AppData\Local\Programs\Python\Python36-32\lib\site-packages\gevent\_tblib.py", line 425, in dump_traceback
    return dumps(tb)
RecursionError: maximum recursion depth exceeded while pickling an object
Sat Mar 31 09:10:46 2018 <built-in method switch of Greenlet object at 0x07635710> failed with RecursionError
Impulse-PW commented 6 years ago

I've solved the problem by adding this to the very beginning of my code:

from gevent import monkey
monkey.patch_all(thread=False)
rossengeorgiev commented 6 years ago

Read #97