title: Login to google using Python
body: <p>I have been trying for some time now to login to my google account and download a file from it <strong>without using any external libraries</strong>.
  I am specifically talking about Google Finance (<a href="https://www.google.com/finance" rel="nofollow">https://www.google.com/finance</a>). 
  All I want to do is login, and download my portfolio (after you sign in and go to the Portfolios tab, there is a link saying: Download to spreadsheet).
  But I can't get it to work.</p>
  
  <p>I have seen several posts here regarding similar problems but none of them worked for me.</p>
  
  <p>This is the code I have now:</p>
  
  <pre><code>import urllib, urllib2, cookielib
  
  #Gets the current directory
  output_path = os.getcwd()
  
  def make_url(ticker_symbol):
      return base_url + ticker_symbol
  
  def make_filename(ticker_symbol):
      return output_path + "\\" + ticker_symbol + ".csv"
  
  # Login page for Google Finance
  login_url = "https://accounts.google.com/ServiceLogin?service=finance&amp;passive=1209600&amp;continue=https://www.google.com/finance&amp;followup=https://www.google.com/finance"
  
  # Google Finance portfolio Download url (works after you signed in)
  download_url = "https://www.google.com/finance/portfolio?pid=1&amp;output=csv&amp;action=viewt&amp;ei=ypZyUZi_EqGAwAP5Vg"
  
  username = 'my_username'
  password = 'my_password'
  
  cj = cookielib.CookieJar()
  opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
  login_data = urllib.urlencode({'username' : username, 'j_password' : password})
  opener.open(login_url, login_data)
  
  # Download the file
  try:
      urllib.urlretrieve(download_url, make_filename("My_portfolio"))
  except urllib.ContentTooShortError as e:
      outfile = open(make_filename("My_portfolio"), "w")
      outfile.write(e.content)
      outfile.close()
  </code></pre>
  
  <p>What am I doing wrong?</p>
  
  <p>Thanks a lot!!</p>