public async Task <bool> Login(CancellationToken ct = default(CancellationToken)) { Config.Log.LogOut("\t[Login]"); if (string.IsNullOrEmpty(Config.refresh_token)) { Thread t = new Thread(new ThreadStart(() => { Authkey = new FormLogin().Login(ct); })); t.SetApartmentState(System.Threading.ApartmentState.STA); t.IsBackground = true; t.Start(); while (t.IsAlive) { await Task.Delay(1000).ConfigureAwait(false); } if (Authkey != null && !string.IsNullOrEmpty(Authkey.access_token)) { key_timer = DateTime.Now; DriveData.RemoveCache(); return(true); } return(false); } else { return(await Refresh(ct).ConfigureAwait(false)); } }
private async void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e) { Text = e.Url.ToString(); var path = e.Url.AbsoluteUri; if (path.StartsWith(ConfigAPI.App_redirect)) { if (string.IsNullOrEmpty(ConfigAPI.client_secret)) { return; } const string code_str = "?code="; var i = path.IndexOf(code_str); if (i < 0) { return; } string code = path.Substring(i + code_str.Length, path.IndexOf('&', i) - i - code_str.Length); await GetAuthorizationCode(code, ct_soruce.Token); if (key != null && key.access_token != "") { webBrowser1.Navigate(ConfigAPI.LoginSuccess); timer1.Enabled = true; } } if (path.StartsWith(ConfigAPI.App_GetToken)) { try { var body = webBrowser1.DocumentText; var i = body.IndexOf("<PRE>"); var j = body.IndexOf("</PRE>", i); if (i < 0 || j < 0) { return; } key = ParseResponse(body.Substring(i + 5, j - i - 5)); // Save refresh_token Config.refresh_token = key.refresh_token; Config.Save(); } catch (Exception ex) { error_str = ex.ToString(); System.Diagnostics.Debug.WriteLine(error_str); } if (key != null && key.access_token != "") { webBrowser1.Navigate(ConfigAPI.LoginSuccess); timer1.Enabled = true; } } }
static private AuthKeys ParseResponse(string response) { AuthKeys key = new AuthKeys(); var serializer = new DataContractJsonSerializer(typeof(Authentication_Info)); using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(response))) { var data = (Authentication_Info)serializer.ReadObject(ms); key.access_token = data.access_token; key.refresh_token = data.refresh_token; } return(key); }
static public async Task <AuthKeys> RefreshAuthorizationCode(AuthKeys key, CancellationToken ct = default(CancellationToken)) { string error_str; using (var client = new HttpClient()) { try { var response = await client.PostAsync( (string.IsNullOrEmpty(ConfigAPI.client_secret))?ConfigAPI.App_RefreshToken : ConfigAPI.AmazonAPI_token, new FormUrlEncodedContent(new Dictionary <string, string> { { "grant_type", "refresh_token" }, { "refresh_token", key.refresh_token }, { "client_id", ConfigAPI.client_id }, { "client_secret", ConfigAPI.client_secret }, }), ct ).ConfigureAwait(false); response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); // Above three lines can be replaced with new helper method in following line // string body = await client.GetStringAsync(uri); key = ParseResponse(responseBody); // Save refresh_token Config.refresh_token = key.refresh_token; Config.Save(); } catch (HttpRequestException ex) { error_str = ex.Message; System.Diagnostics.Debug.WriteLine(error_str); } catch (OperationCanceledException) { throw; } catch (Exception ex) { error_str = ex.ToString(); System.Diagnostics.Debug.WriteLine(error_str); } } return(key); }
public async Task <bool> Refresh(CancellationToken ct = default(CancellationToken)) { Config.Log.LogOut("\t[Refresh]"); var newkey = new AuthKeys(); newkey.refresh_token = Config.refresh_token; newkey = await FormLogin.RefreshAuthorizationCode(newkey, ct).ConfigureAwait(false); if (newkey != null && !string.IsNullOrEmpty(newkey.access_token)) { Authkey = newkey; key_timer = DateTime.Now; return(true); } Config.refresh_token = ""; return(await Login(ct).ConfigureAwait(false)); }
private async Task GetAuthorizationCode(string access_code, CancellationToken ct = default(CancellationToken)) { using (var client = new HttpClient()) { try { var response = await client.PostAsync( ConfigAPI.AmazonAPI_token, new FormUrlEncodedContent(new Dictionary <string, string> { { "grant_type", "authorization_code" }, { "code", access_code }, { "client_id", ConfigAPI.client_id }, { "client_secret", ConfigAPI.client_secret }, { "redirect_uri", Uri.EscapeUriString(ConfigAPI.App_redirect) }, }), ct ); response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); // Above three lines can be replaced with new helper method in following line // string body = await client.GetStringAsync(uri); key = ParseResponse(responseBody); // Save refresh_token Config.refresh_token = key.refresh_token; Config.Save(); } catch (HttpRequestException ex) { error_str = ex.Message; System.Diagnostics.Debug.WriteLine(error_str); } catch (Exception ex) { error_str = ex.ToString(); System.Diagnostics.Debug.WriteLine(error_str); } } }