云API网关不允许使用CORS

发布于 2025-01-25 12:55:07 字数 1251 浏览 4 评论 0原文

我有连接到云功能的云网关API。 它使用非常适合我生成的API密钥,但是当我限制了仅启用特定域访问的密钥时 - example.com,我开始获取CORS客户端错误。 (它在没有域限制的情况下起作用)

所以我在控制台中的config> config文件中启用了CORS,该文件没有错误,但是在访问API时,我仍然会遇到CORS错误来自我域中的键的URL:

https://project-xxxx.uc.gateway.dev/search?key=AIxxxxxxxxx

config:

swagger: "2.0"
info:
  title: projectapi
  description: "data api"
  version: "1.0.0"
schemes:
  - "https"
host: "project-xxxxxxxx.apigateway.xxxxx-xxxxx.cloud.goog"
x-google-endpoints:
- name: "project-xxxxxxxx.apigateway.xxxxx-xxxxx.cloud.goog"
  allowCors: True  
paths:
  "/search":
    get:
      description: "data"
      operationId: "project"
      x-google-backend:
        address: https://us-central1-projectName.cloudfunctions.net/search
      security:
        - api_key: [key]
      responses:
        200:
          description: "Success."
          schema:
            type: string
        400:
          description: "Invalid"

securityDefinitions:
 api_key:
    type: "apiKey"
    name: "key"
    in: "query" 

我的云功能确实允许CORS ,并且 works 很棒如果直接称为 ,则在此处的链接地址密钥。

为什么此配置不允许网关CORS?

I have Cloud Gateway API that connects to Cloud Function.
It worked great with an API key that I generated, but then when I restricted the key to enable access for my specific domain only - example.com, I started to get CORS errors on the client. ( it worked without domain restriction)

So I enabled CORS in the config file in the Console, the file had no errors, but I still get a CORS error when accessing the API URL from my domain with the key:

https://project-xxxx.uc.gateway.dev/search?key=AIxxxxxxxxx

Config :

swagger: "2.0"
info:
  title: projectapi
  description: "data api"
  version: "1.0.0"
schemes:
  - "https"
host: "project-xxxxxxxx.apigateway.xxxxx-xxxxx.cloud.goog"
x-google-endpoints:
- name: "project-xxxxxxxx.apigateway.xxxxx-xxxxx.cloud.goog"
  allowCors: True  
paths:
  "/search":
    get:
      description: "data"
      operationId: "project"
      x-google-backend:
        address: https://us-central1-projectName.cloudfunctions.net/search
      security:
        - api_key: [key]
      responses:
        200:
          description: "Success."
          schema:
            type: string
        400:
          description: "Invalid"

securityDefinitions:
 api_key:
    type: "apiKey"
    name: "key"
    in: "query" 

My Cloud Function does allow CORS, and works great if called directly with the link here in the address key.

Why this config wont allow CORS for the Gateway ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心安伴我暖 2025-02-01 12:55:07

解决!
您不仅必须为域名启用密钥,还必须从列表中启用特定 API名称,转到apis&服务=>凭证=> API键=>您的键=> API限制=>从列表中选择您的API名称!

此允许使用平衡器从特定域 - 访问函数 -

Solved !
You must enable the key not only for your domain name, but also for your specific API name from the list, go to APIs & Services => Credentials => API keys => your key => API restrictions => choose your API name from the list !

This allow access to a Function from a specific domain - without using a Balancer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文