The Remote API is not supported by Google App Engine when using federated login. However, apparently it is possible to use it in Python 2.5, as described here:
http://blog.notdot.net/2010/06/Using-remote-api-with-OpenID-authentication
Based on that solution and the comments below the article I have created the following Python 2.7 code:
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
remote_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)])
When I run the remote_api_shell command:
remote_api_shell.py -s my_application.appspot.com/remoteapi
This error is returned:
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
What is wrong with my code? Is it possible to use the Remote API in Python 2.7 on Google App Engine using federated login?
There is nothing wrong with the code. Run the remote_api_shell command like so:
When prompted for an email address, fill in the value of
MY_SECRET_KEY. The password doesn’t matter.