Reputation: 21
I am trying to implement Key Rotation for GCP Service Accounts. I have managed to create a new key and then decode the privateKeyData
which is base64 encoded, which has the actual SA JSON file. Now when I am reading the file back to authenticate, it is giving me this error:
'unicode object has no iterKeys()'
Issue is with json.dumps
I think.
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)
Error:
AttributeError: 'unicode' object has no attribute 'iterkeys'
Dummy Snippet of how the file is being converted after json.dumps
:
"{\n \"type\": \"service_account\",\n \"project_id\": \"testproj\",\n \"private_key_id\": \6866996939\"}"\n
Upvotes: 2
Views: 727
Reputation: 21520
The json.dumps()
function is usually used to turn a dict
into a string representing JSON:
>>> json.dumps({"foo": "bar"})
'{"foo": "bar"}'
But you're giving it a string instead, which is causing it to escape the quotes:
>>> json.dumps('{"foo": "bar"}')
'"{\\"foo\\": \\"bar\\"}"'
You should just write data
to the file instead:
with open('file.json', mode='w') as out:
out.write(data)
It seems you might have a second problem that is causing the exception, you should include the full traceback in your answer, instead of just the last line.
Upvotes: 1