WC1 API - Python GET requests returning code 401

Hi all,

I'm trying to use WC1 API with Python requests, but I'm facing issue to get the authorization. Below you can see the code I'm using:

import hmac, hashlib, base64, requests as r
from datetime import datetime

dt = datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S GMT") # Wed, 14 Dec 2022 23:54:18 GMT

app_key = "******"
app_secret = "****************"

gateway_host = 'api-worldcheck.refinitiv.com'
gateway_url = '/v2/'
scope = 'groups'

dataToSign = f"(request-target): get {gateway_url}{scope}\n host: {gateway_host}\n date: {dt}"
print(dataToSign,'\n')

digest = hmac.new(bytes(app_secret , 'UTF-8'), msg = bytes(dataToSign , 'UTF-8'), digestmod = hashlib.sha256).digest()
signature = base64.b64encode(digest).decode()

authorization = f'Signature keyId="{app_key}",algorithm="hmac-sha256",headers="(request-target) host date",signature="{signature}"'
print(authorization,'\n')

header = { "Date": dt, "Authorization": authorization,}


res = r.get(f'https://{gateway_host}{gateway_url}{scope}', headers=header)

print(res.url, res.status_code)

However I'm always getting the error code 401.

Would you have an example to how to get such authorization using python?

WC1 API - Python requests - Code 401.png

Best Answer

Answers

  • Hi @Bruno.Leal.


    I am looking into this and will get back to you with an answer. Thank you.


    Regards,

    Ssneha Balasubramanian.

  • Hi @Bruno.Leal.


    I may have found a possible solution. Could you replace the line where authorization is built in your code with the below line?


    authorization = 'Signature keyId=\"' + api_key + '\"' \

    + ',algorithm=\"hmac-sha256\",headers=\"' + headers + '\"' \

    + ',signature=\"' + hmac_base + '\"'


    Let me know if it works. Thank you.


    Regards,

    Ssneha Balasubramanian.

  • Please, could you share the entire python code? I didn't understand what value goes in headers variable, because after building my authorization I have to add it to a dict with keys "Authorization" and "Date", and later, use it in my GET requesition:


    1671102760099.png

  • Thank you! It worked.