使用联合登录时,Google App Engine不支持Remote API 。但是,显然可以在 Python 2.5 中使用它,如下所述:
http://blog.notdot.net/2010/06/Using-remote-api-with-OpenID-authentication
基于该解决方案和文章下方的评论,我创建了以下 Python 2.7 代码:
应用程序.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
我的代码有什么问题?是否可以使用联合登录在 Google App Engine 上的 Python 2.7 中使用远程 API?