![](/img/trans.png)
[英]gcp - how to run Python application as Service Account without a key file
[英]GCP Service Account Key Rotation
我正在尝试为GCP服务帐户实施密钥轮换。 我设法创建了一个新密钥,然后对base64编码的privateKeyData
进行了解码,它具有实际的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\\"}"'
您应该改为将data
写入文件:
with open('file.json', mode='w') as out:
out.write(data)
看来您可能还有第二个问题导致了异常,您应该在答案中包括完整的追溯,而不是仅最后一行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.