【问题标题】:GCP Service Account Key RotationGCP 服务帐号密钥轮换
【发布时间】:2018-11-30 20:06:25
【问题描述】:

我正在尝试为 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

【问题讨论】:

    标签: python json google-cloud-platform service-accounts


    【解决方案1】:

    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)
    

    看来您可能有导致异常的第二个问题,您应该在答案中包含完整的回溯,而不仅仅是最后一行。

    【讨论】:

      猜你喜欢
      • 2021-12-29
      • 1970-01-01
      • 2020-11-27
      • 2021-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多