Skip to content

Instantly share code, notes, and snippets.

@tpeng
Created January 9, 2013 09:54
Show Gist options
  • Select an option

  • Save tpeng/4492006 to your computer and use it in GitHub Desktop.

Select an option

Save tpeng/4492006 to your computer and use it in GitHub Desktop.
login Quora programmatically but got HTTP Error 500
# login the Quora and get notifications with unofficial API (http://www.quora.com/Edmond-Lau/Edmond-Laus-Posts/Quora-Extension-API)
# tpeng <[email protected]> 2013/1/7
# some references
# https://gist.github.com/2341517
# http://www.ruby-doc.org/gems/docs/q/quora-client-0.1.2/Quora/Auth.html
# http://stackoverflow.com/questions/10616449/how-to-programmatically-log-in-to-quora-by-java-code
import cookielib
import json
import re
import urllib
import urllib2
class Quora(object):
def __init__(self):
self.cj = cookielib.LWPCookieJar()
cookie_support = urllib2.HTTPCookieProcessor(self.cj)
opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler(debuglevel=1))
urllib2.install_opener(opener)
def login(self, email, pwd):
url = 'http://www.quora.com/login'
text = urllib2.urlopen(url).read()
p_formkey = re.compile(r'formkey:\s+\"([a-z0-9]+)\"', re.I | re.M)
formkey = re.search(p_formkey, text).group(1)
p_window_id = re.compile(r'windowId\s+=\s+\"([a-z0-9-]+)\"', re.I | re.M)
window_id = re.search(p_window_id, text).group(1)
p_live = re.compile(r'live:(\w+):cls:a\.app\.view\.login', re.I | re.M)
p_vconjson = re.compile(r'InlineLoginBase:(\w+)\"', re.I | re.M)
vconjson = {'cls':'a.app.view.login', 'InlineLogin': re.search(p_vconjson, text).group(1),
'live' : re.search(p_live, text).group(1)}
# username = urllib.quote(username)
# pwd = urllib.quote(pwd)
auth_data = {'args' : [],
'kwargs':
{'email' : email, 'password' : pwd}}
data = {'json' : auth_data,
'window_id' : window_id,
'formkey': formkey,
'__vcon_json': json.dumps(vconjson),
'__vcon_method' : 'do_login',
'js_init': '{}'}
headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux i686; rv:8.0) Gecko/20100101 Firefox/8.0',
# 'Referer' : 'http://www.quora.com/login/',
# 'X-Requested-With' : "XMLHttpRequest",
# 'Accept' : "application/json, text/javascript, */*",
}
req = urllib2.Request(
url='http://www.quora.com/webnode2/server_call_POST/',
data=urllib.urlencode(data),
headers=headers
)
text = urllib2.urlopen(req).read()
print text
def get_notifications(self):
url = 'http://api.quora.com/api/logged_in_user?fields=notifs,inbox'
result = urllib2.urlopen(url)
text = result.read()
print text
return text
if __name__ == '__main__':
q = Quora()
q.login('[email protected]', 'xxx')
@karan
Copy link

karan commented Jul 4, 2013

I tried using mechanize, and got the same HTTP 500 error. Did you figure out why Quora seems unhappy about bots logging in to the site?

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