【问题标题】:Getting a 401 Unauthorized while accessing appengine environments using remote api使用远程 api 访问 appengine 环境时获得 401 Unauthorized
【发布时间】:2018-05-15 18:37:54
【问题描述】:

我在本地机器上使用远程 api 访问一些 appengine 项目时遇到了一些问题。它以 401 - urllib2.HTTPError: HTTP Error 401: Unauthorized Too many auth attempts 失败。

我正在使用以下命令来启动远程 api shell:python /usr/lib/google-cloud-sdk/platform/google_appengine/remote_api_shell.py -s <version>-dot-<my appengine project>.appspot.com

我使用的是 gcloud 版本 200.0.0。在我运行 gcloud auth application-default login 并完成 Web 身份验证流程后,我的凭据已保存到 ~/.config/gcloud/application_default_credentials.json。我已确认将相同的代码部署到我们所有的 appengine 环境 并且在app.yaml中也设置了以下内容

builtins:
- remote_api: on

根据https://cloud.google.com/appengine/docs/standard/python/tools/remoteapi

我们有不少 appengine 项目。此命令适用于所有这些,但始终在其中 2 个上失败。根据 IAM 和管理页面,我的电子邮件被列为其失败的两个环境之一的所有者(我认为它至少应该在该环境中工作,但事实并非如此)。我还需要做什么才能远程访问环境吗?或者最近有没有其他人经历过这种情况。

在下面附加完整的堆栈跟踪

Traceback (most recent call last):
  File "/usr/lib/google-cloud-sdk/platform/google_appengine/remote_api_shell.py", line 133, in <module>
    run_file(__file__, globals())
  File "/usr/lib/google-cloud-sdk/platform/google_appengine/remote_api_shell.py", line 129, in run_file
    execfile(_PATHS.script_file(script_name), globals_)
  File "/usr/lib/google-cloud-sdk/platform/google_appengine/google/appengine/tools/remote_api_shell.py", line 160, in <module>
    main(sys.argv)
  File "/usr/lib/google-cloud-sdk/platform/google_appengine/google/appengine/tools/remote_api_shell.py", line 156, in main
    oauth2=True)
  File "/usr/lib/google-cloud-sdk/platform/google_appengine/google/appengine/tools/remote_api_shell.py", line 74, in remote_api_s
hell
    secure=secure, app_id=appid)
  File "/usr/lib/google-cloud-sdk/platform/google_appengine/google/appengine/ext/remote_api/remote_api_stub.py", line 1052, in Co
nfigureRemoteApiForOAuth
    rpc_server_factory=rpc_server_factory)
  File "/usr/lib/google-cloud-sdk/platform/google_appengine/google/appengine/ext/remote_api/remote_api_stub.py", line 1137, in Co
nfigureRemoteApi
    app_id = GetRemoteAppIdFromServer(server, path, rtok)
  File "/usr/lib/google-cloud-sdk/platform/google_appengine/google/appengine/ext/remote_api/remote_api_stub.py", line 842, in Get
RemoteAppIdFromServer
    response = server.Send(path, payload=None, **urlargs)
  File "/usr/lib/google-cloud-sdk/platform/google_appengine/google/appengine/tools/appengine_rpc_httplib2.py", line 259, in Send
    NeedAuth()
  File "/usr/lib/google-cloud-sdk/platform/google_appengine/google/appengine/tools/appengine_rpc_httplib2.py", line 235, in NeedA
uth
    RaiseHttpError(url, response_info, response, 'Too many auth attempts.')
  File "/usr/lib/google-cloud-sdk/platform/google_appengine/google/appengine/tools/appengine_rpc_httplib2.py", line 85, in RaiseH
ttpError
    raise urllib2.HTTPError(url, response_info.status, msg, response_info, stream)
urllib2.HTTPError: HTTP Error 401: Unauthorized Too many auth attempts.

【问题讨论】:

  • 呃,我今天早上也遇到了同样的事情。你发现问题了吗?

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


【解决方案1】:

我注册了 Google Cloud 付费支持以解决此问题。花了一点时间,但解决方案是像这样添加--secure 标志:

python remote_api_shell.py --secure app-name

我不知道幕后发生了什么变化,所以现在需要这个额外的标志。我最近没有对我的网站或项目进行任何可能导致此问题的更改。

同样麻烦的是remote_api_shell.py的默认模式是不安全的,但至少有解决办法。

以下是我不再需要的原始答案。


我能够让它工作,但它有点像 hack,我想以正确的方式修复它。

这是破解:

  • 在 Cloud Console 上创建一个新的服务帐号并使其成为项目所有者
  • 将密钥下载为 JSON 文件
  • export GOOGLE_APPLICATION_CREDENTIALS=[PATH TO JSON FILE]

现在remote_api_shell.py 有效。

看起来以前的服务帐户之一已损坏,但我不知道是怎么回事。

【讨论】:

    【解决方案2】:

    remote_api_shell.py 使用存储在此处的 API 凭据:

    ~/.config/gcloud/application_default_credentials.json
    

    您可以重命名该文件并运行命令'gcloud auth login' 以重新初始化登录凭据。这适用于几种类似的情况。

    编辑:

    使用这样的命令强制作用域可能也是值得的:

    gcloud auth application-default login --scopes='https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/userinfo.email'
    

    您可以找到有关范围here 的更多详细信息。它们应该默认设置,但可能值得试一试。

    【讨论】:

    【解决方案3】:

    我不是这方面的专家,但应用程序默认凭据似乎与用户帐户相关联,而不是与应用的服务帐户相关联。 (我假设这是因为我可以使用从gcloud auth application-default login 获得的单个凭据使用 remote_api_shell.py 连接到我的两个应用程序。

    您使用的用户帐户是否有可能未列为无法运行的应用的所有者或编辑者?在 gaefan 的案例中,他们可以创建新的服务帐户并使其成为所有者这一事实表明不是,但值得检查的是您没有遇到这种情况:

    • 您已在一个浏览器中以 your.name@gmail.com 身份登录到 console.cloud.google.com。

    • 您已在其他浏览器中以 your.name@company.com 身份登录到 console.cloud.google.com,并且可以访问那里的应用程序。

    • gcloud auth application-default login 在第一个浏览器中弹出登录/授权流程,并为 your.name@gmail.com 生成一个应用程序默认凭据,当您确实需要 your.name@company.com 时。

    【讨论】:

      猜你喜欢
      • 2019-03-16
      • 2017-10-30
      • 2017-01-25
      • 2022-12-30
      • 2017-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-10
      相关资源
      最近更新 更多