title: How do you access an authenticated Google App Engine service from a (non-web) python client?
body: <p>I have a Google App Engine app - <a href="http://mylovelyapp.appspot.com/" rel="noreferrer">http://mylovelyapp.appspot.com/</a>
  It has a page - mylovelypage</p>
  
  <p>For the moment, the page just does <code>self.response.out.write('OK')</code></p>
  
  <p>If I run the following Python at my computer:</p>
  
  <pre><code>import urllib2
  f = urllib2.urlopen("http://mylovelyapp.appspot.com/mylovelypage")
  s = f.read()
  print s
  f.close()
  </code></pre>
  
  <p>it prints "OK"</p>
  
  <p>the problem is if I add <code>login:required</code> to this page in the app's yaml</p>
  
  <p>then this prints out the HTML of the Google Accounts login page</p>
  
  <p>I've tried "normal" authentication approaches. e.g.</p>
  
  <pre><code>passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
  
  auth_handler = urllib2.HTTPBasicAuthHandler()
  auth_handler.add_password(None,
                            uri='http://mylovelyapp.appspot.com/mylovelypage',
                            user='billy.bob@gmail.com',
                            passwd='billybobspasswd')
  opener = urllib2.build_opener(auth_handler)
  urllib2.install_opener(opener)
  </code></pre>
  
  <p>But it makes no difference - I still get the login page's HTML back.</p>
  
  <p>I've tried <a href="http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html" rel="noreferrer">Google's ClientLogin auth API</a>, but I can't get it to work.</p>
  
  <pre><code>h = httplib2.Http()
  
  auth_uri = 'https://www.google.com/accounts/ClientLogin'
  headers = {'Content-Type': 'application/x-www-form-urlencoded'}
  myrequest = "Email=%s&amp;Passwd=%s&amp;service=ah&amp;source=DALELANE-0.0" % ("billy.bob@gmail.com", "billybobspassword")
  response, content = h.request(auth_uri, 'POST', body=myrequest, headers=headers)
  
  if response['status'] == '200':
      authtok = re.search('Auth=(\S*)', content).group(1)
  
      headers = {}
      headers['Authorization'] = 'GoogleLogin auth=%s' % authtok.strip()
      headers['Content-Length'] = '0'
  
      response, content = h.request("http://mylovelyapp.appspot.com/mylovelypage", 
                                    'POST', 
                                    body="", 
                                    headers=headers)
  
      while response['status'] == "302":        
          response, content = h.request(response['location'], 'POST', body="", headers=headers) 
  
      print content
  </code></pre>
  
  <p>I do seem to be able to get some token correctly, but attempts to use it in the header when I call 'mylovelypage' still just return me the login page's HTML. :-( </p>
  
  <p>Can anyone help, please?</p>
  
  <p>Could I use the <a href="http://code.google.com/p/gdata-python-client/" rel="noreferrer">GData client library</a> to do this sort of thing? From
  what I've read, I think it should be able to access App Engine apps,
  but I haven't been any more successful at getting the authentication working for App Engine stuff there either </p>
  
  <p>Any pointers to samples, articles, or even just keywords I should be
  searching for to get me started, would be very much appreciated.</p>
  
  <p>Thanks!</p>