401 response on cases/screeningRequest with python

import requests
import hmac
import hashlib
import base64
import time
import json
# To get the GMT time
stringdataformat = ("%a, %d %b %Y %H:%M:%S GMT")
datenow = time.gmtime()
date = time.strftime(dataformat, datenow)
print(date) api_key = "xxxxxxxxxxxxxxxxxxxx"  
# Enter your API key
api_secret = "xxxxxxxxxxxxxxxxxxxxxxx"  
# Enter your API secret
group_id = "xxxxxxxxxxxxxxxxxxxxxxx"  
# Enter your group Id
api_token = api_secret.encode()
path = "https://rms-world-check-one-api-pilot.thomsonreuters.com/v2/cases/screeningRequest"
gatewayurl = "/v2/"
gatewayhost = "rms-world-check-one-api-pilot.thomsonreuters.com"
content_type = "application/json" 
name1 = "putin"
entity_type = "INDIVIDUAL"
bcontent = "{\n\"secondaryFields\": [],\n  \"entityType\": \"" + entity_type + "\",\n  \"customFields\": [],\n  \"groupId\":\"" + group_id + "\",\n  \"providerTypes\": [\n    \"WATCHLIST\"\n  ],\n  \"name\": \"" + name1 + "\"}"

content = bcontent.encode('utf-8')length = len(content)
str_length = str(length)
 
datatosign = "(request-target): post " + gatewayurl + "cases/screeningRequest\n" + \             "host: " + gatewayhost + "\n" + \             "date: " + date + "\n" + \             "content-type: " + content_type + "\n" + \             "content-length: " + str_length + "\n" + \             content.decode()  

byte_datatosign = datatosign.encode() 
 
def hbase(byte_datatosign, api_token):   
encrypt = hmac.new(api_token, byte_datatosign, digestmod=hashlib.sha256)   
digest_maker = encrypt.digest()   
base = base64.b64encode(digest_maker)   
return base.decode()  

hmacbase = hbase(byte_datatosign, api_token)
print(hmacbase) 

authorisation = "Signature keyId=\"" + api_key + "\"" + ",algorithm=\"hmac-sha256\"" + ",headers=\"(request-target) host date content-type content-length\"" + ",signature=\"" + hmacbase + "\""

print(authorisation) 

headers = {     'Authorization': authorisation,    'Date': date,    'Content-Type': content_type,    'Content-Length': str_length,    'Accept': "*/*",    'Cache-Control': "no-cache",    'Host': "rms-world-check-one-api-pilot.thomsonreuters.com",    'Accept-Encoding': "gzip, deflate",    'Connection': "keep-alive",    'cache-control': "no-cache"} 

requests.post(path, data=content, headers=headers)


I'm trying to make a screening request with the following code with python and it keeps returning 401 errors. What am I doing wrong?

Best Answer