python - google.api_core.exceptions.PermissionDenied 但服务帐户具有所需的权限
问题描述
我目前正在使用 python GCP API 创建云任务队列。我的代码是从示例代码修改的,逻辑是检查队列是否存在,如果不存在则创建一个新队列并将新任务放入该队列。所以我使用 try-except 和 importfrom google.api_core import exceptions
来处理错误。但是现在的问题一直是我的服务帐户没有权限云任务。这是错误。
google.api_core.exceptions.PermissionDenied
google.api_core.exceptions.PermissionDenied: 403 The principal (user or service account) lacks IAM permission "cloudtasks.tasks.create" for the resource "projects/xxxx/locations/us-central1" (or the resource may not exist).
这是我的代码。
@app.route('/train_model/<dataset_name>/<dataset_id>/', methods=["POST", "GET"])
def train_model(dataset_name,dataset_id):
if request.method == 'POST':
form = request.form
model = form.get('model_name')
date = form.get('date')
datetime_object = datetime.strptime(date, '%Y-%m-%d %H:%M:%S')
timezone = pytz.timezone('Asia/Hong_Kong')
timezone_date_time_obj = timezone.localize(datetime_object)
data=[dataset_id,model]
payload = str(data).encode()
# Create a client.
url = "https://us-central1-xxx.cloudfunctions.net/create_csv"
try:
client = tasks_v2.CloudTasksClient.from_service_account_json(
'./xxxxx.json')
url = "https://us-central1-xxxxxx.cloudfunctions.net/create_csv"
location = 'us-central1'
project = 'xxxxx'
queue = 'testing1'
parent = client.location_path(project, location)
task = {
"http_request": {
'http_method': 'POST',
'url': url,
'body': payload
}}
# set schedule time
timestamp = timestamp_pb2.Timestamp()
timestamp.FromDatetime(timezone_date_time_obj)
task['schedule_time'] = timestamp
response = client.create_task(parent, task)
except exceptions.FailedPrecondition:
location = 'us-central1'
project = 397901391776
# Create a client.
client = tasks_v2.CloudTasksClient.from_service_account_json(
"./xxxx.json")
parent = client.location_path(project, location)
queue = {"name": 'x'}
queue.update(name="projects/xxxxx/locations/us-west2/queues/" + queue #the name of the queue from try.)
response = client.create_queue(parent, queue)
parent = client.queue_path(project, location, queue)
task = {
"http_request": {
'http_method': 'POST',
'url': url,
'body':payload
}}
# set schedule time
timestamp = timestamp_pb2.Timestamp()
timestamp.FromDatetime(timezone_date_time_obj)
task['schedule_time'] = timestamp
response = client.create_task(parent, task)
print(response)
return redirect('/datasetinfo/{}/{}/'.format(dataset_name,dataset_id))
解决方案
我已经重现了您的场景,并且设法解决了同样的问题。问题不在于身份验证,而是资源不存在。
为了获取资源路径,而不是使用location_path
你应该使用的函数queue_path
。这样,变量parent
将包含队列的名称,调用create_task
将能够找到资源。
最后,将角色编辑器授予服务帐户可能太多了,您应该将访问权限限制在最小可行范围内。如果此代码只需要创建任务,在这种情况下,您应该创建一个仅具有所需权限的自定义角色。cloudtasks.tasks.create
推荐阅读
- arrays - Laravel collect($array)->sortBy($value)->values() 显示在屏幕上
- python - 数据框中的向量化字符串搜索
- r - 改进 R 中的现有循环和延迟
- java - 用坐标、长度、宽度和角度计算矩形的中心
- python - 如何将 arg 从命令传递到子命令 Discord py
- c# - 将文件上传到 Blob 存储错误 get_NetworkTimeout 没有实现
- typoscript - Typo3:语言没有 isoCodeA2
- pyparsing - 将文本“DA23.3445 DC15.445”解析为 [“DA”、“23.3445”、“DC”、“15.445”]
- python - matplotlib 中未定义 ax
- html - 如何将所有 html 组件移动到页面的中心?