gpt4 book ai didi

firebase - 谷歌云平台 : Cloud Functions and Cloud Tasks doesn't work with authorized service account email

转载 作者:行者123 更新时间:2023-12-05 06:09:02 24 4
gpt4 key购买 nike

我创建了一个服务帐户电子邮件并向电子邮件添加了 cloudfunctions.invoker 角色,这样我就可以确保只有云任务可以触发云功能,并且我删除了 AllUsers 角色.但是当云任务尝试运行云函数时,状态码为UNAUTHENTICATED(16): HTTP status code 401,执行失败。

我现在的代码和控制台是这样的。

index.ts

export const addTasks = functions.https.onCall((data, context) => {
if (!context.auth) {
throw new functions.https.HttpsError('failed-precondition', 'You are not authenticated.')
}

const client = new tasks.CloudTasksClient()

const projectId = functions.config().project.id
const queue = 'queue'
const location = functions.config().project.location
const parent = client.queuePath(projectId, location, queue)
const url = `https://${location}-${projectId}.cloudfunctions.net/executeSomething`
const serviceAccountEmail = functions.config().project.email

const task: tasks.protos.google.cloud.tasks.v2.ITask = {
httpRequest: {
httpMethod: 'POST',
url: url,
oidcToken: {
serviceAccountEmail: serviceAccountEmail,
},
},
scheduleTime: {
seconds: ...,
},
}

const request: tasks.protos.google.cloud.tasks.v2.ICreateTaskRequest = {
parent: parent,
task: task,
}

return client.createTask(request)
}

我的云函数的控制台

enter image description here

我将云函数调用者角色添加到服务帐户电子邮件中。

我的firebase项目环境变量

enter image description here

当我将 AllUsers 角色添加到云功能时,它按预期工作,所以我确信我在限制访问时犯了一个错误。我错过了什么?

更新:

我的云任务控制台

enter image description here enter image description here

最佳答案

对我来说,从 task 中的 oidcToken 对象中删除 audience: new URL(url).origin, 解决了 UNAUTHENTICATED(16 ): HTTP 状态码 401 问题。

const task = {
httpRequest: {
httpMethod: 'POST',
url,
oidcToken: {
serviceAccountEmail: email,
//audience: new URL(url).origin,
},
headers: {
'Content-Type': 'application/json',
},
body,
},
};

关于firebase - 谷歌云平台 : Cloud Functions and Cloud Tasks doesn't work with authorized service account email,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64962927/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com