DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Get A Jaiku Personal_key Given A Username And Password

05.25.2008
| 3611 views |
  • submit to reddit
        Logs into Jaiku and then gets the personal_key for using the API.

def GetJaikuPersonalKey(user, password):
  """Finds the Jaiku API personal_key from a username and password.

  One day I'll learn to use urllib2 properly and cookie parsing and stuff.
  """

  # login and find a cookie
  login_url = "http://jaiku.com/login"
  request_body = urllib.urlencode({'log': user,
                                   'pwd': password,
                                   'rememberme': '1'})
  # Open a connection to the authentication server.
  auth_connection = httplib.HTTPConnection('jaiku.com')
  # Begin the POST request to the client login service.
  auth_connection.putrequest('POST', '/login')
  # Set the required headers for an Account Authentication request.
  auth_connection.putheader('Content-type',
                            'application/x-www-form-urlencoded')
  auth_connection.putheader('Content-Length', str(len(request_body)))
  auth_connection.endheaders()
  auth_connection.send(request_body)
  auth_response = auth_connection.getresponse()
  if auth_response.status == 303:
    cookie_str = auth_response.getheader("set-cookie")
    # TODO(ark) parse this properly!
    res = re.search("(jaikuuser_[^;]*).*(jaikupass_[^;]*)", cookie_str)
    if res:
      auth_cookie = "%s; %s" % (res.group(1), res.group(2))
      apikey_url = "http://api.jaiku.com/key"
      req = urllib2.Request(url=apikey_url)
      req.add_header('Cookie', auth_cookie)
      f = urllib2.urlopen(req)
      return f.read()