Help in setting global variables for websocket API in R
Hello, I am struggling in setting my global environment variables to get data via websocket in Elektron.
I tried Python code and it works fine, the connection gives access_token. However, using these variables in R does not give this token. Files can be downloaded here https://developers.refinitiv.com/en/api-catalog/elektron/refinitiv-websocket-api/download
For python I got following settings (market authentication file):
# Global Default Variables
app_id = '256'
auth_url = 'https://api.refinitiv.com:443/auth/oauth2/v1/token'
hostname = 'emea-1.pricing.streaming.edp.thomsonreuters.com'
password = 'MY PASSWORD WHEN SETTING MACHINE ID'
newPassword = ''
position = ''
sts_token = ''
refresh_token = ''
user = 'MACHINE ID'
clientid = 'APP ID VIA APPKEY GENERATOR'
port = '443'
client_secret = ''
scope = 'trapi'
ric = '/TRI.N'
service = 'ELEKTRON_DD'
Here I got an output:
Sending authentication request with password to https://api.refinitiv.com:443/auth/oauth2/v1/token ...
Refinitiv Data Platform Authentication succeeded. RECEIVED:
{
"access_token":"",
"expires_in":"300",
"refresh_token":"b92019b7-914c-4dc9-b5a9-b504ae3f99ec",
"scope":"trapi.auth.cloud-credentials trapi.cfs.claimcheck.read trapi.data.symbology.advanced.read trapi.data.symbology.read trapi.metadata.read trapi.streaming.pricing.read",
"token_type":"Bearer"
}
In R I set following (in auth_hostname i deleted https://, because in R code it will add this):
# Global Default Variables
hostname = "emea-1.pricing.streaming.edp.thomsonreuters.com"
port = "443"
user = "MACHINE ID"
app_id = "APP ID VIA APPKEY GENERATOR"
auth_token = ""
auth_hostname = "api.refinitiv.com:443/auth/oauth2/v1/token"
auth_port = "443"
password = "MY PASSWORD WHEN SETTING MACHINE ID"
However, in R when running code, it shows following:
> auth_url
[1] "https://api.refinitiv.com:443/auth/oauth2/v1/token"
> req
$url
[1] "https://api.refinitiv.com:443/auth/oauth2/v1/token"
$status_code
[1] 400
$type
[1] "application/json"
Can you help in setting up my variables for R? additionally I noticed that in R market_price file there is a need to set only following variables. Here no need to set the password. How can it be possible?
hostname = "emea-1.pricing.streaming.edp.thomsonreuters.com"
port = "443"
user = "MACHINE ID "
app_id = "APP ID VIA APPKEY GENERATOR"
UPDATE: 28.10.2020 14:22 CET
I figured out that in R should be following:
hostname = "emea-1.pricing.streaming.edp.thomsonreuters.com"
port = "443"
auth_hostname = "api.refinitiv.com:443/auth/oauth2/v1/token"
auth_port = "443"
However I have following error. I found that in Python code grand_types="password". I tried in R grand_types equals to the word password as a string and as a character from password(=password) and the same error persisted. How can I tackle it?
> cat(toJSON(auth_json, pretty=TRUE, auto_unbox=TRUE))
{
"error": {
"id": "46309bba-637b-47a3-9d7a-ba8d931cb063",
"code": "400",
"message": "Validation error",
"status": "Bad Request",
"errors": [
{
"key": "grant_type",
"reason": "Missing required parameter 'grant_type'"
}
]
}
Best Answer
-
Hi,
- Regarding requests to Refinitiv platform, there is one incorrect usage in your scripts (both python and R): http request should be sent to https://api.refinitiv.com/... (and not to https://api.refinitiv.com:443/... ).
443 port number is usually dedicated for websocket connection with a streaming server.
You can familiarize yourself on https://apidocs.refinitiv.com/ to check request format & response.
That means you have to request token to https://api.refinitiv.com/auth/oauth2/v1/token - For the Validation error with R, the message "Missing required parameter 'grant_type'"
That means your request didn't contain grant_type value.
Ex: check if your request for access token to https://api.refinitiv.com/auth/oauth2/v1/token contains post data with grant_type set to "password"
{ 'scope': 'trapi', 'grant_type': 'password', 'username': 'xxxx', 'password': 'xxxxxxx', 'takeExclusiveSignOnControl': 'true', 'client_id': 'your_app_key' }
0 - Regarding requests to Refinitiv platform, there is one incorrect usage in your scripts (both python and R): http request should be sent to https://api.refinitiv.com/... (and not to https://api.refinitiv.com:443/... ).
Answers
-
pierre.faurel Thank you for noticing point 1. Indeed there should not bee 443 in the link (found it here https://emea1.apps.cp.thomsonreuters.com/Apps/APIDocs/1.2019.3/#/details/L2F1dGgvb2F1dGgyL3Yx/L3Rva2Vu/POST/PLAYGROUND)
Regarding point 2. I change R code tocontent = paste("grant_type=", "password","&username=", user, "&password=", "password", sep="") #content = paste("scope=","trapi","&grant_type=", "password","&username=", user, "&password=", password,"takeExclusiveSignOnControl=true","&app_id=",app_id, sep="") h <- new_handle(copypostfields = content) handle_setheaders(h, "Content-Type" = "application/x-www-form-urlencoded") handle_setopt(h, ssl_verifypeer = FALSE, ssl_verifyhost = FALSE) auth_url = paste("https://", auth_hostname, sep="")# ":", auth_port, "/getToken", sep="") This part i did not include (even if include, the same output) also see output below under *** this command always caused if fully written req <- curl_fetch_memory(auth_url, handle = h) res_headers = parse_headers(req$headers) auth_json_string = rawToChar(req$content) auth_json = fromJSON(auth_json_string) cat("RECEIVED:\n") cat(toJSON(auth_json, pretty=TRUE, auto_unbox=TRUE))
So, I played around with CONTENT code having multiple versions, also set "grant_type=", "password" and "grant_type=", password (so, the same letters and numbers as my password) and the output is always the same
2.1) with "grant_type=", "password" in both versions of CONTENT the output is following :
> cat(toJSON(auth_json, pretty=TRUE, auto_unbox=TRUE)) { "error": "invalid_request" }
2.2) with "grant_type=", password in both versions of CONTENT the output is following :
> cat(toJSON(auth_json, pretty=TRUE, auto_unbox=TRUE)) { "error": "unsupported_grant_type" }
3) Additionally, the strange is that when using curl_fetch_memory it seems that handle=h is simply the same as auth_hostname, however I believe that handle should be with different input. Am I right?
> content = paste("grant_type=", "password","&username=", user, "&password=", password, sep="")
> h <- new_handle(copypostfields = content)
> h
<curl handle> (empty)
> handle_setheaders(h,
+ "Content-Type" = "application/x-www-form-urlencoded"
+ )
> handle_setopt(h, ssl_verifypeer = FALSE, ssl_verifyhost = FALSE)
> auth_url = paste("https://", auth_hostname, sep="")# ":", auth_port, "/getToken", sep="")
> auth_url
[1] "https://api.refinitiv.com/auth/oauth2/v1/token"
> req <- curl_fetch_memory(auth_url, handle = h)
> req
$url
[1] "https://api.refinitiv.com/auth/oauth2/v1/token"
$status_code
[1] 400
$type
[1] "application/json"
> h
<curl handle> (https://api.refinitiv.com/auth/oauth2/v1/token)***
> auth_json = fromJSON(auth_json_string) Error: parse error: trailing garbage 404 page not found (right here) ------^
0 -
thank you for your answer, I posted my answer below, because it exceeds 1500 characters
0
Categories
- All Categories
- 6 AHS
- 39 Alpha
- 161 App Studio
- 4 Block Chain
- 4 Bot Platform
- 16 Connected Risk APIs
- 47 Data Fusion
- 30 Data Model Discovery
- 608 Datastream
- 1.3K DSS
- 577 Eikon COM
- 4.9K Eikon Data APIs
- 7 Electronic Trading
- Generic FIX
- 7 Local Bank Node API
- Trading API
- 2.7K Elektron
- 1.3K EMA
- 236 ETA
- 519 WebSocket API
- 33 FX Venues
- 10 FX Market Data
- 1 FX Post Trade
- 1 FX Trading - Matching
- 12 FX Trading – RFQ Maker
- 5 Intelligent Tagging
- 2 Legal One
- 20 Messenger Bot
- 2 Messenger Side by Side
- 9 ONESOURCE
- 7 Indirect Tax
- 59 Open Calais
- 264 Open PermID
- 39 Entity Search
- 2 Org ID
- PAM
- PAM - Logging
- 8.4K Private Comments
- 6 Product Insight
- Project Tracking
- ProView
- ProView Internal
- 20 RDMS
- 1.4K Refinitiv Data Platform
- 367 Refinitiv Data Platform Libraries
- 3 Refinitiv Due Diligence
- LSEG Due Diligence Portal API
- 3 Refinitiv Due Dilligence Centre
- Rose's Space
- 1.1K Screening
- 18 Qual-ID API
- 13 Screening Deployed
- 23 Screening Online
- 10 World-Check Customer Risk Screener
- 990 World-Check One
- 44 World-Check One Zero Footprint
- 45 Side by Side Integration API
- Test Space
- 3 Thomson One Smart
- 1.2K TR Internal
- Global Hackathon 2015
- 2 Specialists Who Code
- 10 TR Knowledge Graph
- 150 Transactions
- 142 REDI API
- 1.7K TREP APIs
- 4 CAT
- 21 DACS Station
- 117 Open DACS
- 1.1K RFA
- 103 UPA
- 172 TREP Infrastructure
- 224 TRKD
- 886 TRTH
- 5 Velocity Analytics
- 5 Wealth Management Web Services
- 59 Workspace SDK
- 9 Element Framework
- 5 Grid
- 13 World-Check Data File
- Yield Book Analytics
- 46 中文论坛