如何使用google.api_core.operations_v1.AbstractOperationsClient获取长时间运行的操作

0 投票
0 回答
16 浏览
提问于 2025-04-12 02:35

我用下面的代码创建了一个新的磁盘:

disk_client = compute_v1.DisksClient()
request = compute_v1.InsertDiskRequest(project=PROJECT, zone=ZONE,disk_resource=disk)
operation = disk_client.insert(project=PROJECT, zone=ZONE, disk_resource=disk)

通过gcloud我可以看到我的操作:

gcloud compute operations describe operation-1711690161380-614c5ec08dd85-8f424625-2dfb72a1 --zone europe-west3-b

endTime: '2024-03-28T22:29:43.198-07:00'
id: '2781620015981129566'
insertTime: '2024-03-28T22:29:21.946-07:00'
kind: compute#operation
name: operation-1711690161380-614c5ec08dd85-8f424625-2dfb72a1
operationType: insert
progress: 100
selfLink: https://www.googleapis.com/compute/v1/projects/my-project/zones/europe-west3-b/operations/operation-1711690161380-614c5ec08dd85-8f424625-2dfb72a1
startTime: '2024-03-28T22:29:21.957-07:00'
status: DONE
targetId: '5681157969428606814'
targetLink: https://www.googleapis.com/compute/v1/projects/my-project/zones/europe-west3-b/disks/rst99-vtlnprd01-disk-1
user: my-service-account@my-project.iam.gserviceaccount.com
zone: https://www.googleapis.com/compute/v1/projects/my-project/zones/europe-west3-b
 

在另一个脚本中,我想用google.api_core.operations_v1.AbstractOperationsClient这个类来获取长时间运行的操作的结果,但总是返回一个凭证问题!

这是我的代码:

os.environ['GOOGLE_APPLICATION_CREDENTIALS']="..\\..\\my-service-account.json"

name = f"projects/{PROJECT}/zones/{ZONE}/operations/operation-1711690161380-614c5ec08dd85-8f424625-2dfb72a1"

credentials, projectId = google.auth.default() 
print("credentials service account: ", credentials.service_account_email)
options = ClientOptions(api_endpoint="compute.googleapis.com/compute")
operation_client = AbstractOperationsClient(credentials=credentials, client_options=options)
operation = operation_client.get_operation(name)

但是我遇到了以下错误:

credentials service account:  my-service-account@my-project.iam.gserviceaccount.com
Traceback (most recent call last):
  File "C:\Users\john.do\operation.py", line 22, in <module>
    operation = operation_client.get_operation(name)
  File "C:\Users\john.do\AppData\Local\Programs\Python\Python310\lib\site-packages\google\api_core\operations_v1\abstract_operations_client.py", line 495, in get_operation
    response = rpc(
  File "C:\Users\john.do\AppData\Local\Programs\Python\Python310\lib\site-packages\google\api_core\gapic_v1\method.py", line 131, in __call__
    return wrapped_func(*args, **kwargs)
  File "C:\Users\john.do\AppData\Local\Programs\Python\Python310\lib\site-packages\google\api_core\retry\retry_unary.py", line 293, in retry_wrapped_func
    return retry_target(
  File "C:\Users\john.do\AppData\Local\Programs\Python\Python310\lib\site-packages\google\api_core\retry\retry_unary.py", line 153, in retry_target
    _retry_error_helper(
  File "C:\Users\john.do\AppData\Local\Programs\Python\Python310\lib\site-packages\google\api_core\retry\retry_base.py", line 212, 
in _retry_error_helper
    raise final_exc from source_exc
  File "C:\Users\john.do\AppData\Local\Programs\Python\Python310\lib\site-packages\google\api_core\retry\retry_unary.py", line 144, in retry_target
    result = target()
  File "C:\Users\john.do\AppData\Local\Programs\Python\Python310\lib\site-packages\google\api_core\grpc_helpers.py", line 79, in error_remapped_callable
    return callable_(*args, **kwargs)
  File "C:\Users\john.do\AppData\Local\Programs\Python\Python310\lib\site-packages\google\api_core\operations_v1\transports\rest.py", line 301, in _get_operation
    raise core_exceptions.from_http_response(response)
google.api_core.exceptions.Unauthorized: 401 GET https://compute.googleapis.com/compute/v1/projects/my-project/zones/europe-west3-b/operations/operation-1711690161380-614c5ec08dd85-8f424625-2dfb72a1: Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.

我使用的是同一个服务账号来创建磁盘和获取操作,你能帮我吗?

谢谢

0 个回答

暂无回答

撰写回答