python - 将 Azure 服务管理与 python sdk 连接

标签 python python-2.7 azure sdk azure-service-fabric

我正在使用本指南: 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的官方文档,问题原因如下错误信息所述。

enter image description here

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

  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.

  • 服务器证书文件未上传到 Azure 管理门户设置中,如下所示。
  • enter image description here

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

    希望有帮助。

    关于python - 将 Azure 服务管理与 python sdk 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42981804/

    相关文章:

    python - 更新 Dataframe 列的有效方法

    Python从不同版本导入不同的库

    android - 测试 MobileServiceSQLiteStore 时无法加载文件或程序集 `SQLitePCL`

    Azure - 按主要版本从容器注册表中检索二头肌模块

    python crontab 替代方案 - APScheduler 和 python-daemon

    python - 如何将列表转换为 REST 的分页 JSON 响应?

    python-2.7 - pandas groupby 分组和亚组级别分析

    c# - 将 C# 控制台应用程序发布为 Azure Web 作业

    python - 带有 Flask 应用程序的 uwsgi 给出 "callable not found or import error"

    python - 在 Python 中插入 JSON 对象作为参数(转义引号)