Google app engine 如何在Python2.7中通过联合登录使用Google App Engine远程API?

Google app engine 如何在Python2.7中通过联合登录使用Google App Engine远程API?,google-app-engine,python-2.7,Google App Engine,Python 2.7,当使用联合登录时,该应用程序由Google应用程序引擎提供。但是,显然可以在Python 2.5中使用它,如下所述: 基于该解决方案和本文下面的注释,我创建了以下Python 2.7代码: app.yaml: application: my_application version: 1 runtime: python27 api_version: 1 threadsafe: true builtins: - appstats: on #- remote_api: on handlers:

当使用联合登录时,该应用程序由Google应用程序引擎提供。但是,显然可以在Python 2.5中使用它,如下所述:

基于该解决方案和本文下面的注释,我创建了以下Python 2.7代码:

app.yaml:

application: my_application
version: 1
runtime: python27
api_version: 1
threadsafe: true

builtins:
- appstats: on
#- remote_api: on

handlers:
- url: /remoteapi.*
  script: remote_api.app
- url: /.*
  script: main.app
  secure: never
远程api.py:

from google.appengine.ext.remote_api import handler
from google.appengine.ext import webapp
import re

MY_SECRET_KEY = 'secret'
cookie_re = re.compile('^"?([^:]+):.*"?$')

class ApiCallHandler(handler.ApiCallHandler):
    def CheckIsAdmin(self):
        login_cookie = self.request.cookies.get('dev_appserver_login', '')
        match = cookie_re.search(login_cookie)
        if (match and match.group(1) == MY_SECRET_KEY
            and 'X-appcfg-api-version' in self.request.headers):
            return True
        else:
            self.redirect('/_ah/login')
            return False


app = webapp.WSGIApplication([('.*', ApiCallHandler)])
当我运行remote_api_shell命令时:

remote_api_shell.py -s my_application.appspot.com/remoteapi
返回此错误:

Traceback (most recent call last):
  File "remote_api_shell.py", line 133, in <module>
    run_file(__file__, globals())
  File "remote_api_shell.py", line 129, in run_file
    execfile(script_path, globals_)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\re
mote_api_shell.py", line 140, in <module>
    main(sys.argv)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\re
mote_api_shell.py", line 136, in main
    appengine_rpc.HttpRpcServer)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\re
mote_api_shell.py", line 76, in remote_api_shell
    rpc_server_factory=rpc_server_factory)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\remo
te_api\remote_api_stub.py", line 682, in ConfigureRemoteApi
    app_id = GetRemoteAppIdFromServer(server, path, rtok)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\remo
te_api\remote_api_stub.py", line 525, in GetRemoteAppIdFromServer
    response = server.Send(path, payload=None, **urlargs)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\ap
pengine_rpc.py", line 366, in Send
    f = self.opener.open(req)
  File "C:\Python27\lib\urllib2.py", line 400, in open
    response = meth(req, response)
  File "C:\Python27\lib\urllib2.py", line 513, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python27\lib\urllib2.py", line 438, in error
    return self._call_chain(*args)
  File "C:\Python27\lib\urllib2.py", line 372, in _call_chain
    result = func(*args)
  File "C:\Python27\lib\urllib2.py", line 521, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 302: Found
回溯(最近一次呼叫最后一次):
文件“remote_api_shell.py”,第133行,在
运行\u文件(\uuuu文件,globals())
文件“remote_api_shell.py”,第129行,在run_文件中
execfile(脚本路径,全局路径)
文件“C:\ProgramFiles(x86)\Google\Google\U appengine\Google\appengine\tools\re
mote_api_shell.py”,第140行,in
主(系统argv)
文件“C:\ProgramFiles(x86)\Google\Google\U appengine\Google\appengine\tools\re
mote_api_shell.py”,第136行,主视图
appengine_rpc.HttpRpcServer)
文件“C:\ProgramFiles(x86)\Google\Google\U appengine\Google\appengine\tools\re
mote_api_shell.py”,第76行,位于远程api_shell中
rpc_服务器_工厂=rpc_服务器_工厂)
文件“C:\ProgramFiles(x86)\Google\Google\U appengine\Google\appengine\ext\remo
te_api\remote_api_stub.py”,第682行,在ConfigureRemoteApi中
app_id=GetRemoteAppIdFromServer(服务器、路径、rtok)
文件“C:\ProgramFiles(x86)\Google\Google\U appengine\Google\appengine\ext\remo
GetRemoteAppIdFromServer中的te_api\remote_api_stub.py”,第525行
response=server.Send(路径,有效负载=None,**urlargs)
文件“C:\ProgramFiles(x86)\Google\Google\U appengine\Google\appengine\tools\ap
pengine_rpc.py”,第366行,发送
f=自动开启器开启(要求)
文件“C:\Python27\lib\urllib2.py”,第400行,打开
响应=方法(请求,响应)
文件“C:\Python27\lib\urllib2.py”,第513行,在http\u响应中
“http”、请求、响应、代码、消息、hdrs)
文件“C:\Python27\lib\urllib2.py”第438行出错
返回自我。调用链(*args)
文件“C:\Python27\lib\urllib2.py”,第372行,在调用链中
结果=func(*args)
文件“C:\Python27\lib\urllib2.py”,第521行,默认为http\u error\u
raise HTTPError(请求获取完整url(),代码,消息,hdrs,fp)
urllib2.HTTPError:HTTP错误302:找到

我的代码有什么问题?是否可以使用联邦登录在Google App Engine上使用Python 2.7中的远程API?

代码没有问题。按如下方式运行remote_api_shell命令:

remote_api_shell.py -s my_application.appspot.com -p /remoteapi
当提示输入电子邮件地址时,请填写
MY\u SECRET\u KEY
的值。密码不重要