I have created a new disk using the code below:
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)
Using gcloud I can see my operation:
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: [email protected]
zone: https://www.googleapis.com/compute/v1/projects/my-project/zones/europe-west3-b
In a different script, I want to get the result of the long-running operation using the google.api_core.operations_v1.AbstractOperationsClient class, but always return a credential issue!
Here is the code:
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)
But I have the following error:
credentials service account: [email protected]
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.
I am using the same service account to create the disk and get the operation, can you help?
Thanks