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.
Here is my code(I’m a new python user,the code will be very bad..):
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.
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.
Very appreciated!
i think the
SIDisn’t the only cookie you need to send, and theAuthorizationheader shouldn’t be sent her.you should use a
CookieJarwith aHttpCookieProcessorto manage your cookies instead of doing it manually. example here