使用Python将base64编码的google服务帐户密钥转换为JSON文件

使用Python将base64编码的google服务帐户密钥转换为JSON文件,python,base64,google-compute-engine,Python,Base64,Google Compute Engine,您好,我正在尝试将google服务帐户JSON密钥(包含在foo.JSON-more context文件中名为privateKeyData的base64编码字段中)转换为实际的JSON文件(我需要该格式,因为ansible只接受该格式) json文件是使用这个google python api获得的 我正在尝试做的(尽管我使用的是python)也被描述为线程,顺便说一句,它对我来说不起作用(在OSx和Linux上尝试过) 屈服 ./test.py b'0\x82\t\xab\x02\x01\x

您好,我正在尝试将google服务帐户JSON密钥(包含在foo.JSON-more context文件中名为privateKeyData的base64编码字段中)转换为实际的JSON文件(我需要该格式,因为ansible只接受该格式)

json文件是使用这个google python api获得的

我正在尝试做的(尽管我使用的是python)也被描述为线程,顺便说一句,它对我来说不起作用(在OSx和Linux上尝试过)

屈服

./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
/test.py
10\X0 0 0 0 0 0\X8 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0#此处截断
回溯(最近一次呼叫最后一次):
文件“/test.py”,第17行,在
outputStr=b64decodedBytes.decode('UTF-8')
UnicodeDecodeError:“utf-8”编解码器无法解码位置1中的字节0x82:无效的开始字节
我想我已经没有主意了,现在花了一天多的时间在这上面:(


我做错了什么?

您的base64解码逻辑在我看来很好。您面临的问题可能是由于字符编码不匹配。调用
create
后收到的响应正文(您的foo.json文件)可能未使用UTF-8进行编码。请查看响应标题的
内容类型
字段。该字段应如下所示:

Content-Type: text/javascript; charset=Shift_JIS
尝试使用内容类型中使用的编码对base64解码字符串进行解码

b64decodedBytes.decode('Shift_JIS')

感谢您的帮助Mug4n。我已经尝试在我所能看到的所有页面上使用tcpdump运行
create
方法。grepping(似乎无法在该连接上进行专门筛选)显示带有字符集utf-8的内容类型…:(它现在起作用了,很有趣,我不知道发生了什么变化。感谢againprivateKeyData以“MII…”开头)当我谈到这个问题的时候。现在我又回到了“ewo…”。天知道这里发生了什么
b64decodedBytes.decode('Shift_JIS')