Jump to content
We've recently updated our Privacy Statement, available here. ×

windows authentication in a script to connect to Rest API


Claudia Siano 2

Recommended Posts

Hi

I got a very simple script to get data from a rest api url.

I do have acess to that url , i can open in Chrome no problem,

However when i tried to connect from Spotfire ( 10) i get

SystemError: The remote server returned an error: (401) Unauthorized.

 

What's the syntax to make spotfire to sue my windows authentification

this is the code:

 

import clr

clr.AddReference('System.Web.Extensions')

from Spotfire.Dxp.Data.Import import *

from Spotfire.Dxp.Data import *

from System.Net import HttpWebRequest

uri = "http://testjsonpage=1&entitiesPerPage=20000

response = webRequest.GetResponse()

print response

Link to comment
Share on other sites

  • 1 month later...

Hi Claudia,

Looking at the code, the webRequest needs to be defined before it can be used to get response from the REST service - as below:

webRequest = HttpWebRequest.Create(uri)

 

response = webRequest.GetResponse()Here is an example:https://community.spotfire.com/wiki/ironpython-script-access-data-web-service-using-httpwebrequest-and-parse-returned-json

For windows authentication to REST - what type are we looking at, is it NTLM or Kerberos Does the service work with SOAP UI Depending on the type of authentication, you might need to add a custom header to the request as below -

req.Headers.Add('Subscription-Key','427aa77c36f7976b163a')Please see if this example snippet help:

def getData(url):

import clr

clr.AddReference('System.Web.Extensions')

from System.Net import WebClient, ServicePointManager, CredentialCache

from System.Web.Script.Serialization import JavaScriptSerializer

from System.IO import StreamReader

 

#bypass invalid ssl certificate

def AcceptAllCertifications(a,b,c,d):return True

ServicePointManager.ServerCertificateValidationCallback = AcceptAllCertifications

 

#Get the json data from web service

wc = WebClient()

wc.Headers.Add('Content-Type', 'application/x-www-form-urlencoded')

 

#windows auth

wc.Credentials = CredentialCache.DefaultCredentials

 

requestData= wc.OpenRead(url)

reader = StreamReader (requestData);

response = reader.ReadToEnd()

 

#Parse the results

js = JavaScriptSerializer()

responseDict = js.Deserialize(response,object)

return responseDict

Or this one by specifying the network credentials:

####

from System.Data import DataSet, DataTable, XmlReadMode

from System.IO import StringReader, StreamReader, StreamWriter, MemoryStream, SeekOrigin

from System.Net import HttpWebRequest, NetworkCredential, CredentialCache, DecompressionMethods

 

url = "http://myserver/page.html"

webRequest = HttpWebRequest.Create(url)

webRequest.Credentials = NetworkCredential(user, password)

response = webRequest.GetResponse()

textArea.HtmlContent = response

#####- Neeraj

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...