【发布时间】:2019-09-16 06:29:16
【问题描述】:
我遇到了与谷歌云有关的 CORS 相关问题,该服务运行在
需要身份验证。
如果我尝试通过 cli 使用 Bearer 令牌执行 curl 命令,
一切正常。
不幸的是,如果我尝试在 javascript 中通过 ajax 执行相同的调用,
我收到了 403。
const http = new XMLHttpRequest();
const url = 'https://my-app.run.app';
http.open("GET", url);
http.withCredentials = true;
http.setRequestHeader("authorization", 'Bearer ' + id_token);
http.send();
http.onreadystatechange = (e) => {
console.log(http.responseText)
}
云运行日志中的错误是这样的:
The request was not authenticated. Either allow unauthenticated invocations or set the proper Authorization header. Read more at https://cloud.google.com/run/docs/securing/authenticating
容器永远不会被击中。
我看到的问题是,当我在网络中使用 ajax 进行调用时
浏览器。网络浏览器正在发出飞行前请求(
url )而不发送授权标头(这是预期的
行为)
问题似乎是云运行尝试验证 OPTIONS
请求并且永远不会到达我的容器,据我所知,
不应该这样做。 (
https://www.w3.org/TR/cors/#cross-origin-request-with-preflight-0)
这是云运行的已知问题吗?
如何向经过身份验证的云运行服务发出 ajax 请求?
【问题讨论】:
标签: ajax cors google-cloud-run