question

Upvotes
Accepted
4 1 0 3

Cannot connect to DSWS via Python

Using the below code:

import DatastreamDSWS as DSWS

ds = DSWS.Datastream(username = 'XXXX', password = "XXXX")

I get the following error: Token Request : Unexpected error

I am still able to connect to Datastream using the PyDSWS package:

ds = PyDSWS.Datastream(username='XXXX', password='XXXX')

So I don't believe it's a permissioning issue.

Our firm recently had product.datastream.com whitelisted so that we can access the service, but I believe this requires a proxy setting for requests package in python. In the old (PyDSWS) package this required adding a proxy setting to the requests.get call

ie

proxy = {'http': 'http://proxy.xxx.com'}

requests.get(url, proxies = proxy)

Any idea how to add this setting in the new DatastreamDSWS package?

datastream-apidsws-api
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

Upvotes
Accepted
38.1k 71 35 53

It works fine on my machine with Python 3.7 64-bit and Requests 2.21. Please verify the versions of Python and Requests libraries.

You may need to run the following code to verify the problem.

import DatastreamDSWS as DSWS 
import requests
import json
import os

url = "https://product.datastream.com/DSWSClient/V1/DSService.svc/rest/"
token_url = url + "GetToken"

def json_Request( raw_text):
jsonText = json.dumps(raw_text)
byteTemp = bytes(jsonText,'utf-8')
byteTemp = jsonText.encode('utf-8')
jsonRequest = json.loads(byteTemp)
return jsonRequest

os.environ['HTTP_PROXY']="http://127.0.0.1:8080"
os.environ['HTTPS_PROXY']="http://127.0.0.1:8080"
tokenReq = {"Password":'<password>',"Properties":[{'Key':'__AppId','Value':"PythonLib 1.0"}],"UserName":'<username>'}
json_tokenReq = json_Request(tokenReq)

json_ResponseRaw = requests.post(token_url, json=json_tokenReq)
print(json_ResponseRaw.status_code, json_ResponseRaw.text)

json_Response = json_ResponseRaw.json()
print(json_Response["TokenValue"])

We need to verify which line throws the exception.

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

The above seems to work, I don't get an exception

Using the above code:

print(json_ResponseRaw.status_code, json_ResponseRaw.text)

Result: 200 {"Properties":null,"TokenExpiry":"\/Date(1562961104528)\/","TokenValue":"Gz2mKM6I5GIVXFF7FepWV6kUYD4GrPnu6Z42LWoXJZxZj8t27YV82gyIwYmvidr8s3y7LXlyJrvv+KG6dDkddYWbm6p8cd3SsAoDWMDjrBVcM8p9nZzxXKqCAGvMDTLb2RwJLR4Nn8dDNO\/Yfz2EkaUkmuO63cphTAI5GwPoJwwEcDSXunOQ7Ymngg+Ras6\/9hABsGVvN811fzlVj5xeK\/Y1YdO8\/uSC7IqLDWui89U=CA0A0609A1DDAB5190574B505AC680FC70CD1680"}

print(json_Response["TokenValue"])

Result: Gz2mKM6I5GIVXFF7FepWV6kUYD4GrPnu6Z42LWoXJZxZj8t27YV82gyIwYmvidr8s3y7LXlyJrvv+KG6dDkddYWbm6p8cd3SsAoDWMDjrBVcM8p9nZzxXKqCAGvMDTLb2RwJLR4Nn8dDNO/Yfz2EkaUkmuO63cphTAI5GwPoJwwEcDSXunOQ7Ymngg+Ras6/9hABsGVvN811fzlVj5xeK/Y1YdO8/uSC7IqLDWui89U=CA0A0609A1DDAB5190574B505AC680FC70CD1680

ds = DSWS.Datastream(username = 'XXXX', password = "XXXX")

Result: JSON decoder error while posting Token request

Any further ideas?

Nvm, was just missing a backslash after the URL, its working now, thanks!!

Upvotes
38.1k 71 35 53

You can set a proxy via environment variables.

set https_proxy=http://127.0.0.1:36036
set http_proxy=http://127.0.0.1:36036

Moreover, you can set the environment variables via the Python code.

import os
os.environ['HTTP_PROXY']="http://127.0.0.1:8889"
os.environ['HTTPS_PROXY']="http://127.0.0.1:8889"
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

Thanks, seems like that worked for setting the proxy, although now I'm encountering a different error:

import DatastreamDSWS as DSWS

import os

os.environ['HTTP_PROXY'] = "http://xxxx.xxxx:xx"

os.environ['HTTPS_PROXY'] = "http://xxxx.xxxx:xx"

ds = DSWS.Datastream(username = 'XXXX', password = "XXXX")

Running the above I get the error: JSON decoder error while posting Token request

Any ideas on this issue? I am able to pull data from my laptop, so I don't believe its an issue with my credentials

Click below to post an Idea Post Idea