private static Task <IDictionary <string, string> > PerformMasterLoginAsync(string email, string password) { var signature = GoogleKeyUtils.CreateSignature(email, password, androidKey); var request = GenerateBaseRequest(email, signature, "ac2dm"); request.Add("add_account", "1"); return(PerformAuthRequestAsync(request)); }
// signature public static string CreateSignature(string email, string password, RSAParameters key) { using (var rsa = new RSACryptoServiceProvider()) { rsa.ImportParameters(key); var sha1 = SHA1.Create(); byte[] prefix = { 0x00 }; var hash = sha1.ComputeHash(GoogleKeyUtils.KeyToStruct(key)).Take(4).ToArray(); var encrypted = rsa.Encrypt(Encoding.UTF8.GetBytes(email + "\x00" + password), true); return(DataTypeUtils.UrlSafeBase64(DataTypeUtils.CombineBytes(prefix, hash, encrypted))); } }
// perform_master_login public Dictionary <string, string> PerformMasterLogin(string service = "ac2dm", string deviceCountry = "us", string operatorCountry = "us", string lang = "en", int sdkVersion = 21) { string signature = GoogleKeyUtils.CreateSignature(email, password, androidKey); var dict = new Dictionary <string, string> { { "accountType", "HOSTED_OR_GOOGLE" }, { "Email", email }, { "has_permission", 1.ToString() }, { "add_account", 1.ToString() }, { "EncryptedPasswd", signature }, { "service", service }, { "source", "android" }, { "device_country", deviceCountry }, { "operatorCountry", operatorCountry }, { "lang", lang }, { "sdk_version", sdkVersion.ToString() } }; return(PerformAuthRequest(dict)); }
private async static Task <IDictionary <string, string> > PerformAuthRequestAsync(IDictionary <string, string> data) { var handler = new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip, AllowAutoRedirect = false }; using (var tempHttpClient = new HttpClient(handler)) { tempHttpClient.DefaultRequestHeaders.UserAgent.ParseAdd(userAgent); HttpResponseMessage response; using (var formUrlEncodedContent = new FormUrlEncodedContent(data)) { response = await tempHttpClient.PostAsync(authUrl, formUrlEncodedContent).ConfigureAwait(false); } var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false); return(GoogleKeyUtils.ParseAuthResponse(content)); } }
// _perform_auth_request private Dictionary <string, string> PerformAuthRequest(Dictionary <string, string> data) { NameValueCollection nvc = new NameValueCollection(); foreach (var kvp in data) { nvc.Add(kvp.Key.ToString(), kvp.Value.ToString()); } using (WebClient client = new WebClient()) { client.Headers.Add(HttpRequestHeader.UserAgent, userAgent); string result; try { byte[] response = client.UploadValues(authUrl, nvc); result = Encoding.UTF8.GetString(response); } catch (WebException e) { result = new StreamReader(e.Response.GetResponseStream()).ReadToEnd(); } return(GoogleKeyUtils.ParseAuthResponse(result)); } }