// Logs in using email and password public async void Login(string email, string password, string code) { client.DefaultRequestHeaders.Remove("Referer"); client.DefaultRequestHeaders.Add("Referer", "https://discordapp.com/login"); var log = new LoginRequest(email, password); var content = new JsonContent(log); var response = await client.PostAsync(DiscordAPI.loginEndpoint, content); var ree = await response.Content.ReadAsStringAsync(); var responseObject = Serializer.Parse <LoginPayload>(ree); client.DefaultRequestHeaders.Remove("Referer"); if (response.StatusCode == HttpStatusCode.BadRequest) { if (responseObject.Errors != null) { LoginError?.Invoke(new LoginErrorEventArgs(this, "Wrong email or password")); } if (responseObject.CaptchaKey != null) { LoginError?.Invoke(new LoginErrorEventArgs(this, "Log in and out of Discord and solve the captcha.")); } } else if (response.IsSuccessStatusCode) { if (string.IsNullOrEmpty(responseObject.Token)) { if (!string.IsNullOrEmpty(responseObject.Ticket)) { client.DefaultRequestHeaders.Add("Referer", "https://discordapp.com/channels/login"); // Adds a delay so we don't get ratelimited //await Task.Delay(1500); LoginMfa(responseObject.Ticket, code); } else { LoginError?.Invoke(new LoginErrorEventArgs(this, "Unknown error")); Debug.WriteLine("Unknown error"); } } else { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(responseObject.Token); client.DefaultRequestHeaders.Add("Referer", "https://discordapp.com/channels/@me"); DataStore.SaveToken(responseObject.Token); Ready?.Invoke(new ReadyEventArgs(this)); } } else { LoginError?.Invoke(new LoginErrorEventArgs(this, "Error " + response.StatusCode)); Debug.WriteLine("Error " + response.StatusCode); } }
private void OnError(object sender, ErrorEventArgs e) { Console.WriteLine("Error"); Console.WriteLine(e.ToString()); if (e.Exception.Message.Contains("401") && e.Exception.Message.Contains("HTTP")) { LoginError?.Invoke(); Disconnect(); return; } if (!clean) { CreateWebsocket(); } }
// Login using token, sets Authorization header and tests if the token is valid by sending a GET request to user API endpoint public async void Login(string token) { client.DefaultRequestHeaders.Remove("Referer"); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(token); client.DefaultRequestHeaders.Add("Referer", "https://discordapp.com/channels/@me"); var response = await client.GetAsync(DiscordAPI.userEndpoint); Debug.WriteLine($"{response.StatusCode}: {await response.Content.ReadAsStringAsync()}"); if (response.StatusCode == HttpStatusCode.Unauthorized) { LoginError?.Invoke(new LoginErrorEventArgs(this, "Invalid token")); Debug.WriteLine("Invalid token"); } else if (response.IsSuccessStatusCode) { DataStore.SaveToken(token); Ready?.Invoke(new ReadyEventArgs(this)); } }
public async void LoginMfa(string ticket, string code) { var log = new MfaRequest(code, ticket); var content = new JsonContent(log); var response = await client.PostAsync(DiscordAPI.mfaEndpoint, content); var responseObject = Serializer.Parse <MfaPayload>(await response.Content.ReadAsStringAsync()); client.DefaultRequestHeaders.Remove("Referer"); if (response.StatusCode == HttpStatusCode.BadRequest) { if (responseObject.Message != null) { LoginError?.Invoke(new LoginErrorEventArgs(this, responseObject.Message)); Debug.WriteLine(responseObject.Message); } } else if (response.IsSuccessStatusCode) { if (string.IsNullOrEmpty(responseObject.Token)) { LoginError?.Invoke(new LoginErrorEventArgs(this, "Unknown error")); Debug.WriteLine("Unknown error"); } else { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(responseObject.Token); client.DefaultRequestHeaders.Add("Referer", "https://discordapp.com/channels/@me"); DataStore.SaveToken(responseObject.Token); Ready?.Invoke(new ReadyEventArgs(this)); } } else { LoginError?.Invoke(new LoginErrorEventArgs(this, "Error " + response.StatusCode)); Debug.WriteLine("Error " + response.StatusCode); } }
protected virtual void OnLoginError(LoginEventArgs e) { LoginError?.Invoke(this, e); }