我有一个 Google App Engine 应用程序 - http://mylovelyapp.appspot.com/它有一个页面 - mylovelypage

目前,该页面只是 self.response.out.write('OK')

如果我在我的计算机上运行以下 Python:

import urllib2
f = urllib2.urlopen("http://mylovelyapp.appspot.com/mylovelypage")
s = f.read()
print s
f.close()

它打印“确定”

问题是如果我添加 login:required 到应用程序 yaml 中的此页面

然后打印出 Google 帐户登录页面的 HTML

我尝试过“正常”身份验证方法。例如

passman = urllib2.HTTPPasswordMgrWithDefaultRealm()

auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler.add_password(None,
                          uri='http://mylovelyapp.appspot.com/mylovelypage',
                          user='billy.bob@gmail.com',
                          passwd='billybobspasswd')
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)

但这没有什么区别 - 我仍然得到登录页面的 HTML。

我试过了 Google 的 ClientLogin 身份验证 API, ,但我无法让它工作。

h = httplib2.Http()

auth_uri = 'https://www.google.com/accounts/ClientLogin'
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
myrequest = "Email=%s&Passwd=%s&service=ah&source=DALELANE-0.0" % ("billy.bob@gmail.com", "billybobspassword")
response, content = h.request(auth_uri, 'POST', body=myrequest, headers=headers)

if response['status'] == '200':
    authtok = re.search('Auth=(\S*)', content).group(1)

    headers = {}
    headers['Authorization'] = 'GoogleLogin auth=%s' % authtok.strip()
    headers['Content-Length'] = '0'

    response, content = h.request("http://mylovelyapp.appspot.com/mylovelypage", 
                                  'POST', 
                                  body="", 
                                  headers=headers)

    while response['status'] == "302":        
        response, content = h.request(response['location'], 'POST', body="", headers=headers) 

    print content

我似乎确实能够正确获取一些令牌,但是当我调用“mylovelypage”时尝试在标头中使用它仍然只是返回登录页面的 HTML。:-(

有人可以帮忙吗?

我可以使用 GData客户端库 做这种事?从我阅读的内容来看,我认为它应该能够访问应用引擎应用程序,但是我还没有成功地获得身份验证的应用程序,即

对我应该搜索的样本,文章,甚至只是关键字的任何指示,都将非常感谢。

谢谢!

有帮助吗?

解决方案

appcfg.py(将数据上传到 App Engine 的工具)必须准确执行此操作,以便通过 App Engine 服务器对自身进行身份验证。相关功能被抽象到appengine_rpc.py中。简而言之,解决方案是:

  1. 使用 谷歌客户端登录API 获取身份验证令牌。appengine_rpc.py 执行此操作 _GetAuthToken
  2. 将身份验证令牌发送到 App Engine 应用程序上的特殊 URL。然后该页面返回一个 cookie 和一个 302 重定向。忽略重定向并存储 cookie。appcfg.py 在中执行此操作 _GetAuthCookie
  3. 在以后的所有请求中使用返回的 cookie。

您可能还想看看 _认证, ,了解 appcfg 如何处理来自 ClientLogin 的各种返回代码,以及 _GetOpener, ,了解 appcfg 如何创建不遵循 HTTP 重定向的 urllib2 OpenerDirector。或者,事实上,您可以直接使用 AbstractRpcServer 和 HttpRpcServer 类,因为它们几乎可以完成您需要的所有操作。

其他提示

感谢 Arachnid 的回答 - 它按照建议工作

这是代码的简化副本,以防对下一个人尝试有帮助!

import os
import urllib
import urllib2
import cookielib

users_email_address = "billy.bob@gmail.com"
users_password      = "billybobspassword"

target_authenticated_google_app_engine_uri = 'http://mylovelyapp.appspot.com/mylovelypage'
my_app_name = "yay-1.0"



# we use a cookie to authenticate with Google App Engine
#  by registering a cookie handler here, this will automatically store the 
#  cookie returned when we use urllib2 to open http://currentcost.appspot.com/_ah/login
cookiejar = cookielib.LWPCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))
urllib2.install_opener(opener)

#
# get an AuthToken from Google accounts
#
auth_uri = 'https://www.google.com/accounts/ClientLogin'
authreq_data = urllib.urlencode({ "Email":   users_email_address,
                                  "Passwd":  users_password,
                                  "service": "ah",
                                  "source":  my_app_name,
                                  "accountType": "HOSTED_OR_GOOGLE" })
auth_req = urllib2.Request(auth_uri, data=authreq_data)
auth_resp = urllib2.urlopen(auth_req)
auth_resp_body = auth_resp.read()
# auth response includes several fields - we're interested in 
#  the bit after Auth= 
auth_resp_dict = dict(x.split("=")
                      for x in auth_resp_body.split("\n") if x)
authtoken = auth_resp_dict["Auth"]

#
# get a cookie
# 
#  the call to request a cookie will also automatically redirect us to the page
#   that we want to go to
#  the cookie jar will automatically provide the cookie when we reach the 
#   redirected location

# this is where I actually want to go to
serv_uri = target_authenticated_google_app_engine_uri

serv_args = {}
serv_args['continue'] = serv_uri
serv_args['auth']     = authtoken

full_serv_uri = "http://mylovelyapp.appspot.com/_ah/login?%s" % (urllib.urlencode(serv_args))

serv_req = urllib2.Request(full_serv_uri)
serv_resp = urllib2.urlopen(serv_req)
serv_resp_body = serv_resp.read()

# serv_resp_body should contain the contents of the 
#  target_authenticated_google_app_engine_uri page - as we will have been 
#  redirected to that page automatically 
#
# to prove this, I'm just gonna print it out
print serv_resp_body

对于那些无法让 ClientLogin 工作的人,请尝试应用程序引擎 OAuth 支持.

我不太熟悉 AppEngine 或 Google 的 Web api,但对于强力方法,您可以使用 mechanize 之类的东西编写脚本(http://wwwsearch.sourceforge.net/mechanize/)在开始执行客户端的实际工作之前简单地完成登录过程。

我不是 python 专家或应用程序引擎专家。但是您是否尝试遵循示例应用程序 http://code.google.com/appengine/docs/gettingstarted/usingusers.html. 。我创建了一个 http://quizengine.appspot.com, ,它似乎与谷歌身份验证和其他一切都工作得很好。只是一个建议,但请查看入门指南。如果这个建议听起来很幼稚,请放轻松。:) 谢谢。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top