首页 文章

使用App Engine凭据进行身份验证,以使用OpenAPI与Google Cloud Endpoints上的应用进行通信

提问于
浏览
0

如果您使用Google Auth Library for Python验证App Engine应用程序与在Compute Engine上运行的应用程序进行通信,该应用程序使用Extensible Service Proxy (ESP)作为Cloud Endpoints的一部分与OpenAPI进行通信,则会收到错误响应:

{
  "code": 16,
  "message": "JWT validation failed: BAD_FORMAT",
  "details": [{
      "@type": "type.googleapis.com/google.rpc.DebugInfo",
      "stackEntries": [],
      "detail": "auth"
  }]
}

我找到的唯一sample Cloud Endpoints code不使用任何库,而是手动构建JSON Web令牌(JWT)并完全手动设置正确的HTTP头 . 有没有办法用标准库来实现它,所以你可以获得它带来的所有其他好处?

1 回答

  • 1

    问题是由于 google.auth.app_engine.Credentials() 在其构建的JWT中不包含 aud 声明,而ESP需要它 . (我希望我之前找到了Troubleshooting JWT Validation页面,这样我就可以节省数小时的费用 . )

    以下是如何构建ESP将接受的凭据:

    import google.auth.app_engine
    import google.auth.jwt
    from google.appengine.api import app_identity
    
    AUDIENCE = '...'
    
    credentials = google.auth.jwt.Credentials(
        signer=google.auth.app_engine.Signer(),
        issuer=app_identity.get_service_account_name(),
        subject=app_identity.get_service_account_name(),
        audience=AUDIENCE)
    

    其中 AUDIENCE 必须与您的OpenAPI文件中的 x-google-audiences 值匹配 or 您的Cloud Endponts服务名称(有关详细信息,请参阅上面链接的故障排除文档) .

    只要您传递 --appidentity_private_key_path--appidentity_email_address 标志,此代码甚至可以与 dev_appserver.py 和服务帐户一起使用 . 但是'll need to convert your service account'的私钥是 dev_appserver.py 接受的格式,因为它不支持Google Cloud Console可以为您提供的任何格式 . 有关说明,请参阅this bug comment .

相关问题