Reputation: 1505
I am trying to modify my python method so that its reading values from a separate json config file.
I have a separate valid json file that looks like this testtok.json
:
{
"email" : "[email protected]",
"audience" : "http://someaudience.com",
"jti" : "MYJTI1234",
"name" : "John Smith",
"humid" : "ABC1234"
}
I want to pass in those values to my function here:
def tokengen(self, privatekey):
with open('config/testtok.json', 'r') as config:
data = json.load(config)
try:
"""Simulate Token Manager creating a token"""
email = config["email"]
audience = config["audience"]
jti = config["jti"]
name = config["name"]
humid = config["humid"]
#email = "[email protected]"
#audience = "http://someaudience.com"
#jti = "MYJTI1234"
#name = "John Smith"
#humid = "ABC1234"
"""Time Component"""
timestamp = testdate()
timestamp.now()
issued = int(time.time())
expires_in=2400
expires = issued + expires_in
additional_headers = {
"alg": "RS256",
"typ": "JWT"
}
payload = {
"iss": email,
"sub": email,
"aud": audience,
"iat": issued,
"nbf": issued,
"exp": expires,
"jti": jti,
"name": name,
"humid": humid,
"email": email
}
self.testtoken = jwt.encode(payload, privatekey, algorithm="RS256", headers=additional_headers).decode("utf-8")
valid_response = {'validation' : 'VALID', 'ts' : timestamp.utc_datetime_str, 'test_token' : self.testtoken}
return(valid_response)
except Exception as ex:
invalid_response = {'validation' : 'INVALID', 'ts' : timestamp.utc_datetime_str, 'test_token' : self.testtoken}
return(invalid_response)
I'm seeing this error and unclear how to troubleshoot this.
Traceback (most recent call last):
File "testTokClass.py", line 25, in tokengen
config["email"]
TypeError: '_io.TextIOWrapper' object is not subscriptable
Is there a better way to do this? Ideally, I would like to have the config file as json. Thanks.
Upvotes: 0
Views: 1761
Reputation: 15018
Th problem is that the config
is a file handle; in the line data = json.load(config)
the JSON is read from the file pointed to by config
and into the data
variable.
So, just change:
email = config["email"]
to:
email = data["email"]
And similarly for the next four lines.
Upvotes: 1