|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# TRKD HTTP JSON with Python Example: Authentication\n", |
| 8 | + "\n", |
| 9 | + "## Overview\n", |
| 10 | + "The [Thomson Reuters Knowledge Direct (TRKD) API](https://developers.thomsonreuters.com/thomson-reuters-knowledge-direct-trkd) integrates into your website, trading platform, company intranet/extranet, advisory portal and mobile applications to provide up-to-date financial market data, news and analytics and powerful investment tools.\n", |
| 11 | + "\n", |
| 12 | + "TRKD offers a wide range of Refinitiv' information and services delivered in a request-response scenario via web services using today's industry standard protocols (SOAP/XML and REST/JSON). Connectivity can be via HTTP and HTTPS, over the Internet or Delivery Direct. All data are snapshot (non-streaming) data.\n", |
| 13 | + "\n", |
| 14 | + "This is an example project that shows how to implement TRKD HTTP JSON client with Python programming lanugage in Jupyter Notebook.\n", |
| 15 | + "\n", |
| 16 | + "### TRKD JSON application implementation process\n", |
| 17 | + "The JSON application requires the following steps to consume data from TRKD API services\n", |
| 18 | + "1. Authentication with TRKD Authentication service to get an authen token\n", |
| 19 | + "2. Send a request message with the required input information and authen token to the interested TRKD service" |
| 20 | + ] |
| 21 | + }, |
| 22 | + { |
| 23 | + "cell_type": "code", |
| 24 | + "execution_count": null, |
| 25 | + "metadata": {}, |
| 26 | + "outputs": [], |
| 27 | + "source": [ |
| 28 | + "# #uncomment if you do not have requests and websocket-client (version 0.49 and above) installed\n", |
| 29 | + "\n", |
| 30 | + "# # Install requests package in a current Jupyter kernal\n", |
| 31 | + "# import sys\n", |
| 32 | + "# !{sys.executable} -m pip install requests" |
| 33 | + ] |
| 34 | + }, |
| 35 | + { |
| 36 | + "cell_type": "code", |
| 37 | + "execution_count": null, |
| 38 | + "metadata": {}, |
| 39 | + "outputs": [], |
| 40 | + "source": [ |
| 41 | + "# import required libraries\n", |
| 42 | + "\n", |
| 43 | + "import requests\n", |
| 44 | + "import json" |
| 45 | + ] |
| 46 | + }, |
| 47 | + { |
| 48 | + "cell_type": "code", |
| 49 | + "execution_count": null, |
| 50 | + "metadata": {}, |
| 51 | + "outputs": [], |
| 52 | + "source": [ |
| 53 | + "# HTTP operations function\n", |
| 54 | + "def doSendRequest(url, requestMsg, headers):\n", |
| 55 | + " result = None\n", |
| 56 | + " try:\n", |
| 57 | + " result = requests.post(url, data=json.dumps(requestMsg), headers=headers)\n", |
| 58 | + " except requests.exceptions.RequestException as e:\n", |
| 59 | + " print('Exception!!!')\n", |
| 60 | + " print(e)\n", |
| 61 | + " return result" |
| 62 | + ] |
| 63 | + }, |
| 64 | + { |
| 65 | + "cell_type": "code", |
| 66 | + "execution_count": null, |
| 67 | + "metadata": {}, |
| 68 | + "outputs": [], |
| 69 | + "source": [ |
| 70 | + "# main process" |
| 71 | + ] |
| 72 | + }, |
| 73 | + { |
| 74 | + "cell_type": "code", |
| 75 | + "execution_count": null, |
| 76 | + "metadata": {}, |
| 77 | + "outputs": [], |
| 78 | + "source": [ |
| 79 | + "# Input your TRKD credentials here\n", |
| 80 | + "\n", |
| 81 | + "username = \"\"\n", |
| 82 | + "password = \"\"\n", |
| 83 | + "appid = \"\"" |
| 84 | + ] |
| 85 | + }, |
| 86 | + { |
| 87 | + "cell_type": "code", |
| 88 | + "execution_count": null, |
| 89 | + "metadata": {}, |
| 90 | + "outputs": [], |
| 91 | + "source": [ |
| 92 | + "# Authentication parameters\n", |
| 93 | + "\n", |
| 94 | + "token = None\n", |
| 95 | + "expire = None" |
| 96 | + ] |
| 97 | + }, |
| 98 | + { |
| 99 | + "cell_type": "markdown", |
| 100 | + "metadata": {}, |
| 101 | + "source": [ |
| 102 | + "#### TRKD Service Token Detail\n", |
| 103 | + "##### TRKD Service Token URL and Header\n", |
| 104 | + "\n", |
| 105 | + "The URL enponint for the TRKD Service Token is following:\n", |
| 106 | + "[https://api.trkd.thomsonreuters.com/api/TokenManagement/TokenManagement.svc/REST/Anonymous/TokenManagement_1/CreateServiceToken_1](https://api.trkd.thomsonreuters.com/api/TokenManagement/TokenManagement.svc/REST/Anonymous/TokenManagement_1/CreateServiceToken_1)\n", |
| 107 | + "\n", |
| 108 | + "Header: \n", |
| 109 | + "- Content-type = application/json;charset=utf-8\n", |
| 110 | + "Method:\n", |
| 111 | + "- Post\n", |
| 112 | + "\n", |
| 113 | + "##### TRKD Service TOKEN Request Message\n", |
| 114 | + "\n", |
| 115 | + "The CreateServiceToken_1 operation requires the following information to perform authentication\n", |
| 116 | + "- ApplicationID\n", |
| 117 | + "- Username\n", |
| 118 | + "- Password\n", |
| 119 | + "\n", |
| 120 | + "The request message structure is following\n", |
| 121 | + "```\n", |
| 122 | + "{\n", |
| 123 | + " “CreateServiceToken_Request_1”:{\n", |
| 124 | + " “ApplicationID”: <application id>,\n", |
| 125 | + " “Username”: <username>,\n", |
| 126 | + " “Password”: <password>\n", |
| 127 | + " }\n", |
| 128 | + "}\n", |
| 129 | + "\n", |
| 130 | + "```\n", |
| 131 | + "The example of the response message is shown below\n", |
| 132 | + "```\n", |
| 133 | + "{\n", |
| 134 | + " \"CreateServiceToken_Response_1\": {\n", |
| 135 | + " \"Expiration\": \"2016-09-26T09:42:54.4335265Z\",\n", |
| 136 | + " \"Token\": \"674E12E4EF35F181602672D5529D98379D4B42216057C7FF…\"\n", |
| 137 | + " }\n", |
| 138 | + "}\n", |
| 139 | + "```\n", |
| 140 | + "- Token: an encrypted, expiring string that securely identifies the service user (aka service Token)\n", |
| 141 | + "- Expiration: Token expires after a configurable time period. The default expiration is 90 minutes." |
| 142 | + ] |
| 143 | + }, |
| 144 | + { |
| 145 | + "cell_type": "code", |
| 146 | + "execution_count": null, |
| 147 | + "metadata": {}, |
| 148 | + "outputs": [], |
| 149 | + "source": [ |
| 150 | + "# TRKD Authentication Post Request message\n", |
| 151 | + "\n", |
| 152 | + "authenMsg = {'CreateServiceToken_Request_1': {'ApplicationID': appid, 'Username': username, 'Password': password}}" |
| 153 | + ] |
| 154 | + }, |
| 155 | + { |
| 156 | + "cell_type": "code", |
| 157 | + "execution_count": null, |
| 158 | + "metadata": {}, |
| 159 | + "outputs": [], |
| 160 | + "source": [ |
| 161 | + "# TRKD Authentication Service URL\n", |
| 162 | + "\n", |
| 163 | + "authenURL = (\n", |
| 164 | + " 'https://api.trkd.thomsonreuters.com/api/'\n", |
| 165 | + " 'TokenManagement/TokenManagement.svc/REST/'\n", |
| 166 | + " 'Anonymous/TokenManagement_1/CreateServiceToken_1'\n", |
| 167 | + ")" |
| 168 | + ] |
| 169 | + }, |
| 170 | + { |
| 171 | + "cell_type": "code", |
| 172 | + "execution_count": null, |
| 173 | + "metadata": {}, |
| 174 | + "outputs": [], |
| 175 | + "source": [ |
| 176 | + "# TRKD Authentication Request headers\n", |
| 177 | + "\n", |
| 178 | + "authen_headers = {'content-type': 'application/json;charset=utf-8'}" |
| 179 | + ] |
| 180 | + }, |
| 181 | + { |
| 182 | + "cell_type": "code", |
| 183 | + "execution_count": null, |
| 184 | + "metadata": {}, |
| 185 | + "outputs": [], |
| 186 | + "source": [ |
| 187 | + "print('############### Sending Authentication request message to TRKD ###############')" |
| 188 | + ] |
| 189 | + }, |
| 190 | + { |
| 191 | + "cell_type": "code", |
| 192 | + "execution_count": null, |
| 193 | + "metadata": {}, |
| 194 | + "outputs": [], |
| 195 | + "source": [ |
| 196 | + "# Send Authentication request\n", |
| 197 | + "\n", |
| 198 | + "authenResult = doSendRequest(authenURL, authenMsg, authen_headers)" |
| 199 | + ] |
| 200 | + }, |
| 201 | + { |
| 202 | + "cell_type": "code", |
| 203 | + "execution_count": null, |
| 204 | + "metadata": {}, |
| 205 | + "outputs": [], |
| 206 | + "source": [ |
| 207 | + "if authenResult and authenResult.status_code == 200:\n", |
| 208 | + " print('Authenticaion success')\n", |
| 209 | + " print('JSON resonse: %s' % (json.dumps(authenResult.json(),sort_keys=True, indent=2, separators=(',', ':'))))\n", |
| 210 | + " token = authenResult.json()['CreateServiceToken_Response_1']['Token']\n", |
| 211 | + " expire = authenResult.json()['CreateServiceToken_Response_1']['Expiration']\n", |
| 212 | + "elif authenResult.status_code is not 200:\n", |
| 213 | + " print('Authenticaion fail with status code %s' % authenResult.status_code)\n", |
| 214 | + " if authenResult.status_code == 500:\n", |
| 215 | + " print('Error: %s' % (json.dumps(authenResult.json(), sort_keys=True, indent=2, separators=(',', ':'))))" |
| 216 | + ] |
| 217 | + }, |
| 218 | + { |
| 219 | + "cell_type": "code", |
| 220 | + "execution_count": null, |
| 221 | + "metadata": {}, |
| 222 | + "outputs": [], |
| 223 | + "source": [ |
| 224 | + "# Print Authentication information\n", |
| 225 | + "\n", |
| 226 | + "print('Token = : %s' % token)\n", |
| 227 | + "print('Expiration = %s' % expire)" |
| 228 | + ] |
| 229 | + }, |
| 230 | + { |
| 231 | + "cell_type": "markdown", |
| 232 | + "metadata": {}, |
| 233 | + "source": [ |
| 234 | + "## Conclusion\n", |
| 235 | + "All TRKD HTTP JSON applications require authenticaiton to access TRKD data. The application needs to request for Service Token from TRKD server, then keeps a response Service Token for later use in other request message header." |
| 236 | + ] |
| 237 | + }, |
| 238 | + { |
| 239 | + "cell_type": "markdown", |
| 240 | + "metadata": {}, |
| 241 | + "source": [ |
| 242 | + "## <a href=\"references\"></a>References\n", |
| 243 | + "For further details, please check out the following resources:\n", |
| 244 | + "* [Thomson Reuters Knowledge Direct API page](https://developers.refinitiv.com/thomson-reuters-knowledge-direct-trkd) on the [Refinitiv Developer Community](https://developers.thomsonreuters.com/) web site.\n", |
| 245 | + "* [Thomson Reuters Knowledge Direct API Catalog](https://www.trkd.thomsonreuters.com/SupportSite/RequestBuilder/requestbuilder.aspx) web site.\n", |
| 246 | + "* TRKD Article: [How to implement TRKD JSON application with Python chapter 1: the basic](https://developers.refinitiv.com/article/how-implement-trkd-json-application-python-chapter-1-basic)\n", |
| 247 | + "\n", |
| 248 | + "For any question related to this tutorial or TRKD API, please use the Developer Community [Q&A Forum](https://community.developers.refinitiv.com/spaces/51/view.html)." |
| 249 | + ] |
| 250 | + }, |
| 251 | + { |
| 252 | + "cell_type": "code", |
| 253 | + "execution_count": null, |
| 254 | + "metadata": {}, |
| 255 | + "outputs": [], |
| 256 | + "source": [] |
| 257 | + } |
| 258 | + ], |
| 259 | + "metadata": { |
| 260 | + "kernelspec": { |
| 261 | + "display_name": "Python 3", |
| 262 | + "language": "python", |
| 263 | + "name": "python3" |
| 264 | + }, |
| 265 | + "language_info": { |
| 266 | + "codemirror_mode": { |
| 267 | + "name": "ipython", |
| 268 | + "version": 3 |
| 269 | + }, |
| 270 | + "file_extension": ".py", |
| 271 | + "mimetype": "text/x-python", |
| 272 | + "name": "python", |
| 273 | + "nbconvert_exporter": "python", |
| 274 | + "pygments_lexer": "ipython3", |
| 275 | + "version": "3.7.3" |
| 276 | + } |
| 277 | + }, |
| 278 | + "nbformat": 4, |
| 279 | + "nbformat_minor": 2 |
| 280 | +} |
0 commit comments