For a deeper look into our World Check One API, look into:

Overview |  Quickstart |  Documentation |  Downloads

question

Upvotes
Accepted
13 2 8 7

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

python#technology#productauthenticationworldcheck-one-api
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Upvotes
Accepted
546 4 0 2

Hi @Bruno.Leal.


PFA the link to the same - https://developers.refinitiv.com/content/dam/devportal/en_us/product-docs/wc1-api/auth-module/hmac_authorization_files.zip. You can choose python as the language in the link. Thank you.


Regards,

Ssneha Balasubramanian.

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Thank you! It worked.
Upvotes
546 4 0 2

Hi @Bruno.Leal.


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


Regards,

Ssneha Balasubramanian.

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Upvotes
546 4 0 2

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.

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

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

1671102760099.png (32.6 KiB)

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.