2016-08-30 48 views
0

我想知道是否可以在本地dev_appserver.py上運行的腳本中使用remote_api_stub。我可以使用google應用內的remote_api_stub連接到遠程/生產數據存儲嗎?

我已經成功地通過從命令行運行的基本腳本通過remote_api_stub連接到開發/生產數據存儲,所以我非常有信心app.yaml設置正確。

如果我想從我的本地開發服務器中找到用戶對象列表,我可以簡單地設置存根以連接到本地服務器,然後像往常一樣獲取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像以前一樣

As far作爲不使用OAuth時使用的電子郵件和密碼組合,我已經嘗試了對應用擁有所有權的用戶名/密碼,但似乎並不奏效。

爲了使命令行ConfigureRemoveApiForOAuth能夠正常工作,我必須使用密鑰設置一個服務帳戶。我想知道如何使用該電子郵件[email protected],但我不確定密碼是什麼,所以我想這可能是不正確的。

我也試過在appengine_config.py裏面設置remote_api_stub而不是在個別的處理程序中,所有的問題都是一樣的。

我也試過這兩個與vm:true和沒有在app.yaml,它似乎沒有區別。

總結一下: 是否可以使用remote_api_stub從谷歌應用程序本身的代碼連接到谷歌應用程序引擎數據存儲?我已經可以從應用程序的外部訪問數據存儲區了,但是裏面運行的任何代碼似乎都不起作用。

感謝

編輯:有問題的帳戶具有「允許安全性較低的應用程序登錄是在谷歌設置爲或任何設置ON」

回答

相關問題