我正在尝试抓取一些网络内容,以获取我的Google应用程序生成的Google Appstats的统计信息。请注意,这与Google Analytics(分析)不同。我正在使用python 2.7.5。我面临的问题是我的请求中的初始Google身份验证。我有需要从google应用统计信息中调用的api,但是当我使用自己的google appengine帐户凭据时,仍会收到DENY作为响应。这将导致重定向到account.google.com页面。我尝试了几种不同的方法,但未成功登录account.google.com。
有人对此有任何想法吗?如果您能指出一些好的参考资料,将会对您有所帮助
谢谢
此代码示例将使您能够获取受Google登录名保护的/ secure页面的内容。不要忘记设置电子邮件,密码和应用程序ID。然后,您可以使用此打开器获取其他受保护的页面。
import urllib
import urllib2
import cookielib
import logging
EMAIL = ''
PASSWORD = ''
APPID = 'YOURAPPID'
# Setup to be able to get the needed cookies that GAE returns
cookiejar = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))
urllib2.install_opener(opener)
# This is the setup to construct the login URL for authentication.
authreq_data = urllib.urlencode({'Email': EMAIL,
'Passwd': PASSWORD,
'service': 'ah',
'source': '',
'accountType': 'HOSTED_OR_GOOGLE'})
# Get an AuthToken from Google Accounts
auth_req = urllib2.Request('https://www.google.com/accounts/ClientLogin',
data=authreq_data)
try:
auth_resp = opener.open(auth_req)
logging.info('Successful authorization as %s' % EMAIL)
except urllib2.HTTPError:
logging.warning('Authorization as %s failed. '
'Please, check your email and password' % EMAIL)
auth_resp_body = auth_resp.read()
auth_resp_dict = dict(x.split('=')
for x in auth_resp_body.split('\n') if x)
authtoken = auth_resp_dict['Auth']
authreq_data = urllib.urlencode({'continue': 'http://%s.appspot.com/secure' % APPID,
'auth': authtoken})
login_uri = ('http://%s.appspot.com/_ah/login?%s' % (APPID, authreq_data))
# Do the actual login and getting the cookies.
print opener.open(urllib2.Request(login_uri)).read()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句