Python3:Azure密钥保管库密钥,创建RAS密钥:TypeError

时间:2019-12-03 12:40:39

标签: python python-3.x azure azure-keyvault

以下示例:

https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/keyvault/azure-keyvault-keys

每当我运行以下代码时:

rsa_key = key_client.create_rsa_key("rsa-key-demo", size=2048)
print(rsa_key.name)
print(rsa_key.key_type)

密钥将在我的Azure keyVult中创建,但是我总是收到以下错误消息:

  

回溯(最近通话最近):文件   rsa_key中的“ c:/Users/user/Desktop/azure_keyValut_demo.py”,第152行=   key_client.create_rsa_key(“ rsa-key-demo”,size = 2048)文件   “ C:\ Users \ user \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ azure \ core \ tracing \ decorator.py”,   第62行,在wrapper_use_tracer中,返回func(* args,** kwargs)#类型:   忽略文件   “ C:\ Users \ user \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ azure \ keyvault \ keys_client.py”,   第131行,在create_rsa_key中返回self.create_key(name,   key_type =“ RSA-HSM”(如果为hsm则为“ RSA”,** kwargs)文件   “ C:\ Users \ user \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ azure \ core \ tracing \ decorator.py”,   第62行,在wrapper_use_tracer中,返回func(* args,** kwargs)#类型:   忽略文件   “ C:\ Users \ user \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ azure \ keyvault \ keys_client.py”,   第96行,位于create_key中   ** kwargs文件“ C:\ Users \ user \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ azure \ keyvault \ keys_shared_produced \ v7_0 \ operations_key_vault_client_operations.py”,   第96行,在create_key反序列化= self._deserialize('KeyBundle',   响应)文件   “ C:\ Users \ user \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ msrest \ serialization.py”,   第1180行,通话数据= self._unpack_content(response_data,   content_type)文件   “ C:\ Users \ user \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ msrest \ serialization.py”,   第1334行,在unpack_content中,返回json.loads(data)文件   “ C:\ Users \ user \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ json_init.py”,   第348行,在加载'not {!r}'。format(s.class.name))TypeError中:   JSON对象必须为str,bytes或bytearray,而不是“ method”


我不太明白为什么会抛出该异常。

我在stackoverflow上发现了这一点,但仍然不明白发生了什么:

Why won't python3 load this json string?


代码成功运行,Python2.7没有问题

所有关键操作(创建,删除,获取)都会引发异常

所有加密操作也都会发生

1 个答案:

答案 0 :(得分:0)

根据我的测试,如果我们在版本0.6.x下使用msrest,则会收到错误消息。 我的测试步骤如下

我的requirements.txt

azure-common==1.1.23
azure-core==1.1.1
azure-identity==1.1.0
azure-keyvault-keys==4.0.0
certifi==2019.11.28
cffi==1.13.2
chardet==3.0.4
cryptography==2.8
idna==2.8
isodate==0.6.0
msal==1.0.0
msal-extensions==0.1.3
msrest==0.5.5
oauthlib==3.1.0
portalocker==1.5.2
pycparser==2.19
PyJWT==1.7.1
pywin32==227
requests==2.22.0
requests-oauthlib==1.3.0
six==1.13.0
urllib3==1.25.7

我的代码

from azure.identity import ClientSecretCredential
from azure.keyvault.keys import KeyClient

tenant_id=''
client_id=''
client_secret=''
credential = ClientSecretCredential(tenant_id, client_id, client_secret)
client = KeyClient("https://testkey08.vault.azure.net/", credential)

key = client.create_rsa_key("rsa-key-demo", size=2048)
print(key.name)
print(key.key_type)

enter image description here

因此,请将merest更新为0.6.x。详细步骤如下

  1. 创建项目
python -m venv mytestenv # Might be "python3" or "py -3.6" depending on your Python installation
cd mytestenv
source bin/activate      # Linux shells (Bash, ZSH, etc.)
scripts\activate         # Windows shells (PowerShell, CMD)
  1. 安装SDK 我的requirements.txt
azure-common==1.1.23
azure-core==1.1.1
azure-identity==1.1.0
azure-keyvault-keys==4.0.0
certifi==2019.11.28
cffi==1.13.2
chardet==3.0.4
cryptography==2.8
idna==2.8
isodate==0.6.0
msal==1.0.0
msal-extensions==0.1.3
msrest==0.6.10
oauthlib==3.1.0
portalocker==1.5.2
pycparser==2.19
PyJWT==1.7.1
pywin32==227
requests==2.22.0
requests-oauthlib==1.3.0
six==1.13.0
urllib3==1.25.7
xlrd==1.2.0

pip install -r requirements.txt
  1. 代码
from azure.identity import ClientSecretCredential
from azure.keyvault.keys import KeyClient

tenant_id=''
client_id=''
client_secret=''
credential = ClientSecretCredential(tenant_id, client_id, client_secret)
client = KeyClient("https://testkey08.vault.azure.net/", credential)

key = client.create_rsa_key("rsa-key-demo", size=2048)
print(key.name)
print(key.key_type)

enter image description here