get data using API in python

tarun_dutta
tarun_dutta Newcomer

I want to retrieve the data using python


This is the following code. Please tell me where am I doing wrong.


##create authentication request URL, message and header

authenMsg = {'CreateServiceToken_Request_1': { 'ApplicationID':appid, 'Username':username,'Password':password }}

authenURL = 'https://api.trkd.thomsonreuters.com/api/TokenManagement/TokenManagement.svc/REST/Anonymous/TokenManagement_1/CreateServiceToken_1';

headers = {'content-type': 'application/json;charset=utf-8'}

result = requests.post(authenURL, data = json.dumps(authenMsg), headers=headers)

try:

if result.status_code == 200:

print('response status %s'%(result.status_code))

token = result.json()['CreateServiceToken_Response_1']['Token']

print('Token: %s'%(token))

expire = result.json()['CreateServiceToken_Response_1']['Expiration']


else:

print('response status %s'%(result.status_code))

if result.status_code == 500: ## if username or password or appid is wrong

print('Error: %s'%(result.json()))

result.raise_for_status()

except requests.exceptions.RequestException as e:

print('Exception!!!')

print(e)

sys.exit(1)


headers_ = {

"POST": "/api/Fundamentals/Fundamentals.svc HTTP/1.1"

,"Content-Type": "application/soap+xml"

,"Host": "api.trkd.thomsonreuters.com"

,"Content-Length": "1"

,'X-Trkd-Auth-Token': token

}

url = "http://api.trkd.thomsonreuters.com/api/Fundamentals/Fundamentals.svc"

r = requests.post(url, data = json.dumps(authenMsg), headers=headers_)

print(r)




Tagged:

Best Answer

  • wasin.w
    wasin.w admin
    Answer ✓

    Hello @tarun_dutta

    I have checked your error message and the code. The problems are following:

    1. The application sends the HTTP Post request message to RKD Fundamental Service SOAP endpoint. You cannot send the HTTP Post request message to the SOAP API endpoint
    2. The application sends the authentication request message as a Fundamental request message to RKD Fundamental service.

    I strongly suggest you check the RKD Fundamental Service information in RKD API Catalog page, then choose Fundamentals --> Get General Information. The service supports both SOAP and HTTP JSON interfaces. You can find the url endpoint, request message detail and example result from the page.

    image

    image

    Based on RKD API page above, the URL for Fundamentals service : General Company Information HTTP JSON interface is

    http://api.trkd.thomsonreuters.com/api/Fundamentals/Fundamentals.svc/REST/Fundamentals_1/GetGeneralInformation_1

    The example request message in JSON format is following:

    {
        "GetGeneralInformation_Request_1": {
            "companyId": "IBM.N",
            "companyIdType": "RIC",
            "ShowReferenceInformation": False
        }
    }

    I have tested the following example code, and it works fine in my environment.

    fundamental_header= {
        'content-type': 'application/json',
         'X-Trkd-Auth-ApplicationID': appid, 
         'X-Trkd-Auth-Token': token
    }
    fundamental_request_message = {
        "GetGeneralInformation_Request_1": {
            "companyId": "IBM.N",
            "companyIdType": "RIC",
            "ShowReferenceInformation": False
        }
    }
    url = "http://api.trkd.thomsonreuters.com/api/Fundamentals/Fundamentals.svc/REST/Fundamentals_1/GetGeneralInformation_1"
    result = requests.post(url, data = json.dumps(fundamental_request_message), headers=fundamental_header)
    print(result.json())

Answers