Advertisement
Guest User

Untitled

a guest
Jan 25th, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. #r "../../../bin/FSharp.Data.dll"
  2. open FSharp.Data
  3. open System
  4.  
  5. let url = "https://app.sablono.com/api/projectStatus.xsjs?project=350f83c2-3f3d-49dc-90ec-cf22d1562c3c2"
  6. let username = "xxx"
  7. let password = "yyy"
  8.  
  9. let addLogin (req: Net.HttpWebRequest) =
  10. req.Credentials <- new Net.NetworkCredential(username,password)
  11. req
  12.  
  13. let txt = Http.RequestString (url ,customizeHttpRequest = addLogin)
  14.  
  15. 1. GET - https://app.sablono.com/sap/hana/xs/formLogin/token.xsjs
  16.  
  17. with RequestHeader : X-CSRF-Token:Fetch
  18. Reply ResponseHeader : X-CSRF-Token
  19. Can be null.
  20. 2. POST - https://app.sablono.com/sap/hana/xs/formLogin/login.xscfunc
  21.  
  22. With RequestHeader : X-CSRF-Token:<Reply from previous request Request>
  23. and Body as - Form Data:
  24. xs-username=<USERNAME>&xs-password=<PASSWORD>
  25. ResponseHeader:
  26. set-cookie:xsId04BF0299185B59F3F3F554A382524B98=A22594ACC3339C4F86FAB57F68CF9FDB; path=/; HttpOnly
  27. set-cookie:sapxslb=6D458FD9581EE04333FC0494D0E9F113; path=/; HttpOnly
  28.  
  29. Http.RequestString("https://app.sablono.com/sap/hana/xs/formLogin/login.xscfunc",
  30. body = FormValues ["xs-username","username";"xs-password","password"])
  31.  
  32. Http.RequestString("https://app.sablono.com/sap/hana/xs/formLogin/login.xscfunc",
  33. body = FormValues ["xs-username","username";"xs-password","password"],
  34. headers = [ "X-CSRF-Token", "reply from first request" ])
  35.  
  36. open System.Text
  37. open System.Net
  38. open System.IO
  39.  
  40. let get projectId =
  41. let un = "USERNAME"
  42. let pw = "password"
  43.  
  44.  
  45. let cookieCont =
  46. let token =
  47. printfn "getting token.."
  48. let req1 = HttpWebRequest.Create "https://app.sablono.com/sap/hana/xs/formLogin/token.xsjs" :?> HttpWebRequest
  49. 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
  50. let resp = req1.GetResponse()
  51. resp.Headers.Get("X-CSRF-Token") //The first time the token is "unsafe"
  52.  
  53. let jar = new CookieContainer()
  54. let req2 = HttpWebRequest.Create "https://app.sablono.com/sap/hana/xs/formLogin/login.xscfunc" :?> HttpWebRequest
  55. let byteArray =
  56. let postData = "xs-username=" + un + "&xs-password=" + pw
  57. Encoding.UTF8.GetBytes(postData)
  58.  
  59. req2.CookieContainer <- jar
  60. req2.Method <- "POST"
  61. req2.Headers.Add("X-CSRF-Token", token)
  62. req2.ContentType <-"application/x-www-form-urlencoded charset=UTF-8"
  63. req2.ContentLength <- int64 byteArray.Length
  64.  
  65. //Put username and password inside the body
  66. use dataStream = req2.GetRequestStream()
  67. dataStream.Write(byteArray, 0, byteArray.Length)
  68.  
  69. let _ = // this is to avoid 401 error
  70. let login_resp = req2.GetResponse()
  71. login_resp.GetResponseStream()
  72.  
  73. dataStream.Close()
  74. jar
  75.  
  76.  
  77.  
  78. printfn "downloading JSON .."
  79. let req3 = HttpWebRequest.Create ("https://app.sablono.com/api/projectStatus.xsjs?project=" + projectId) :?> HttpWebRequest
  80. req3.AutomaticDecompression <- DecompressionMethods.GZip
  81. req3.CookieContainer <- cookieCont
  82. req3.ContentType <- "application/jsoncharset=UTF-8"
  83. req3.Accept <- "application/json"
  84.  
  85.  
  86. let resultJSON =
  87. let resp = req3.GetResponse()
  88. let respStream = resp.GetResponseStream()
  89. use reader = new StreamReader(respStream)
  90. reader.ReadToEnd()
  91.  
  92. resultJSON
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement