我有一个 Google App Engine 应用程序 - http://mylovelyapp.appspot.com/ 它有一个页面 - mylovelypage
目前,页面只是self.response.out.write('OK')
如果我在我的计算机上运行以下 Python:
import urllib2
f = urllib2.urlopen("http://mylovelyapp.appspot.com/mylovelypage")
s = f.read()
print s
f.close()
它打印“确定”
问题是如果我login:required
在应用程序的 yaml 中添加到此页面
然后这会打印出 Google 帐户登录页面的 HTML
我已经尝试过“正常”的身份验证方法。例如
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)
但这没有什么区别——我仍然得到了登录页面的 HTML。
我已经尝试过Google 的 ClientLogin auth API,但我无法让它工作。
h = httplib2.Http()
auth_uri = 'https://www.google.com/accounts/ClientLogin'
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
myrequest = "Email=%s&Passwd=%s&service=ah&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
我似乎确实能够正确获取一些令牌,但是当我调用“mylovelypage”时尝试在标题中使用它仍然只是返回登录页面的 HTML。:-(
有人可以帮忙吗?
我可以使用GData 客户端库来做这种事情吗?从我读到的内容来看,我认为它应该能够访问 App Engine 应用程序,但我也没有更成功地让 App Engine 的身份验证工作在那里
任何指向样本、文章,甚至只是我应该搜索以帮助我开始的关键字的指针,都将不胜感激。
谢谢!