question

Upvotes
Accepted
1 2 4 7

RDP News Wealth API

Hi running the below code (with my app key) and it does not seem to working is there anything obvious I am doing wrong?


import requests

import json

import pandas as pd


# Set up authentication headers

headers = {

'Authorization': 'APPKEY',

'Content-Type': 'application/json'

}


# Define search criteria

search_term = 'Rolls-Royce'

start_date = '2022-04-24T00:00:00Z'

end_date = '2022-05-01T23:59:59Z'


# Define endpoint and query parameters

endpoint = 'https://api.refinitiv.com/data/news/v1/articles'

params = {

'query': search_term,

'startTime': start_date,

'endTime': end_date,

'language': 'en',

'count': 100,

'includeSentiment': True

}


# Make request and get response

response = requests.get(endpoint, headers=headers, params=params)


# Convert response to JSON

data = json.loads(response.text)


# Extract sentiment information and aggregate by day

df = pd.DataFrame(data['data'])

df['publishedDate'] = pd.to_datetime(df['publishedDate'], format='%Y-%m-%dT%H:%M:%SZ')

df['sentiment'] = df['sentiment'].apply(lambda x: x['score'])

df = df.groupby(pd.Grouper(key='publishedDate', freq='D'))['sentiment'].mean().reset_index()


# Print result

print(df)


#technologyrefinitiv-data-platformnews
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.

1 Answer

· Write an Answer
Upvotes
Accepted
25.7k 58 17 14

Hello @danny.mullins1

Thank you for reaching out to us. Please see my answer below:

### RDP Authentication ###

Firstly, the application cannot access the RDP service with just the App Key credential (like the Eikon Data API does).

The RDP entitlement check is based on OAuth 2.0 specification. The first step of an application workflow is to get a token from RDP Auth Service, which will allow access to the protected resource, i.e. data REST API.

The API requires the username, password, and Client ID (aka App Key) access credential information:

Once the authentication success, the function gets the RDP Auth service response message and keeps the following RDP token information in the variables.

  • access_token
  • refresh_token
  • expires_in

Next, after the application received the Access Token (and authorization token) from RDP Auth Service, all subsequent REST API calls will use this token to get the data via the Authorization HTTP request message header as shown below to get the data.

  • Header:
    • Authorization = Bearer <RDP Access Token>

Please notice the space between the Bearer and RDP Access Token values.

So, the code should be:

rdp_username = "RDP_USER"
rdp_password = "RDP_PASSWORD"
rdp_appkey = "RDP_APPKEY"
scope = 'trapi'
access_token = None
refresh_token = None
expires_in = 0

# Authentication
auth_url = 'https://api.refinitiv.com/auth/oauth2/v1/token'
payload = f'username={rdp_username}&password={rdp_password}&grant_type=password&scope={scope}&takeExclusiveSignOnControl=true&client_id={rdp_appkey}'

# Send HTTP Request
try:
    response = requests.post(auth_url, 
        headers = {'Content-Type':'application/x-www-form-urlencoded'}, 
        data = payload, 
        auth = (rdp_appkey, '')
        )
except requests.exceptions.RequestException as exp:
    print(f'Caught exception: {exp}')


if response.status_code == 200:  # HTTP Status 'OK'
    print('Authentication success')
    access_token = response.json()['access_token']
    refresh_token = response.json()['refresh_token']
    expires_in = int(response.json()['expires_in'])
if response.status_code != 200:
    print(f'RDP authentication failure: {response.status_code} {response.reason}')
    print(f'Text: {response.text}')
    sys.exit(1)


# Set up authentication headers
headers = {
    'Authorization': f'Bearer {access_token}',
    'Content-Type': 'application/json'
}

Please find more detail on the RDP Tutorial Authorization - All about tokens page.

### RDP APIs /data/news/v1/articles Service ###

I checked the RDP API Document https://apidocs.refinitiv.com/Apps/ApiDocs page, but I cannot find /data/news/v1/articles service. The code always returns 404 page not found

rdp-news-services.png

Can you please confirm which service you want to use?

 
              

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.

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.