I'm trying to make an API call to COF data inquiry, but I'm getting the following error: {'responseStatus': {'status': 401, 'code': '9210', 'severity': 'ERROR', 'message': 'Token validation failed', 'info': ''}}
This is the code
import datetime
from distutils import debug
import time
import json
import ast
import logging
import requests
from jwcrypto import jwk, jwe
myKey_ID = 'bf4e64ce-0e41-4a73-9a3d-f8ed8d25133a'
server_cert = 'server_cert_bf4e64ce-0e41-4a73-9a3d-f8ed8d25133a.pem'
private_key = 'key_bf4e64ce-0e41-4a73-9a3d-f8ed8d25133a.pem'
def encrypt(payload, server_cert, myKey_ID😞
payload = json.dumps(payload)
protected_header = {
"alg": "RSA-OAEP-256",
"typ":"JOSE",
"enc": "A128GCM",
"kid": myKey_ID,
"channelSecurityContext":"RSA_PKI",
"iat": int(round(time.time() * 1000))
}
jwetoken = jwe.JWE(payload.encode('utf-8'),
recipient=loadPem(server_cert),
protected=protected_header)
encryptedPayload = jwetoken.serialize(compact=True)
return {"encData": encryptedPayload}
def decrypt(encPayload, private_key😞
print(encPayload);
if type(encPayload) is str:
payload = json.loads(encPayload)
if encPayload.get('encData', True😞
jwetoken = jwe.JWE()
jwetoken.deserialize(encPayload["encData"], key=loadPem(private_key))
return jwetoken.payload
return encPayload
def loadPem(filePath😞
with open(filePath, "rb") as pemfile:
return jwk.JWK.from_pem(pemfile.read())
print("START Sample Code for Two-Way (Mutual) SSL")
print(datetime.datetime.now())
date = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
headers = { "content-type": "application/json",
'accept': 'application/json',
'keyId': myKey_ID
}
body = {}
payload = json.loads('''{ "requestHeader":{
"requestMessageId":"6da6b8b024532a2e0eacb1af58581",
"messageDateTime":"'''+ date +'''"
},
"requestData":{
"pANs":[
4072208010000000
],
"group":"STANDARD"
}
}''')
user_id = 'RPNXAMSRCKPXKLZ1C42F21JX4aWTmPRVJT7l5RSJh9ts6r-6c'
password = 'r2AOqpk7nf0yZ6hKF3oYIIn7Vq'
cert = 'cert.pem'
key = 'key_974255ae-5455-486e-ac2c-a13630debe20.pem'
timeout = 10
encryptedPayload = encrypt(payload, server_cert, myKey_ID)
try:
response = requests.post(url,
cert=(cert, key),
headers = headers,
auth=(user_id, password),
json = encryptedPayload,
timeout=timeout
)
except Exception as e:
print(e)
decryptedPayload = decrypt(response.json(), private_key)
data = ast.literal_eval(decryptedPayload.decode('utf8'))
print("Decrypted payload",data)
var1 = str(response.status_code)
var2 = '200'
msg = " Two-Way (Mutual) SSL test failed"
assert var1 == var2, msg
print("END Sample Code for Two-Way (Mutual) SSL\n\n")
When I debug I get this
Sorry for the bad english and I would appreciate any type of help.I'm doing this for the visa virtual internship program and trying to learn as much as I can.