Here we are going to get the auth token and json object from webservice using basic auth
from System import Uri from System.Net import HttpWebRequest from System.IO import StringReader, StreamReader, StreamWriter, MemoryStream, SeekOrigin #1 Data Source Settings usr = "username@someplace.com" pwd = "password" url = "https://my.webservice.com/api/authenticate" #2 Prepare and send url to server webRequest = HttpWebRequest.Create(Uri(url)) #2.1 Add authorization to http header authorization = usr + ":" + pwd from System.Text.Encoding import UTF8 binaryAuthorization = UTF8.GetBytes(authorization) from System import Convert authorization = Convert.ToBase64String(binaryAuthorization) authorization = "Basic " + authorization webRequest.Headers.Add("AUTHORIZATION", authorization) #3 Read response into memory webRequest.Method = 'POST' webRequest.ContentType = 'application/x-www-form-urlencoded' webRequest.ContentLength = 0 response = webRequest.GetResponse() streamReader = StreamReader(response.GetResponseStream()) jsonData = streamReader.ReadToEnd() #Parse JSON. Example {code:200,data:{userId:"jleviaguirre", firstName:"Jose", lastName:"leviaguirre", authToken:"ABCD01234..."}} import clr clr.AddReference('System.Web.Extensions') import System from System.Web.Script.Serialization import JavaScriptSerializer js = JavaScriptSerializer() jsonObj = js.Deserialize(jsonData,object) #parsed json data print "http status code:",jsonObj["code"] print "user id:",jsonObj["data"]["userId"] print "name:", jsonObj["data"]["firstName"], jsonObj["data"]["lastName"]
print "token:",jsonObj["data"]["authToken"]print "response data:\n",jsonData
#practical example Document.Properties["loggedInuser"]=jsonObj["data"]["firstName"]
from System import Uri from System.Net import HttpWebRequest from System.IO import StringReader, StreamReader, StreamWriter, MemoryStream, SeekOrigin #1 Data Source Settings usr = "username@someplace.com" pwd = "password" url = "https://my.webservice.com/api/authenticate" #2 Prepare and send url to server webRequest = HttpWebRequest.Create(Uri(url)) #2.1 Add authorization to http header authorization = usr + ":" + pwd from System.Text.Encoding import UTF8 binaryAuthorization = UTF8.GetBytes(authorization) from System import Convert authorization = Convert.ToBase64String(binaryAuthorization) authorization = "Basic " + authorization webRequest.Headers.Add("AUTHORIZATION", authorization) #3 Read response into memory webRequest.Method = 'POST' webRequest.ContentType = 'application/x-www-form-urlencoded' webRequest.ContentLength = 0 response = webRequest.GetResponse() streamReader = StreamReader(response.GetResponseStream()) jsonData = streamReader.ReadToEnd() #Parse JSON. Example {code:200,data:{userId:"jleviaguirre", firstName:"Jose", lastName:"leviaguirre", authToken:"ABCD01234..."}} import clr clr.AddReference('System.Web.Extensions') import System from System.Web.Script.Serialization import JavaScriptSerializer js = JavaScriptSerializer() jsonObj = js.Deserialize(jsonData,object) #parsed json data print "http status code:",jsonObj["code"] print "user id:",jsonObj["data"]["userId"] print "name:", jsonObj["data"]["firstName"], jsonObj["data"]["lastName"]
print "token:",jsonObj["data"]["authToken"]print "response data:\n",jsonData
#practical example Document.Properties["loggedInuser"]=jsonObj["data"]["firstName"]
ConversionConversion EmoticonEmoticon