0

使用 Python 和 urllib2 登录 Google Appengine 我从https://www.google.com/accounts/ClientLogin获得了 Auth 和 Sid 但后来我添加了标题,却发现我没有登录。

这是我的代码(我是一个新的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.

我在firefox中使用firebug查看头信息,cookie不只是上面的sid。我该怎么办?接下来的表演需要登录(我还没写),我该怎么写?它是在我的笔记本电脑上午餐的,所以我不使用 urlfetch 之类的 appengieg 的库。非常感激!

4

1 回答 1

0

我认为这SID不是您需要发送的唯一 cookie,并且Authorization不应将标头发送给她。

您应该使用 aCookieJarHttpCookieProcessor管理您的 cookie,而不是手动进行。这里的例子

于 2012-05-05T16:59:27.390 回答