For a deeper look into our Elektron API, look into:

Overview |  Quickstart |  Documentation |  Downloads |  Tutorials |  Articles

question

Upvotes
Accepted
28 6 6 5

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'"
      }
    ]
  }
pythonelektronrefinitiv-realtimeelektron-sdkwebsocketsr
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.

Upvotes
Accepted
4.3k 2 4 5

Hi,

  1. 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

  2. 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' } 
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.

thank you for your answer, I posted my answer below, because it exceeds 1500 characters

Upvotes
28 6 6 5

@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 to


content = 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) ------^
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.