简体   繁体   English

使用python登录到Google Appengine

[英]Login to Google Appengine with python

Log in Google Appengine with Python and urllib2 I have got Auth and Sid from https://www.google.com/accounts/ClientLogin But then I added header,only to see that I was not loggin in. 使用Python和urllib2登录Google Appengine,我从https://www.google.com/accounts/ClientLogin获得了Auth和Sid,但是随后我添加了标头,只是看到我没有登录。

Here is my code(I'm a new python user,the code will be very bad..): 这是我的代码(我是python新用户,代码会很糟糕..):

LOGIN_URL = 'https://www.google.com/accounts/ClientLogin'
EMAIL = 'XXXXX@gmail.com'
PASSWORD = 'XXXXX'
print "Your Google account:"
print EMAIL
print "Logging in..."
arequest = urllib2.Request(LOGIN_URL, urlencode({
    'service': 'ah',
    'Email': EMAIL,
    'Passwd': PASSWORD,
}))

f = urllib2.urlopen(arequest)
lines = f.read().split()
auth = lines[2][5:]
sid = lines[0][4:]
print "Google Logged in"
#print auth and sid,and you will find the code above is right
brequest = urllib2.Request("https://appengine.google.com")
brequest.add_header('User-Agent', 'Here is Chrome's UA')
brequest.add_header('Content-Type', 'application/x-www-form-urlencoded')
brequest.add_header('Authorization', 'GoogleLogin auth=' + auth)
brequest.add_header('Cookie', 'SID=' + sid)
brequest.add_header('Referer', 'https://appengine.google.com/start') 
print "Opening with Auth info..."
bresponse = urllib2.urlopen(brequest)
print bresponse.read()
#The output shows that I haven't logged in.

I used firebug in firefox to view the header info,the cookie was not only the sid above. 我在firefox中使用firebug查看标题信息,cookie不仅是上面的sid。 What should I do?The performance next needs logged in(I haven't write that),how should I write? 我该怎么办?性能下一个需要登录(我还没有写),该怎么写? It was lunched on my laptop,so I don't use appengieg's lib like urlfetch. 它在我的笔记本电脑上吃过午餐,所以我不使用urlfetch这样的appengieg的lib。 Very appreciated! 非常感激!

i think the SID isn't the only cookie you need to send, and the Authorization header shouldn't be sent her. 我认为SID不是您需要发送的唯一cookie,并且不应将Authorization标头发送给她。

you should use a CookieJar with a HttpCookieProcessor to manage your cookies instead of doing it manually. 您应该使用带有HttpCookieProcessorCookieJar来管理您的cookie,而不是手动执行。 example here 这里的例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM