/// <summary> /// Finds the specified token in the given <see cref="TokenCollection"/> object /// </summary> /// <param name="tokens"><see cref="TokenCollection"/> object</param> /// <param name="key">Key to find</param> /// <returns>The value associated with the key</returns> public static string FindToken(TokenCollection tokens, string key) { if (tokens == null) { throw new ArgumentNullException("tokens"); } string returnValue = null; bool fNextOne = false; foreach (string token in tokens) { if (fNextOne) { returnValue = token; break; } if (key == token) { // next one is it fNextOne = true; } } return returnValue; }
/// <summary> /// Parses the stream in token collection. /// </summary> /// <param name="inputStream">The input stream.</param> /// <returns>The <see cref="TokenCollection"/> object</returns> public static TokenCollection ParseStreamInTokenCollection(Stream inputStream) { // get the body and parse it var encoder = new ASCIIEncoding(); var readStream = new StreamReader(inputStream, encoder); String body = readStream.ReadToEnd(); readStream.Close(); // all we are interested is the token, so we break the string in parts var tokens = new TokenCollection(body, '=', true, 2); return tokens; }
/// <summary>Standard constructor</summary> public TokenEnumerator(TokenCollection tokens) { _tokens = tokens; }
/// <summary>Standard constructor</summary> public TokenEnumerator(TokenCollection tokens) { _tokens = tokens; }
private static string QueryClientLoginToken(GDataCredentials gc, string serviceName, string applicationName, bool fUseKeepAlive, Uri clientLoginHandler, IWebProxy proxy) { if (gc == null) { throw new ArgumentNullException("gc", "No credentials supplied"); } HttpWebRequest authRequest = WebRequest.Create(clientLoginHandler) as HttpWebRequest; if (authRequest != null) { if (proxy != null) { authRequest.Proxy = proxy; } authRequest.KeepAlive = fUseKeepAlive; string accountType = GoogleAuthentication.ACCOUNT_TYPE; if (!String.IsNullOrEmpty(gc.AccountType)) { accountType += gc.AccountType; } else { accountType += GoogleAuthentication.ACCOUNT_TYPE_DEFAULT; } WebResponse authResponse; string authToken = null; try { authRequest.ContentType = "application/x-www-form-urlencoded"; authRequest.Method = "POST"; var encoder = new ASCIIEncoding(); string user = gc.Username ?? ""; string pwd = gc.Password ?? ""; // now enter the data in the stream string postData = GoogleAuthentication.EMAIL + "=" + Utility.UriEncodeUnsafe(user) + "&"; postData += GoogleAuthentication.PASSWORD + "=" + Utility.UriEncodeUnsafe(pwd) + "&"; postData += GoogleAuthentication.SOURCE + "=" + Utility.UriEncodeUnsafe(applicationName) + "&"; postData += GoogleAuthentication.SERVICE + "=" + Utility.UriEncodeUnsafe(serviceName) + "&"; if (gc.CaptchaAnswer != null) { postData += GoogleAuthentication.CAPTCHA_ANSWER + "=" + Utility.UriEncodeUnsafe(gc.CaptchaAnswer) + "&"; } if (gc.CaptchaToken != null) { postData += GoogleAuthentication.CAPTCHA_TOKEN + "=" + Utility.UriEncodeUnsafe(gc.CaptchaToken) + "&"; } postData += accountType; byte[] encodedData = encoder.GetBytes(postData); authRequest.ContentLength = encodedData.Length; Stream requestStream = authRequest.GetRequestStream(); requestStream.Write(encodedData, 0, encodedData.Length); requestStream.Close(); authResponse = authRequest.GetResponse(); } catch (WebException e) { authResponse = e.Response; } var response = authResponse as HttpWebResponse; if (response != null) { // check the content type, it must be text if (!response.ContentType.StartsWith("text")) { throw new Exception( "Execution of authentication request returned unexpected content type: " + response.ContentType); } TokenCollection tokens = Utility.ParseStreamInTokenCollection(response.GetResponseStream()); authToken = Utility.FindToken(tokens, GoogleAuthentication.AUTH_TOKEN); if (authToken == null) { throw new Exception("Authentication failed. Try again later."); } // failsafe. if getAuthException did not catch an error... var code = (int)response.StatusCode; if (code != 200) { throw new Exception("Execution of authentication request returned unexpected result: " + code); } } if (authResponse != null) { authResponse.Close(); } return(authToken); } throw new OutOfMemoryException("Unable to create WebRequest."); }