【发布时间】:2018-11-24 21:23:10
【问题描述】:
您好,我正在尝试将 google 服务帐户 JSON 密钥(包含在文件 foo.json 中名为 privateKeyData 的 base64 编码字段中 - 更多上下文 here )转换为实际的 JSON 文件(我只需要将该格式作为 ansible接受)
foo.json文件是使用这个google python apimethod获取的
我正在尝试做的事情(尽管我使用的是 python)也被描述为 this 线程,顺便说一句,它对我不起作用(在 OSx 和 Linux 上尝试过)。
#!/usr/bin/env python3
import json
import base64
with open('/tmp/foo.json', 'r') as f:
ymldict = json.load(f)
b64encodedCreds = ymldict['privateKeyData']
b64decodedBytes = base64.b64decode(b64encodedCreds,validate=True)
outputStr = b64decodedBytes
print(outputStr)
#issue
outputStr = b64decodedBytes.decode('UTF-8')
print(outputStr)
产量
./test.py
b'0\x82\t\xab\x02\x01\x030\x82\td\x06\t*\x86H\x86\xf7\r\x01\x07\x01\xa0\x82\tU\x04\x82\tQ0\x82\tM0\x82\x05q\x06\t*\x86H\x86\xf7\r\x01\x07\x01\xa0\x82\x05b\x04\x82\x05^0\x82\x05Z0\x82\x05V\x06\x0b*\x86H\x86\xf7\r\x01\x0c\n\x01\x02\xa0\x82\x#TRUNCATING HERE
Traceback (most recent call last):
File "./test.py", line 17, in <module>
outputStr = b64decodedBytes.decode('UTF-8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x82 in position 1: invalid start byte
我想我的想法已经用完了,现在花了一天多的时间:(
我做错了什么?
【问题讨论】:
标签: python base64 google-compute-engine