Created
October 22, 2010 23:54
-
-
Save TheDahv/641567 to your computer and use it in GitHub Desktop.
For getting a Google auth token using Client Login without Google's libraries in F#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System.Text | |
open System.IO | |
open System.Net | |
open System.Web | |
let auth = | |
let URL = "https://www.google.com/accounts/ClientLogin" | |
let webRequest = HttpWebRequest.Create(URL) | |
webRequest.Method <- "POST" | |
webRequest.ContentType <- "application/x-www-form-urlencoded" | |
let postParams = | |
"accountType=" + HttpUtility.UrlEncode(ACCOUNT_TYPE) + // "GOOGLE" | |
"&Email=" + HttpUtility.UrlEncode(email) + | |
"&Passwd=" + HttpUtility.UrlEncode(password) + | |
"&source=" + HttpUtility.UrlEncode(SOURCE) + // "COMPANY-APPLICATION-VERSION" | |
"&service=" + HttpUtility.UrlEncode(SERVICE) // "analytics" | |
let postBytes = Encoding.UTF8.GetBytes(postParams) | |
webRequest.ContentLength <- (int64)postBytes.Length | |
let strmReq = webRequest.GetRequestStream() | |
strmReq.Write(postBytes, 0, postBytes.Length) | |
strmReq.Close() | |
let GetAuthFromResponse (response:WebResponse) = | |
let responseStream = response.GetResponseStream() | |
let reader = new StreamReader(responseStream) | |
let readerResponse = | |
reader.ReadToEnd().Split('\n') | |
let sResponse = | |
readerResponse | |
|> Array.filter( fun str -> str.Split('=').Length >= 2 ) | |
|> Array.map (fun a -> | |
let subsplit = a.Split('=') | |
(subsplit.[0], subsplit.[1]) | |
) | |
sResponse |> Array.find( fun (key, value) -> key.ToLower() = "auth" ) |> snd | |
let response = webRequest.GetResponse() | |
GetAuthFromResponse response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment