[更新] 我发现问题出在哪里。我需要为任务 service-[project-number]@gcp-sa-cloudtasks.iam.gserviceaccount.com 创建帐户
我从 Google Cloud Task 调用 Google Cloud Function 时遇到问题。仅当我使用 oidc_token 参数时才会出现此问题。
我从这里使用了 Google Cloud Tasks 教程。我正在使用相同的服务帐户来创建任务并作为oidc_token
价值。我向该帐户添加了所有者角色。
这是一个代码:
from google.cloud import tasks_v2
from google.protobuf import timestamp_pb2
client = tasks_v2.CloudTasksClient()
project = '[Project]'
queue = 'fooqueue2'
location = 'asia-east2'
url = f'https://asia-east2-{project}.cloudfunctions.net/helloworld'
payload = None
in_seconds = 0
# Construct the fully qualified queue name.
parent = client.queue_path(project, location, queue)
# Construct the request body.
task = {
'http_request': {
'http_method': 'POST',
'url': url,
'oidc_token': {
'service_account_email': f"{project}@appspot.gserviceaccount.com"
}
}
}
print(list(client.list_tasks(parent)))
response = client.create_task(parent, task)
print('Created task {}'.format(response.name))
那么如果获取任务我看到
last_attempt {
schedule_time {
seconds: 1579860668
nanos: 184117000
}
dispatch_time {
seconds: 1579860668
nanos: 185066000
}
response_time {
seconds: 1579860668
nanos: 206251000
}
response_status {
code: 2
message: "UNKNOWN(2): HTTP status code 0"
}
}
所有用户均可使用的功能。我向此 service_account_email 添加了所有者、Cloud Functions Invoker、Service Account Token Creator、Cloud Tasks Service Agent 角色。