我可以使用Google应用中的remote_api_stub连接到远程/生产数据存储吗?

时间:2016-08-30 22:40:55

标签: google-app-engine google-app-engine-python

我想知道是否可以在本地dev_appserver.py上运行的脚本中使用remote_api_stub。

我已经成功地通过remote_api_stub从命令行运行的基本脚本连接到开发/生产数据存储区,所以我非常有信心app.yaml设置正确。

如果我想从本地开发服务器中找到User对象列表,我可以简单地设置存根以连接到本地服务器,然后像往常一样获取ndb模型。

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

如果我想从生产服务器中找到用户对象列表,我只需要更改一些路径

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

这两项工作都按预期工作,并会返回正确的数据。

当我尝试在dev_appserver.py实例上实际运行的任何代码段内设置remote_api_sub时,问题就开始了。

例如,如果我尝试在任何基本请求处理程序上配置存根,我会遇到一些问题。

如果我使用remote_api_stub.ConfigureRemoteApiForOAuth,使用与以前相同的参数,我会收到NotSupportedOnThisPlatform错误

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

所以我继续寻找然后发现了这个非常相似的问题和答案:here

但是,看起来他们使用的是remote_api_stub.ConfigureRemoteApi而不是remote_api_stub.ConfigureRemoteApiForOAuth。

当我尝试使用

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

我得到了

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

如果我尝试使用(自己设置app_id)

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

我得到了与之前相同的HTTP错误404

就不使用OAuth时使用的电子邮件和密码组合而言,我尝试过对应用程序拥有所有权的用户名/密码,但这似乎不起作用。

我必须使用密钥设置一个服务帐户,以便命令行ConfigureRemoveApiForOAuth工作。我想知道如何使用该电子邮件remote-api-service@myappid.iam.gserviceaccount.com,但我不确定密码是什么,所以我认为可能不正确。

我也尝试在appengine_config.py中设置remote_api_stub,而不是在各个处理程序中设置,所有问题都是一样的。

我也尝试过使用vm:true和app.yaml,但它似乎没有什么区别。

总结一下: 是否可以使用remote_api_stub从谷歌应用程序本身的代码连接到谷歌应用程序引擎数据库?我已经可以从应用程序的OUTSIDE访问数据存储了,但是在里面运行的任何代码似乎都不起作用。

由于

编辑:相关帐户“允许不太安全的应用程序登录或谷歌设置为ON的任何设置”

1 个答案:

答案 0 :(得分:0)

我能够通过使用gcloud库来解决这个问题。

看到这个答案: https://stackoverflow.com/a/34575005/2209486