简体   繁体   English

Python3:Azure Key Vault Keys,创建 RAS 密钥:TypeError

[英]Python3 : Azure Key Vault Keys, creating RAS key : TypeError

Following example in :以下示例中:

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

whenever I run the below code :每当我运行以下代码时:

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

The key will be created in my Azure keyVult, however I always get the following error :密钥将在我的 Azure keyVult 中创建,但是我总是收到以下错误:

Traceback (most recent call last): File "c:/Users/user/Desktop/azure_keyValut_demo.py", line 152, in rsa_key = key_client.create_rsa_key("rsa-key-demo", size=2048) File "C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\azure\\core\\tracing\\decorator.py", line 62, in wrapper_use_tracer return func(*args, **kwargs) # type: ignore File "C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\azure\\keyvault\\keys_client.py", line 131, in create_rsa_key return self.create_key(name, key_type="RSA-HSM" if hsm else "RSA", **kwargs) File "C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\azure\\core\\tracing\\decorator.py", line 62, in wrapper_use_tracer return func(*args, **kwargs) # type: ignore File "C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\azure\\keyvault\\keys_client.py", line 96, in create_key **kwargs File "C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\azure\\keyvault\\keys_shared_gener回溯(最近一次调用):文件“c:/Users/user/Desktop/azure_keyValut_demo.py”,第 152 行,在 rsa_key = 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 中 return func(*args, **kwargs) # 输入:忽略文件“C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\azure\\keyvault\\keys_client.py”,第 131 行,在 create_rsa_key 中 return self.create_key(name, key_type=" RSA-HSM" if hsm else "RSA", **kwargs) 文件 "C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\azure\\core\\tracing\\decorator.py" , line 62, in wrapper_use_tracer return func(*args, **kwargs) # type: ignore File "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_gener ated\\v7_0\\operations_key_vault_client_operations.py", line 96, in create_key deserialized = self._deserialize('KeyBundle', response) File "C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\msrest\\serialization.py", line 1180, in call data = self._unpack_content(response_data, content_type) File "C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\msrest\\serialization.py", line 1334, in unpack_content return json.loads(data) File "C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python36\\lib\\json_init.py", line 348, in loads 'not {!r}'.format(s.class.name)) TypeError: the JSON object must be str, bytes or bytearray, not 'method' ated\\v7_0\\operations_key_vault_client_operations.py”,第 96 行,在 create_key deserialized = self._deserialize('KeyBundle', response) 文件“C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages \\msrest\\serialization.py”,第 1180 行,在调用 data = self._unpack_content(response_data, content_type) 文件“C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\msrest\\”中serialization.py", line 1334, in unpack_content return json.loads(data) File "C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python36\\lib\\json_init.py", line 348, in loading 'not {!r}'.format(s.class.name)) TypeError: the JSON object must be str, bytes or bytearray, not 'method'


I didn't quite understand why that exception is being thrown.我不太明白为什么会抛出该异常。

I found this on stackoverflow but still couldn't understand what's going on:我在 stackoverflow 上发现了这个,但仍然无法理解发生了什么:

Why won't python3 load this json string? 为什么python3不会加载这个json字符串?


The code run successfully with no issues with Python2.7代码运行成功,Python2.7 没有问题

The exception will be thrown for all key operations (create, delete, get)所有关键操作(创建、删除、获取)都会抛出异常

It also happens with all Cryptographic operations它也发生在所有加密操作中

According to my test, if we use msrest under version 0.6.x, we will get the error.根据我的测试,如果我们使用msrest下版本0.6.x,我们会得到错误。 My test steps are as below我的测试步骤如下

My requirements.txt我的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

My code我的代码

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)

在此处输入图片说明

So, please update the merest to 0.6.x.所以,请更新merest到0.6.x. The detailed steps are as below详细步骤如下

  1. Create project创建项目
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. install SDK My requirements.txt安装 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. Code代码
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)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM