Skip to content

Instantly share code, notes, and snippets.

@maxisoft
Created January 10, 2014 22:52
Show Gist options
  • Save maxisoft/8364262 to your computer and use it in GitHub Desktop.
Save maxisoft/8364262 to your computer and use it in GitHub Desktop.
python steam login process copied from http://forums.eventscripts.com/viewtopic.php?f=90&t=46941
import urllib
import urllib2
import json
import time
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5
import base64
import cookielib
uname = "----"
passwd = "----"
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
# Request key
url = 'https://steamcommunity.com/login/getrsakey/'
values = {'username' : uname, 'donotcache' : str(int(time.time()*1000))}
headers = { 'User-Agent' : user_agent }
post = urllib.urlencode(values)
req = urllib2.Request(url, post, headers)
response = urllib2.urlopen(req).read()
data = json.loads(response)
print "Get Key Success:", data["success"]
# Encode key
mod = long(str(data["publickey_mod"]), 16)
exp = long(str(data["publickey_exp"]), 16)
rsa = RSA.construct((mod, exp))
cipher = PKCS1_v1_5.new(rsa)
print base64.b64encode(cipher.encrypt(passwd))
# Login
url2 = 'https://steamcommunity.com/login/dologin/'
values2 = {
'username' : uname,
"password": base64.b64encode(cipher.encrypt(passwd)),
"emailauth": "",
"loginfriendlyname": "",
"captchagid": "-1",
"captcha_text": "",
"emailsteamid": "",
"rsatimestamp": data["timestamp"],
"remember_login": False,
"donotcache": str(int(time.time()*1000)),
}
headers2 = { 'User-Agent' : user_agent }
post2 = urllib.urlencode(values2)
req2 = urllib2.Request(url2, post2, headers)
response2 = urllib2.urlopen(req2).read()
data2 = json.loads(response2)
if data2["success"]:
print "Logged in!"
else:
print "Error, could not login:", data2["message"]
print response2
@zais
Copy link

zais commented Mar 16, 2015

Hello, do you know how to:

  1. request capture from steam?
  2. use this login to query some data from steam?

@jayme-github
Copy link

@maxisoft
I've used something like this for a while but it seems as if it does not work anymore (does it still for you?). I'm always rejected with "Incorrect login." since like two or three weeks but I can't see any change in the steam JavaScript or the process itself... :(

@zais:

  1. request capture from steam?

If you need to solve a captcha dologin will return a captchagid use that to fetch the image from https://steamcommunity.com/public/captcha.php?gid=captchagid

  1. use this login to query some data from steam?

Store the cookies provided and use them for further requests

FTR: Someone has written a quite complete description of the login process here: https://bitbucket.org/Aerizeon/steamweb

@nux17
Copy link

nux17 commented Jun 14, 2015

Hey,

Seems to need some some email authentication code, isn't it ?

I managed to code a Selenium + PhantomJS based authentication working routine, but having issues to export cookies and use'em into urllib2/requests.

Any suggestions ?

@jayme-github
Copy link

Seems to need some some email authentication code, isn't it ?

Sure, thats when your "browser" is not known to steamguard. Even easier as the captcha thing and I'm past that but even with "browser" known to steamguard I get that "Incorrect login".

You will probably have to "ignore_discard" when ex-/importing cookies (speaking in terms of LWPCookieJar).

@nux17
Copy link

nux17 commented Jun 14, 2015

Would you like to help eachother with ?

Here's my snippet :
https://gist.github.com/nux17/7e238bc6e7a66ced9a2b

Logging works, even ask for Auth code. Just have to know how to get cookies extracted and working with urllib2/requests.

@wagenaartje
Copy link

Got the same problem, I tried making my own one with Python 3 by use of this and https://bitbucket.org/Aerizeon/steamweb. It didn't work, looks like they have changed the login procedure. I have sought contact with Aerizeon and he might update the login process.

This also seems interesting:
https://github.com/ChristophHaag/steamchat-webapi/blob/master/steam_webapi.py

@jayme-github
Copy link

I've just updated my fork of this gist with what I used for quite some time.
Stripped that out of my project without testing it so there might contain bugs and debug stuff but it handles captcha and email auth, steam js like escaping of non-ascii characters and saves cookies to a file if thats of help for someone.

@wagenaartje
Copy link

Jayme, does your own also give the 'Incorrect login' message?

@jayme-github
Copy link

Jayme, does your own also give the 'Incorrect login' message?

Yes, since end of april I guess. Worked great 'till then and, as said, I could not see any difference in the javascript steam uses.

@wagenaartje
Copy link

I may have just solved it, instead of getting an 'incorrect login' I know get emailauth. Ill keep you updated and i'll say when (and how) it works completely!

EDIT: It works, I have successfully logged in. I am now cleaning up the code

FYI: I have written it in python 3 (not 2!), you also need to have pycrypto installed which you most likely already have. I will make a repo on github soon (max 5 days) and upload my project there.

@jayme-github
Copy link

Cool! Let me know what you changed or did different than me!

@wagenaartje
Copy link

I uploaded it. https://github.com/wagenaartje/PySteam/blob/master/PySteam.py . It contains a lot of unnecessary headers, but I achieved it by copying the exact login process from internet explorer and including the same headers and such. I think the problem was at the session ID, I am now going to continue to remove unnecessary headers and such.

@wagenaartje
Copy link

also note it does not handle captcha codes yet.

@jayme-github
Copy link

I think the problem was at the session ID

Strange. My code requests a sessionid first as well here and provides it along with all other cookies for every other GET/POST. I also tried the X-Prototype-Version header some time ago...
We'll see what you find out when you reduce the headers and/or what I find out when I have time to dig into this again ;)
Thanks for sharing! I'll follow you for now to not miss any updates on this (why the heck can't I watch this discussion...)

@jayme-github
Copy link

:trollface: I'ts just that stupid twofactorcode parameter that needs to be added to the dologin POST.

@wagenaartje
Copy link

damn xD

@furkan6116
Copy link

you dont need to do that. there is a library for steam link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment