将google端点中的path参数传递给后端不起作用 [英] passing path parameter in google endpoints to backend not working

查看:68
本文介绍了将google端点中的path参数传递给后端不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的设置包含带有google-cloud-functions作为后端的google端点.

My setup contains google-endpoints with google-cloud-functions as my backend.

Google端点是使用以下昂扬v2 yaml定义的:

Google endpoints is defined with the following swagger v2 yaml:

swagger: "2.0"
info:
  description: "yada..."
  version: "0.0.1"
  title: "yadada.."
  termsOfService: "http://swagger.io/terms/"
  contact:
    name: "blah"
    email: "email@mail.com"
    url: "https://example.com"
host: "(generated service url by google when endpoints is deployed, i.e. 'api-gateway-xyz123123-ew.a.run.app')"
tags:
  - name: "Documents"
    description: "blah"
schemes:
  - "https"
paths:
  /api/documents:
    post:
      tags:
        - "Documents"
      summary: "Add a new document"
      description: ""
      security:
        - firebase: []
      operationId: "addDocument"
      x-google-backend:
        address: "(cloud functions http url)/documents"
      consumes:
        - "application/json"
      produces:
        - "application/json"
      parameters:
        - in: "body"
          name: "body"
          description: "Document supplied"
          required: true
          schema:
            $ref: "#/definitions/Document"
      responses:
        201:
          description: "The document was successfully created."
          schema:
            $ref: "#/definitions/Document"
        400:
          description: "Invalid input. See response for details"
          schema:
            items:
              $ref: "#/definitions/Error"
  /api/documents/{document_id}:
    get:
      tags:
        - "Documents"
      summary: "Get a document with the given ID"
      description: ""
      security:
        - firebase: []
      operationId: "getDocument"
      x-google-backend:
        address: "(cloud function http url)/documents/"
        path_translation: APPEND_PATH_TO_ADDRESS
      produces:
        - "application/json"
      parameters:
        - in: "path"
          name: "document_id"
          description: "ID of the document to modify"
          required: true
          type: "string"
      responses:
        200:
          description: "success."
          schema:
            type: "array"
            items:
              $ref: "#/definitions/Document"
        404:
          description: "Document not found"
          schema:
            items:
              $ref: "#/definitions/Error"
securityDefinitions:
  firebase:
    authorizationUrl: ""
    flow: "implicit"
    type: "oauth2"
    x-google-issuer: "https://securetoken.google.com/%%GOOGLE_PROJECT_ID%%"
    x-google-jwks_uri: "https://www.googleapis.com/service_accounts/v1/metadata/x509/securetoken@system.gserviceaccount.com"
    x-google-audiences: "%%GOOGLE_PROJECT_ID%%"
definitions:
  (a lot of type definitions)

这可与POST端点一起使用,而不会出现任何问题.

This works with the POST endpoint without any problems.

问题在于GET REST端点,其中路径变量未正确传递到后端.

The problem is with the GET REST endpoint where the path variable is not passed correctly to the backend.

https://cloud.google.com/endpoints/docs中的一样/openapi/openapi-extensions 我尝试添加x-google-backend参数,如上面的swagger api一样.( path_translation:APPEND_PATH_TO_ADDRESS ).

As in https://cloud.google.com/endpoints/docs/openapi/openapi-extensions I tried to add the x-google-backend parameter as in the swagger api above. (path_translation: APPEND_PATH_TO_ADDRESS).

但是,这不起作用.我收到一个未经授权的错误(403),因为端点的前端未击中云功能.

However this does not work. I get an Unauthorized Error (403) as the cloud function is not hit by the endpoints frontend.

当前,我使用的丑陋的解决方法没有path_translation参数,该参数将google端点路径变量转换为具有相同名称的cloud函数后端中的查询参数.IE.在后端调用/documents?document_id = xyz 网址.

Currently I use an ugly workaround without the path_translation parameter which translates the google endpoints path variable to a query parameter in the cloud function backend with the same name. I.e. in the backend the url /documents?document_id=xyz is called.

(我想实现的目标是通过后端网址/documents/{document_id} 传递呼叫)

(What I try to achieve is to pass the call with the backend url /documents/{document_id})

有人知道如何正确配置基于路径的参数,以便将其正确传递给云功能后端吗?

Does anyone know how to configure path based parameters correctly so that they are passed correctly to the cloud function backend?

谢谢.

关于,塞巴斯蒂安

推荐答案

TL; DR:我认为您的403错误不是正确的错误.应该是404,但是因为端点未知,所以我猜想是403.

TL;DR: I assume that your 403 error isn't the correct error. It should be a 404, but because the endpoint is unknown, I guess that 403 is answered.

Cloud Endpoint对此行为感到沮丧.使用 path_translation:APPEND_PATH_TO_ADDRESS ,您认为您的最终呼叫地址将是/documents/{document_id} ,但.在您的情况下,完整的openAPI路径会附加到您的后端地址中:/documents/api/documents/{document_id}

Cloud Endpoint is frustrating about this behavior. With the path_translation: APPEND_PATH_TO_ADDRESS, you think that your final called address will be /documents/{document_id}, but NO. The full openAPI path is append to your backend address, in your case: /documents/api/documents/{document_id}

这就是为什么端点不存在,而您应该有一个404(而不是403)的原因.

That's why the endpoint doesn't exist and you should have a 404 (and not a 403).

有关更多详细信息,您可以查看此页面.

For more details, you can have a look to this page.

注意:我正在与Google团队就此主题进行交流,并且需要一段时间才能对此行为进行更新.

这篇关于将google端点中的path参数传递给后端不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆