1

I was wondering if it was possible to use the remote_api_stub from within a script that is running on the local dev_appserver.py.

I have successfully been able to connect to both the development/production datastore via the remote_api_stub from a basic script that is run from the command line, so I'm pretty confident the app.yaml is set up correctly.

If I wanted to find a list of User objects from my local development server, I can simply set up the stub to connect to the local server, and then fetch the ndb models as usual.

DEV_HOSTNAME = 'localhost'
DEV_PORT = '54256'
DEV_SERVER = '%s:%s' % (DEV_HOSTNAME, DEV_PORT)
DEV_PATH = '/_ah/remote_api'
DEV_SECURE = False
remote_api_stub.ConfigureRemoteApiForOAuth(DEV_SERVER, DEV_PATH, secure=DEV_SECURE)

users = User.query()
# at this point users will contain data from the DEVELOPMENT server

If I wanted to find a list of User objects from the production server, I just have to change some of the paths

PROD_SERVER = 'myappid.appspot.com'
PROD_PATH = '/_ah/remote_api'
PROD_SECURE = True
remote_api_stub.ConfigureRemoteApiForOAuth(PROD_SERVER, PROD_PATH, secure=PROD_SECURE)
users = User.query()
# at this point users will contain data from the PRODUCTION server

Both of these work as expected and will return me the correct data.

The problem starts when I try to set up the remote_api_sub inside any piece of code that is actually running on the dev_appserver.py instance.

For example if I try to configure the stub on any basic request handler, I run into some issues.

If I use remote_api_stub.ConfigureRemoteApiForOAuth, using the same params as before, I get a NotSupportedOnThisPlatform error

INFO     2016-08-30 22:01:40,362 module.py:787] admin: "GET /_ah/start HTTP/1.1" 404 52
INFO     2016-08-30 22:01:47,342 client.py:539] Attempting refresh to obtain initial access_token
INFO     2016-08-30 22:01:47,461 client.py:797] Refreshing access_token
ERROR    2016-08-30 22:01:47,607 webapp2.py:1552] 
Traceback (most recent call last):
  File "/path/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "/path/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "/path/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/path/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/path/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/path/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "/path/app/admin/controllers/controller.py", line 156, in get
    remote_api_stub.ConfigureRemoteApiForOAuth(PROD_SERVER, PROD_PATH, secure=PROD_SECURE)
  File "/path/google_appengine/google/appengine/ext/remote_api/remote_api_stub.py", line 768, in ConfigureRemoteApiForOAuth
    rpc_server_factory=rpc_server_factory)
  File "/path/google_appengine/google/appengine/ext/remote_api/remote_api_stub.py", line 835, in ConfigureRemoteApi
    app_id = GetRemoteAppIdFromServer(server, path, rtok)
  File "/path/google_appengine/google/appengine/ext/remote_api/remote_api_stub.py", line 569, in GetRemoteAppIdFromServer
    response = server.Send(path, payload=None, **urlargs)
  File "/path/google_appengine/google/appengine/tools/appengine_rpc_httplib2.py", line 247, in Send
    url, method=method, body=payload, headers=headers)
  File "/path/google_appengine/lib/oauth2client/oauth2client/client.py", line 562, in new_request
    redirections, connection_type)
  File "/path/google_appengine/lib/httplib2/httplib2/__init__.py", line 1464, in request
    self.disable_ssl_certificate_validation)
  File "/path/google_appengine/lib/httplib2/httplib2/__init__.py", line 1143, in __init__
    strict, timeout, proxy_info, ca_certs, disable_ssl_certificate_validation)
  File "/path/google_appengine/lib/httplib2/httplib2/__init__.py", line 1092, in __init__
    raise NotSupportedOnThisPlatform()
NotSupportedOnThisPlatform
INFO     2016-08-30 22:01:47,612 module.py:787] admin: "GET /controller1 HTTP/1.1" 500 2937

So I continued looking and then found this very similar question and answer:here

However, it looks like they are using remote_api_stub.ConfigureRemoteApi instead of remote_api_stub.ConfigureRemoteApiForOAuth.

When I try to use

remote_api_stub.ConfigureRemoteApi(app_id=None, path='/_ah/remote_api',
                                   auth_func=lambda: ('myemail', 'mypass'),
                                   servername='myappid.appspot.com')

I get

ERROR    2016-08-30 22:06:58,652 webapp2.py:1552] HTTP Error 404: Not Found
Traceback (most recent call last):
  File "/path/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "/path/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "/path/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/path/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/path/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/path/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "/path/app/admin/controllers/controller.py", line 154, in get
    servername='myappid.appspot.com')
  File "/path/google_appengine/google/appengine/ext/remote_api/remote_api_stub.py", line 835, in ConfigureRemoteApi
    app_id = GetRemoteAppIdFromServer(server, path, rtok)
  File "/path/google_appengine/google/appengine/ext/remote_api/remote_api_stub.py", line 569, in GetRemoteAppIdFromServer
    response = server.Send(path, payload=None, **urlargs)
  File "/path/google_appengine/google/appengine/tools/appengine_rpc.py", line 409, in Send
    self._Authenticate()
  File "/path/google_appengine/google/appengine/tools/appengine_rpc.py", line 555, in _Authenticate
    super(HttpRpcServer, self)._Authenticate()
  File "/path/google_appengine/google/appengine/tools/appengine_rpc.py", line 293, in _Authenticate
    auth_token = self._GetAuthToken(credentials[0], credentials[1])
  File "/path/google_appengine/google/appengine/tools/appengine_rpc.py", line 232, in _GetAuthToken
    response = self.opener.open(req)
  File "/usr/lib/python2.7/urllib2.py", line 410, in open
    response = meth(req, response)
  File "/usr/lib/python2.7/urllib2.py", line 523, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python2.7/urllib2.py", line 448, in error
    return self._call_chain(*args)
  File "/usr/lib/python2.7/urllib2.py", line 382, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 531, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 404: Not Found
INFO     2016-08-30 22:06:58,656 module.py:787] admin: "GET /controller1 HTTP/1.1" 500 3129

If I try to use (setting the app_id myself)

remote_api_stub.ConfigureRemoteApi(app_id=myappid, path='/_ah/remote_api',
                                   auth_func=lambda: ('myemail', 'mypass'),
                                   servername='myappid.appspot.com')

I get the same HTTP Error 404 as before

As far as the email and password combo to use when not using OAuth, I have tried the username/password that has ownership over the app and that didn't seem to work.

I had to set up a service account with the secret key in order for the command line ConfigureRemoveApiForOAuth stuff to work. I was wondering about using that email remote-api-service@myappid.iam.gserviceaccount.com , but I wasn't sure what the password would be so I figured that probably isn't correct.

I have also tried setting up the remote_api_stub inside of appengine_config.py instead of in the individual handlers and all of the problems are the same.

I've also tried this with both with vm: true and without in the app.yaml, and it doesn't seem to make a difference.

To sum it up: Is it possible to use the remote_api_stub to connect to a google app engine datastore from code within the google app itself? I can already access the datastores fine from OUTSIDE the application, but any code running inside seems to not work.

Thanks

edit: The account in question has 'allow less secure apps to log in or whatever setting that is in google set to ON'

Community
  • 1
  • 1
user2209486
  • 61
  • 2
  • 4

1 Answers1

0

I was able to fix this by using the gcloud library.

See this answer: https://stackoverflow.com/a/34575005/2209486

Community
  • 1
  • 1
user2209486
  • 61
  • 2
  • 4