云函数/云任务未经身份验证错误

Stf*_*f_F 8 service-accounts google-cloud-platform google-cloud-functions google-iam google-cloud-tasks

我正在尝试获取云函数来创建将调用云函数的云任务。简单的。

流程和用例与官方教程非常接近。

我还查看了 Doug Stevenson 的这篇文章,特别是其中的安全部分。

16 (UNAUTHENTICATED)不幸的是,我在 Cloud Task 中不断收到错误。

在此输入图像描述

如果我可以相信我在控制台中看到的内容,那么 Cloud Task 似乎没有将 OIDC 令牌附加到请求中:

在此输入图像描述

然而,在我的代码中我确实有这个oidcToken对象:

const { v2beta3, protos } = require("@google-cloud/tasks");
import {
  PROJECT_ID,
  EMAIL_QUEUE,
  LOCATION,
  EMAIL_SERVICE_ACCOUNT,
  EMAIL_HANDLER,
} from "./../config/cloudFunctions";


export const createHttpTaskWithToken = async function (
  payload: {
    to_email: string;
    templateId: string;
    uid: string;
    dynamicData?: Record<string, any>;
  },
  {
    project = PROJECT_ID,
    queue = EMAIL_QUEUE, 
    location = LOCATION, 
    url = EMAIL_HANDLER, 
    email = EMAIL_SERVICE_ACCOUNT,
  } = {}
) {
  const client = new v2beta3.CloudTasksClient();
  const parent = client.queuePath(project, location, queue);

  // Convert message to buffer.
  const convertedPayload = JSON.stringify(payload);
  const body = Buffer.from(convertedPayload).toString("base64");

  const task = {
    httpRequest: {
      httpMethod: protos.google.cloud.tasks.v2.HttpMethod.POST,
      url,
      oidcToken: {
        serviceAccountEmail: email,
        audience: new URL(url).origin,
      },
      headers: {
        "Content-Type": "application/json",
      },
      body,
    },
  };


  try {
    // Send create task request.
    const request = { parent: parent, task: task };
    const [response] = await client.createTask(request);
    console.log(`Created task ${response.name}`);
    return response.name;
  } catch (error) {
    if (error instanceof Error) console.error(Error(error.message));
    return;
  }
};
Run Code Online (Sandbox Code Playgroud)

当在 Cloud Logging 中通过上述代码记录任务对象时,我可以看到该服务帐户是我为此目的而创建的帐户,并且云任务已成功创建。

日志

我是: 在此输入图像描述

以及Cloud Task需要调用的函数:

要调用的函数

从理论上讲,一切似乎都在那里。

关于我会缺少什么有什么建议吗?

谢谢,

gui*_*ere 9

你的观众是不正确的。它必须以函数名称结尾。在这里,您只有区域和项目https://<region>-<projectID>.cloudfunction.net/。使用完整的 Cloud Functions URL。