private HttpClient AuthenticateHttpClient(UnifiAccount account) { var tokenResponse = account.Authenticate(); Assert.IsFalse(tokenResponse.IsError); Assert.IsNotNull(tokenResponse.AccessToken); var httpClient = new HttpClient(); httpClient.SetBearerToken(tokenResponse.AccessToken); httpClient.DefaultRequestHeaders.Remove("Accept"); httpClient.DefaultRequestHeaders.Add("Accept", "application/json;odata.metadata=minimal"); return(httpClient); }
private bool TestUnifiAccount(UnifiAccount unifiAccount) { using (var unifiSession = new UnifiSession(unifiAccount)) { try { var tokenResponse = unifiAccount.Authenticate(); if (tokenResponse.IsError) { string error = tokenResponse.ErrorDescription ?? tokenResponse.Error; MessageDlg.Show(this, TextUtil.LineSeparate(Resources.EditRemoteAccountDlg_TestUnifiAccount_An_error_occurred_while_trying_to_authenticate_, error)); if (tokenResponse.Error == "invalid_scope") // Not L10N { tbxClientScope.Focus(); } else if (tokenResponse.Error == "invalid_client") // Not L10N { tbxClientSecret.Focus(); } else if (tokenResponse.HttpStatusCode == HttpStatusCode.NotFound) { tbxIdentityServer.Focus(); } else { textPassword.Focus(); } return(false); } } catch (Exception e) { MessageDlg.ShowWithException(this, Resources.EditRemoteAccountDlg_TestUnifiAccount_An_error_occurred_while_trying_to_authenticate_, e); tbxIdentityServer.Focus(); return(false); } bool[] contentsAvailable = new bool[1]; unifiSession.ContentsAvailable += () => { lock (contentsAvailable) { contentsAvailable[0] = true; Monitor.Pulse(contentsAvailable); } }; using (var longWaitDlg = new LongWaitDlg()) { try { longWaitDlg.PerformWork(this, 1000, (ILongWaitBroker broker) => { while (!broker.IsCanceled) { RemoteServerException remoteServerException; if (unifiSession.AsyncFetchContents(unifiAccount.GetRootUrl(), out remoteServerException)) { if (remoteServerException != null) { throw remoteServerException; } break; } lock (contentsAvailable) { while (!contentsAvailable[0] && !broker.IsCanceled) { Monitor.Wait(contentsAvailable, 10); } } } }); } catch (OperationCanceledException) { return(false); } catch (Exception e) { MessageDlg.ShowWithException(this, Resources.EditRemoteAccountDlg_TestUnifiAccount_An_exception_occurred_while_trying_to_fetch_the_directory_listing_, e); textServerURL.Focus(); return(false); } } } MessageDlg.Show(this, Resources.EditChorusAccountDlg_TestSettings_Settings_are_correct); return(true); }