Help in setting global variables for websocket API in R

Options

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

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 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&quot;
    > req <- curl_fetch_memory(auth_url, handle = h)
    > req
    $url
    [1] "https://api.refinitiv.com/auth/oauth2/v1/token&quot;

    $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) ------^
  • https://community.developers.refinitiv.com/discussion/comment/68398#Comment_68398

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

Welcome!

It looks like you're new here. Sign in or register to get started.

Welcome!

It looks like you're new here. Sign in or register to get started.