簡體   English   中英

使用python登錄到Google Appengine

[英]Login to Google Appengine with python

使用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的lib。 非常感激!

我認為SID不是您需要發送的唯一cookie,並且不應將Authorization標頭發送給她。

您應該使用帶有HttpCookieProcessorCookieJar來管理您的cookie,而不是手動執行。 這里的例子

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM