Authenticating in Python¶
You can authenticate with the Refinitiv Data Library for Python directly inline using a function in the package’s session
module.
It requires that provide your application key, as well as the username and password you use to access the Refinitiv web portal. In this example, they will be stored in environment variables to avoid exposing private information in the source code.
import os
import refinitiv.data as rd
session = rd.session.platform.Definition(
app_key=os.getenv('RDP_APP_KEY'),
grant=rd.session.platform.GrantPassword(
username=os.getenv('RDP_USERNAME'),
password=os.getenv('RDP_PASSWORD')
)
).get_session()
/home/runner/.local/share/virtualenvs/refinitiv-data-python-cookbook-I-HIyNf4/lib/python3.10/site-packages/refinitiv/data/session/platform.py:90:FutureWarning:
You open a platform session using the default value of the signon_control parameter (signon_control=True).
In future library version v2.0, this default will be changed to False.
If you want to keep the same behavior as today, you will need to set the signon_control parameter to True either in the library configuration file
({'sessions':{'platform':{'your_session_name':{'signon_control':true}}}}) or in your code where you create the Platform Session.
These alternative options are already supported in the current version of the library.
Once that’s successful, you can open a session:
session.open()
[Error 400] - {'error': 'access_denied', 'error_description': 'Account locked out due to provide incorrect password.'}
<OpenState.Closed: 'Closed'>
Then it as the default for the library.
rd.session.set_default(session)
Verify the session is open by executing a query for the current price of Thomson Reuters stock. You should back a table of data.
rd.get_history('TRI.N')
Session is not opened. Can't send any request
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[4], line 1
----> 1 rd.get_history('TRI.N')
File ~/.local/share/virtualenvs/refinitiv-data-python-cookbook-I-HIyNf4/lib/python3.10/site-packages/refinitiv/data/_access_layer/get_history_func.py:96, in get_history(universe, fields, interval, start, end, adjustments, count, use_field_names_in_headers, parameters)
90 warnings.warn(
91 "Parameter 'use_field_names_in_headers' is deprecated and will be removed in future library version v2.0.",
92 FutureWarning,
93 )
95 session = get_default()
---> 96 raise_if_closed(session)
98 logger = session.logger()
100 if interval is not None and interval not in INTERVALS:
File ~/.local/share/virtualenvs/refinitiv-data-python-cookbook-I-HIyNf4/lib/python3.10/site-packages/refinitiv/data/_core/session/tools.py:37, in raise_if_closed(session)
35 error_message = "Session is not opened. Can't send any request"
36 session.error(error_message)
---> 37 raise ValueError(error_message)
ValueError: Session is not opened. Can't send any request
The library expects you to close your session when you’re finished. You can do so by running the following code:
rd.close_session()