Skip to main content
 首页 » 编程设计

python之使用 python sdk 连接 Azure 服务管理

2025年02月15日29soundcode

我正在使用本指南: https://learn.microsoft.com/en-us/azure/cloud-services/cloud-services-python-how-to-use-service-management . 我完全按照指南中的说明进行操作,但我不断收到错误消息。

>>> from azure import * 
>>> from azure.servicemanagement import * 
>>> subscription_id = '************************' 
>>> import os 
>>> os.path.isfile(r'c:\key\mycert.pem') 
True 
>>> certificate_path = r'c:\key\mycert.pem' 
>>> sms = ServiceManagementService(subscription_id, certificate_path) 
>>> result = sms.list_locations() 
Traceback (most recent call last): 
  File "<stdin>", line 1, in <module> 
  File "C:\python27\lib\site-packages\azure\servicemanagement\servicemanagementservice.py", line 1131, in list_locations 
Locations) 
  File "C:\python27\lib\site-packages\azure\servicemanagement\servicemanagementclient.py", line 365, in _perform_get 
response = self.perform_get(path, x_ms_version) 
  File "C:\python27\lib\site-packages\azure\servicemanagement\servicemanagementclient.py", line 175, in perform_get 
response = self._perform_request(request) 
  File "C:\python27\lib\site-packages\azure\servicemanagement\servicemanagementclient.py", line 339, in _perform_request 
return _management_error_handler(ex) 
  File "C:\python27\lib\site-packages\azure\servicemanagement\servicemanagementclient.py", line 419, in _management_error_handler 
return _general_error_handler(http_error) 
  File "C:\python27\lib\site-packages\azure\servicemanagement\_common_error.py", line 34, in _general_error_handler 
raise AzureHttpError(message, http_error.status) 
azure.common.AzureHttpError: Forbidden 
<Error xmlns="http://schemas.microsoft.com/windowsazure" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Code>ForbiddenError</Code><Message>The server failed to authenticate the request. Verify that the certificate is valid and is associated with this subscription.</Message></Error> 

我已将 mycert.cer 上传到我的 Azure 门户中的云服务。 有人知道为什么会出现问题吗? 我确定 pem 和 cer 文件没问题。

请您参考如下方法:

根据你的错误信息&Service Management Status and Error Codes的官方文档,问题原因如下错误信息所述。

根据我的经验,我认为有两个原因会导致这个问题。

  1. 您当前使用的证书在 Azure 上无效。另外重新生成一个新的证书引用官方document ,你也可以关注 Azure SDK for Python 的文档 Using the Azure .PublishSettings certificate创建客户端证书。

Using the Azure .PublishSettings certificate

You can download your Azure publish settings file and use the certificate that is embedded in that file to create the client certificate. The server certificate already exists, so you won’t need to upload one.

  1. 服务器证书文件未上传到 Azure 管理门户设置,如下所示。

作为引用,有一个 blog @GauravMantri 写的,对你很有帮助,详细介绍了步骤虽然它是针对 Java 的。

希望对您有所帮助。