Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #r "../../../bin/FSharp.Data.dll"
- open FSharp.Data
- open System
- let url = "https://app.sablono.com/api/projectStatus.xsjs?project=350f83c2-3f3d-49dc-90ec-cf22d1562c3c2"
- let username = "xxx"
- let password = "yyy"
- let addLogin (req: Net.HttpWebRequest) =
- req.Credentials <- new Net.NetworkCredential(username,password)
- req
- let txt = Http.RequestString (url ,customizeHttpRequest = addLogin)
- 1. GET - https://app.sablono.com/sap/hana/xs/formLogin/token.xsjs
- with RequestHeader : X-CSRF-Token:Fetch
- Reply ResponseHeader : X-CSRF-Token
- Can be null.
- 2. POST - https://app.sablono.com/sap/hana/xs/formLogin/login.xscfunc
- With RequestHeader : X-CSRF-Token:<Reply from previous request Request>
- and Body as - Form Data:
- xs-username=<USERNAME>&xs-password=<PASSWORD>
- ResponseHeader:
- set-cookie:xsId04BF0299185B59F3F3F554A382524B98=A22594ACC3339C4F86FAB57F68CF9FDB; path=/; HttpOnly
- set-cookie:sapxslb=6D458FD9581EE04333FC0494D0E9F113; path=/; HttpOnly
- Http.RequestString("https://app.sablono.com/sap/hana/xs/formLogin/login.xscfunc",
- body = FormValues ["xs-username","username";"xs-password","password"])
- Http.RequestString("https://app.sablono.com/sap/hana/xs/formLogin/login.xscfunc",
- body = FormValues ["xs-username","username";"xs-password","password"],
- headers = [ "X-CSRF-Token", "reply from first request" ])
- open System.Text
- open System.Net
- open System.IO
- let get projectId =
- let un = "USERNAME"
- let pw = "password"
- let cookieCont =
- let token =
- printfn "getting token.."
- let req1 = HttpWebRequest.Create "https://app.sablono.com/sap/hana/xs/formLogin/token.xsjs" :?> HttpWebRequest
- req1.Headers.Add("X-CSRF-Token", "Fetch") //You have to add the X-CSRF-Token = Fetch so that the system can reply with the other token
- let resp = req1.GetResponse()
- resp.Headers.Get("X-CSRF-Token") //The first time the token is "unsafe"
- let jar = new CookieContainer()
- let req2 = HttpWebRequest.Create "https://app.sablono.com/sap/hana/xs/formLogin/login.xscfunc" :?> HttpWebRequest
- let byteArray =
- let postData = "xs-username=" + un + "&xs-password=" + pw
- Encoding.UTF8.GetBytes(postData)
- req2.CookieContainer <- jar
- req2.Method <- "POST"
- req2.Headers.Add("X-CSRF-Token", token)
- req2.ContentType <-"application/x-www-form-urlencoded charset=UTF-8"
- req2.ContentLength <- int64 byteArray.Length
- //Put username and password inside the body
- use dataStream = req2.GetRequestStream()
- dataStream.Write(byteArray, 0, byteArray.Length)
- let _ = // this is to avoid 401 error
- let login_resp = req2.GetResponse()
- login_resp.GetResponseStream()
- dataStream.Close()
- jar
- printfn "downloading JSON .."
- let req3 = HttpWebRequest.Create ("https://app.sablono.com/api/projectStatus.xsjs?project=" + projectId) :?> HttpWebRequest
- req3.AutomaticDecompression <- DecompressionMethods.GZip
- req3.CookieContainer <- cookieCont
- req3.ContentType <- "application/jsoncharset=UTF-8"
- req3.Accept <- "application/json"
- let resultJSON =
- let resp = req3.GetResponse()
- let respStream = resp.GetResponseStream()
- use reader = new StreamReader(respStream)
- reader.ReadToEnd()
- resultJSON
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement