AppEngine - 远程 API 返回 401 和太多身份验证

AppEngine - Remote API returning 401 and too-many-auth

我正在尝试使用远程 API 连接到 AppEngine 实例,方法如下:

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = ".../path/to/key"
remote_api_stub.ConfigureRemoteApiForOAuth(
    server.encode('ascii'), path, secure=True
)

其中 path/to/key 指向在 Google Cloud(测试版)中创建的 JSON 文件,API Manager -> Credentials ->添加凭据 -> 服务帐户。根据 权限 面板的服务帐户 "Can edit"。

当我 运行 这时,我在控制台上收到类似这样的错误,对应于 401(如 App Engine 日志查看器中所报告):

  File "/usr/local/share/app-engine-python/google/appengine/ext/remote_api/remote_api_stub.py", line 768, in ConfigureRemoteApiForOAuth
    rpc_server_factory=rpc_server_factory)
  File "/usr/local/share/app-engine-python/google/appengine/ext/remote_api/remote_api_stub.py", line 835, in ConfigureRemoteApi
    app_id = GetRemoteAppIdFromServer(server, path, rtok)
  File "/usr/local/share/app-engine-python/google/appengine/ext/remote_api/remote_api_stub.py", line 569, in GetRemoteAppIdFromServer
    response = server.Send(path, payload=None, **urlargs)
  File "/usr/local/share/app-engine-python/google/appengine/tools/appengine_rpc_httplib2.py", line 258, in Send
    NeedAuth()
  File "/usr/local/share/app-engine-python/google/appengine/tools/appengine_rpc_httplib2.py", line 234, in NeedAuth
    RaiseHttpError(url, response_info, response, 'Too many auth attempts.')
  File "/usr/local/share/app-engine-python/google/appengine/tools/appengine_rpc_httplib2.py", line 85, in RaiseHttpError
    raise urllib2.HTTPError(url, response_info.status, msg, response_info, stream)
urllib2.HTTPError: HTTP Error 401: Unauthorized Too many auth attempts.

我检查过 /google/appengine/ext/remote_api/remote_api_stub.py:760 中似乎设置了正确的凭据,应该传递给远程 api。

app.yamlbuiltins下有基本设置- remote_api: on

此设置适用于本地 dev_appserver.py,因此我认为问题出在 Google server/cloud 设置上。

我已经在 App Engine -> 设置 -> 应用程序设置(Google 帐户 API 和 Google 应用程序中尝试了两种身份验证方案域)。

我尽可能地遵循了 Google Application Default Credentials,但也许我错过了什么?

可能还值得注意的是,从命令行:

$ GOOGLE_APPLICATION_CREDENTIALS=~/path-to-key
    remote_api_shell.py -s APPID.appspot.com --secure APPID

也给出相同的 401 异常。

好的,看来需要的是:

  1. 转到 appspot.com 管理页面。
  2. 在应用程序设置下,启用云集成
  3. new 云控制台页面中创建一个新的服务帐户 API 管理器(现有的服务帐户似乎不起作用)
  4. 切换到 remote_api_stub.ConfigureRemoteApiFromServer — 经过进一步检查,似乎 ConfigureRemoteApiForOAuth 工作正常。

至少在按预期执行上述访问后。

我希望其他类似情况的人觉得这对您有所帮助。

对于从命令行(bulkloader.pyremote_api_shell.py 等)使用远程 api 的用户,如果您的应用引擎 cookie 已过期,也可能会发生 401 错误。

尝试删除它们并重新验证:

rm ~/.appcfg* gcloud auth login

这可能发生在对 Google 帐户进行更改强制注销(例如:启用 2 因素身份验证)的情况下。

刚遇到这个问题,这里没有任何解决办法。 唯一对我有用的是将 --secure 选项添加到远程 shell 命令

$APPENGINE/remote_api_shell.py  --secure -s my-app.appspot.com