Python GCP服务帐户密钥轮换
我正在尝试为GCP服务帐户实施关键轮换。我已经成功地创建了一个新密钥,然后解码了Python GCP服务帐户密钥轮换,python,json,google-cloud-platform,service-accounts,Python,Json,Google Cloud Platform,Service Accounts,我正在尝试为GCP服务帐户实施关键轮换。我已经成功地创建了一个新密钥,然后解码了privateKeyData,它是base64编码的,具有实际的SA JSON文件。现在,当我读回文件进行身份验证时,它会给我以下错误: “unicode对象没有iterKeys()” 我认为问题在于json.dumps data = base64.b64decode(key['privateKeyData']).decode('utf-8') print data # this prints expected o
privateKeyData
,它是base64编码的,具有实际的SA JSON文件。现在,当我读回文件进行身份验证时,它会给我以下错误:
“unicode对象没有iterKeys()”
我认为问题在于json.dumps
data = base64.b64decode(key['privateKeyData']).decode('utf-8')
print data # this prints expected output
with open('file.json', mode='w') as out:
str = json.dumps(data)
print out # this adds \n,\\ to the output
out.write(str)
错误:
AttributeError: 'unicode' object has no attribute 'iterkeys'
在json.dumps
之后如何转换文件的虚拟片段:
"{\n \"type\": \"service_account\",\n \"project_id\": \"testproj\",\n \"private_key_id\": \6866996939\"}"\n
json.dumps()
函数通常用于将dict
转换为表示json的字符串:
>>> json.dumps({"foo": "bar"})
'{"foo": "bar"}'
但是你给它一个字符串,这导致它转义引号:
>>> json.dumps('{"foo": "bar"}')
'"{\\"foo\\": \\"bar\\"}"'
您只需将数据
写入文件即可:
with open('file.json', mode='w') as out:
out.write(data)
看起来您可能有第二个问题导致了异常,您应该在答案中包含完整的回溯,而不仅仅是最后一行