收到错误代码:SubscriptionNotFound 消息:尝试获取有关 Azure 虚拟机的数据时找不到订阅?

Receiving the error Code: SubscriptionNotFound Message: The subscription not found when trying to get data about Azure Virtual Machine?

我正在编写一个脚本来访问当前有关 Azure 虚拟机的详细信息。这是我到目前为止的代码:

"""
    Instantiate the ComputeManagementClient with the appropriate credentials.

    @return ComputeManagementClient object
"""
def get_access_to_virtual_machine():
    subscription_id = key.SUBSCRIPTION_ID
    credentials = DefaultAzureCredential(authority = AzureAuthorityHosts.AZURE_GOVERNMENT, 
                                        exclude_environment_credential = True,
                                        exclude_managed_identity_credential = True,
                                        exclude_shared_token_cache_credential = True)
    client = KeyClient(vault_url = key.VAULT_URL, credential = credentials)                     
    compute_client = ComputeManagementClient(credentials, subscription_id)
    return compute_client

"""
    Check to see if Azure Virtual Machine exists and the state of the virtual machine.
"""
def get_azure_vm(resource_group_name, virtual_machine_name):
    compute_client = get_access_to_virtual_machine()
    vm_data = compute_client.virtual_machines.get(resource_group_name, 
                                                virtual_machine_name, 
                                                expand = 'instanceView')
    return vm_data

当尝试 运行 get_azure_vm(key.RESOURCE_GROUP, key.VIRTUAL_MACHINE_NAME) 时,我确信它具有正确的凭据,我得到以下错误输出(请注意,我将实际订阅 ID 替换为 'xxxx'现在):

Traceback (most recent call last):
  File "/Users/shilpakancharla/Documents/function_app/WeedsMediaUploadTrigger/event_process.py", line 62, in <module>
    vm_data = get_azure_vm(key.RESOURCE_GROUP, key.VIRTUAL_MACHINE_NAME)
  File "<decorator-gen-2>", line 2, in get_azure_vm
  File "/usr/local/lib/python3.9/site-packages/retry/api.py", line 73, in retry_decorator
    return __retry_internal(partial(f, *args, **kwargs), exceptions, tries, delay, max_delay, backoff, jitter,
  File "/usr/local/lib/python3.9/site-packages/retry/api.py", line 33, in __retry_internal
    return f()
  File "/Users/shilpakancharla/Documents/function_app/WeedsMediaUploadTrigger/event_process.py", line 55, in get_azure_vm
    vm_data = compute_client.virtual_machines.get(resource_group_name, 
  File "/usr/local/lib/python3.9/site-packages/azure/mgmt/compute/v2019_12_01/operations/_virtual_machines_operations.py", line 641, in get
    map_error(status_code=response.status_code, response=response, error_map=error_map)
  File "/usr/local/lib/python3.9/site-packages/azure/core/exceptions.py", line 102, in map_error
    raise error
azure.core.exceptions.ResourceNotFoundError: (SubscriptionNotFound) The subscription 'xxxx' could not be found.
Code: SubscriptionNotFound
Message: The subscription 'xxxx' could not be found.

我正在使用 azure.mgmt.compute 的 beta 预览版,它是随 pip install azure-mgmt-compute=17.0.0b1 安装的。请注意,我还使用了 Azure 政府帐户。有没有办法解决这个错误?我也尝试过使用 ServicePrincipalCredentialsget_azure_credentials() 但 运行 出现不同的错误 - 我被建议使用 DefaultAzureCredentials 和同事的密钥库。

代码没有问题,在我这边运行良好。并且错误消息显示了原因:

azure.core.exceptions.ResourceNotFoundError: (SubscriptionNotFound) The subscription 'xxxx' could not be found. Code: SubscriptionNotFound Message: The subscription 'xxxx' could not be found.

您似乎 运行 本地计算机中的 python 代码。我建议您先使用 Azure CLI 登录,然后检查您在 python 代码中使用的订阅 ID 是否正确。