Search code examples
python-3.xsteamtwo-factor-authentication

Steam 2FA throwing "Failed to get a web session" consistantly


I'm using steam's python library to create an app for generating 2FA codes. The issue is that steam throws me this error whenever I try to add a phone number or add 2FA to said account:

RuntimeError: Failed to get a web session. Try again in a few minutes
Fri Mar 30 08:39:04 2018 <Greenlet at 0x6e64580: handle_after_logon> failed with RuntimeError

Yes I've waited a few minutes and have been trying for hours. Here's the code that I'm using 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". "client" is our logged in SteamClient instance as requested by documentation
    sa = SteamAuthenticator(medium=client)
    #My account has no phone number
    print(sa.has_phone_number())
    #Adding phone number because I know ahead of time I don't have it
    sa.add_phone_number("myphonenumber with area code")
    sa.add()    # SMS code will be sent to account's phone number
    sa.secrets  # dict with authenticator secrets
    #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()

Due to a lack of example code on 2FA in particular I've followed the documentation the best I could, I've looked at all of the below:

http://steam.readthedocs.io/en/latest/api/steam.client.html

http://steam.readthedocs.io/en/latest/api/steam.guard.html

https://github.com/ValvePython/steam/blob/master/recipes/1.Login/persistent_login.py

I feel like there's simply a silly error in my code, but reading through the documentation doesn't appear to be helping me solve this.

Thanks for your guys help.


Solution

  • I debugged for a while and it's an issue with gevent

    I added this line to fix it at the beginning of my script:

    from gevent import monkey
    monkey.patch_all(thread=False)