Skip to content

Commit a2569c4

Browse files
author
Wasin Waeosri
committed
works with README.md and authen script
1 parent 7a0bbc5 commit a2569c4

File tree

2 files changed

+52
-38
lines changed

2 files changed

+52
-38
lines changed

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ This is an example project that shows how to implement TRKD REST Client with pyt
1313

1414
# prerequisite
1515
The following softwares are required to use this script
16-
- Python 2.7.10
16+
- Python 2.7.10 or above
1717
- The [requests](http://docs.python-requests.org/en/master/) library
1818

19-
The script does not support Python 3!
19+
The scripts support Python 2 only
2020

2121
# how to run the script
2222
Run the script via the command line (or shell)
@@ -36,6 +36,17 @@ The best way is to get the pip package management tool
3636
export https_proxy="http://<proxy.server>:<port>"
3737
$>pip install requests
3838
```
39+
# Optional - How to run with Python 3
40+
You can modify the scripts to run with Python 3 (with requests library installed) by just change the code from "**raw_input()**" to "**input()**" as the following example
41+
- Python 2
42+
```
43+
username = raw_input('Please input username: ')
44+
```
45+
- Python 3
46+
```
47+
username = input('Please input username: ')
48+
```
49+
3950
#Releae Note
4051
- Version 1: 6 Sep 2016
4152
- trkd_authen.py

trkd_authen.py

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,49 @@
77
Please see the Thomson Reuters Knowledge Direct product page at http://customers.thomsonreuters.com
88
for additional information regarding the TRKD API.'''
99

10-
11-
import os
1210
import sys
13-
import requests
1411
import json
1512
import getpass
13+
import requests
14+
1615

17-
##Get username, password and applicationid
18-
username = raw_input('Please input username: ')
19-
##use getpass.getpass to hide user inputted password
20-
password = getpass.getpass(prompt='Please input password: ')
21-
appid = raw_input('Please input appid: ')
22-
23-
print '############### Sending Authentication request message to TRKD ###############'
24-
25-
##create authentication request URL, message and header
26-
authenMsg = {'CreateServiceToken_Request_1': { 'ApplicationID':appid, 'Username':username,'Password':password }}
27-
authenURL = 'https://api.trkd.thomsonreuters.com/api/TokenManagement/TokenManagement.svc/REST/Anonymous/TokenManagement_1/CreateServiceToken_1'
28-
headers = {'content-type': 'application/json;charset=utf-8'}
29-
30-
try:
31-
##send request
32-
result = requests.post(authenURL, data = json.dumps(authenMsg), headers=headers)
33-
if result.status_code == 200:
34-
print 'Request success'
35-
print 'response status %s'%(result.status_code)
36-
##get Token
37-
token = result.json()['CreateServiceToken_Response_1']['Token']
38-
print 'Token: %s'%(token)
39-
##get expiraion
40-
expire = result.json()['CreateServiceToken_Response_1']['Expiration']
41-
print 'Exipre: %s'%(expire)
42-
elif result.status_code == 500:
43-
print 'Request fail'
44-
print 'response status %s'%(result.status_code)
45-
print 'Error: %s'%(result.json())
46-
except requests.exceptions.RequestException as e:
47-
print 'Exception!!!'
48-
print e
49-
sys.exit(1)
16+
if __name__ == '__main__':
17+
## Get username, password and applicationid
18+
username = raw_input('Please input username: ')
19+
## use getpass.getpass to hide user inputted password
20+
password = getpass.getpass(prompt='Please input password: ')
21+
appid = raw_input('Please input appid: ')
22+
print('############### Sending Authentication request message to TRKD ###############')
23+
24+
##create authentication request URL, message and header
25+
authenMsg = {'CreateServiceToken_Request_1': { 'ApplicationID':appid, 'Username':username,'Password':password }}
26+
authenURL = 'https://api.trkd.thomsonreuters.com/api/TokenManagement/TokenManagement.svc/REST/Anonymous/TokenManagement_1/CreateServiceToken_1'
27+
headers = {'content-type': 'application/json;charset=utf-8'}
28+
29+
try:
30+
## send request
31+
result = requests.post(authenURL, data = json.dumps(authenMsg), headers=headers)
32+
## request success
33+
if result.status_code == 200:
34+
print('Request success')
35+
print('response status %s'%(result.status_code))
36+
##get Token
37+
token = result.json()['CreateServiceToken_Response_1']['Token']
38+
print('Token: %s'%(token))
39+
##get expiraion
40+
expire = result.json()['CreateServiceToken_Response_1']['Expiration']
41+
print('Expire: %s'%(expire))
42+
## handle error
43+
else:
44+
print('Request fail')
45+
print('response status %s'%(result.status_code))
46+
if result.status_code == 500: ## if username or password or appid is wrong
47+
print('Error: %s'%(result.json()))
48+
result.raise_for_status()
49+
except requests.exceptions.RequestException as e:
50+
print('Exception!!!')
51+
print(e)
52+
sys.exit(1)
5053

5154

5255

0 commit comments

Comments
 (0)