using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Text; using System.Net; using System.Web.Services.Protocols; using System.IO; namespace Google.Accounts { public class ClientLoginRequest : HttpPostClientProtocol { public const string DefaultService = "xapi"; public const string DefaultAccountType = "HOSTED"; public const string DefaultSource = "Company-ApplicationName-Version"; public ClientLoginRequest() { Service = DefaultService; AccountType = DefaultAccountType; Source = DefaultSource; this.Url = "https://www.google.com/accounts/ClientLogin"; this.Timeout = (int) new TimeSpan(0, 0, 15).TotalMilliseconds; } public string AccountType { get; set; } public string Email { get; set; } public string Password { get; set; } public string Service { get; set; } public string Source { get; set; } public Captcha Captcha {get; set; } public ClientSecurityToken Login() { if (Email == null) throw new InvalidOperationException("The email must be set"); if (Password == null) throw new InvalidOperationException("The password must be set"); if (Source == null) throw new InvalidOperationException("The source must be set"); if (Service == null) throw new InvalidOperationException("The service must be set"); try { string email = this.Email.IndexOf('@') == -1 ? this.Email + "@gmail.com" : this.Email; ClientSecurityToken token = LoginRaw ( this.AccountType, email, this.Password, this.Service, this.Source, this.Captcha != null ? this.Captcha.Token : null, this.Captcha != null ? this.Captcha.Answer : null); token.Email = email; return token; } catch (WebException ex) { HttpWebResponse response = (HttpWebResponse) ex.Response; if (response !=null && response.StatusCode == HttpStatusCode.Forbidden) { ReadClientLoginFailureResponse(response); } throw; } } [HttpMethod(typeof(TextReturnReader), typeof(HtmlFormParameterWriter))] public ClientSecurityToken LoginRaw(string accountType, string Email, string Passwd, string service, string source, string logintoken, string logincaptcha) { ClientSecurityToken result = (ClientSecurityToken) this.Invoke( "LoginRaw", this.Url, new object[] {accountType, Email, Passwd, service, source, logintoken, logincaptcha }); return result; } internal static void ReadClientLoginFailureResponse(HttpWebResponse response) { TextReturnReader r = new TextReturnReader(); r.Initialize(new PatternMatcher(typeof(ClientLoginException))); using (Stream stream = response.GetResponseStream()) { throw (ClientLoginException)r.Read(response, stream); } } } [Serializable] public class ClientSecurityToken { public string Email { get; set; } [Match(@"Auth=(\S+)")] public string Auth { get; set; } [Match(@"SID=(\S+)")] public string SID { get; set; } [Match(@"LSID=(\S+)")] public string LSID { get; set; } } public class ClientLoginException : Exception { [Match(@"(?