Added explicit require 'uri' for encode_www_form
Added better login token handling and expiry