【问题标题】:How can I authorize to send a GET request from one GCLOUD function to another如何授权从一个 GCLOUD 函数向另一个函数发送 GET 请求
【发布时间】:2022-10-15 01:00:46
【问题描述】:

尝试通过 GET 访问 gcloud 函数的端点以触发 http 函数。我需要做的就是使用一些参数值来触发该函数。

我们不允许对这些功能进行未经身份验证,因此我需要进行身份验证才能发送它,但在我的一生中无法找到有关如何执行此操作的工作示例。

我已经阅读了this,并且在他们的文档中的链接之后完全绕圈子试图找到我需要做些什么来设置它。

当当天所有内容都更新时,我正在点击的功能会发送一条消息

https.get(`${endPoint}`, (resp) => {
  // The whole response has been received. Print out the result.
  resp.on('end', () => {
    console.log(JSON.parse(data).explanation); //  Should return the text set int he function, current, "It's Done"
    res.status(200).send(`SQL INSERTs have all been run for client(${clientId}) and they have been notified`);
  });
}).on("error", (err) => {
  console.log("Error: " + err.message);
  res.status(200).send(`There was an error running SQL INSERTs  for client(${clientId}) and they have not been notified, error ${err.message}`);
});

在我试图点击它的函数的日志中返回

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 Additional troubleshooting documentation can be found at: https://cloud.google.com/run/docs/troubleshooting#unauthorized-client

所以我特别想弄清楚我需要做什么,以便通过 GET 请求进行身份验证并点击端点。

自发布此问题以来,我还创建了一个服务帐户并下载了设置为GOOGLE_APPLICATION_CREDENTIALS 的凭据,因此如果有使用该 JSON 文件的解决方案,我也可以尝试。

【问题讨论】:

  • 今天看到了 20 次,该文档很糟糕,它对我所做的只是产生错误。然而,我确实得到了一个工作示例,它看起来与任何文档都不一样,但只要它有效,我就很高兴。

标签: google-cloud-functions gcloud


【解决方案1】:

终于找到了一个可行的解决方案。

我在尝试授权时最终使用了这样的 JSON 凭据文件

const auth = new GoogleAuth({credentials: {
    client_email: `${GOOGLE_APPLICATION_CREDENTIALS.client_email}`,
    private_key: `${GOOGLE_APPLICATION_CREDENTIALS.private_key}`
  }
});

...set URL and all that...

const client = await auth.getIdTokenClient(targetAudience);

哪个有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-04
    • 1970-01-01
    • 2015-07-25
    • 2018-08-11
    • 1970-01-01
    • 1970-01-01
    • 2021-02-24
    • 2021-02-12
    相关资源
    最近更新 更多