public void getAPIToken() { if (apiToken == null || DateTimeOffset.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds >= accessTokenExpires) { var apiClient = new RestClient("https://www.nexon.com"); apiClient.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) NexonLauncher/2.1.0 Chrome/66.0.3359.181 Electron/3.0.4 Safari/537.36"; var request = new RestRequest("account-webapi/login/launcher", Method.POST); ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; var requestBody = new AccountLoginJson { id = username, password = password, auto_login = false, client_id = clientID, scope = "us.launcher.all", device_id = NxUtil.getUUID(), }; request.AddJsonBody(requestBody); IRestResponse response = apiClient.Execute(request); var body = JsonConvert.DeserializeObject <dynamic>(response.Content); if (body["code"] != null && ((String)body["code"]).Equals("TRUST_DEVICE_REQUIRED", StringComparison.InvariantCultureIgnoreCase)) { trustDevicePost(); } else { TimeSpan span = DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0)); apiToken = body["access_token"]; string expireTime = body["access_token_expires_in"]; accessTokenExpires = span.TotalSeconds + Double.Parse(expireTime); //add to rest client cookies cookies.Add(new Cookie("nxtk", apiToken) { Domain = ".nexon.net" }); } } }
private void trustDevicePost() { string code = Microsoft.VisualBasic.Interaction.InputBox("Please enter the 2 factor verification code sent to your email.", "Trust Device"); if (!String.IsNullOrEmpty(code)) { var apiClient = new RestClient("https://www.nexon.com"); apiClient.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) NexonLauncher/2.1.0 Chrome/66.0.3359.181 Electron/3.0.4 Safari/537.36"; var request = new RestRequest("account-webapi/trusted_devices", Method.PUT); ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; var requestBody = new TrustDevice { email = username, verification_code = code, device_id = NxUtil.getUUID(), remember_me = true }; request.AddJsonBody(requestBody); IRestResponse response = apiClient.Execute(request); if (response.StatusCode != HttpStatusCode.OK) { MessageBox.Show("That code wasn't accepted!"); } else { //Retry login with trusted device getAPIToken(); } } else { MessageBox.Show("You must enter something into the 2FA code prompt"); } }