public static async Task HandleAsync(string clientId, string clientSecret, string callbackUrl) { string nonce = RandomValues.CreateNonce(); string state = RandomValues.CreateState(); string codeVerifier = RandomValues.CreateCodeVerifier(); string codeChallenge = Base64UrlTextEncoder.Encode(SHA256.HashData(Encoding.UTF8.GetBytes(codeVerifier))); callbackUrl ??= Urls.DefaultCallback; using HttpClient client = HttpClientFactory.Create(); JsonElement configuration = await client.GetAsJsonAsync(Urls.OpenIdConnectConfiguration); Task <string> waitForAuthorizationCodeTask = await AuthorizationCodeCallbackListener.StartAsync(callbackUrl, state); Browser.OpenLoginPage(configuration.AuthorizationEndpoint(), nonce, state, codeChallenge, clientId, callbackUrl); string authorizationCode = await waitForAuthorizationCodeTask; var tokenEndpointPostContent = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("grant_type", "authorization_code"), new KeyValuePair <string, string>("code", authorizationCode), new KeyValuePair <string, string>("redirect_uri", callbackUrl), new KeyValuePair <string, string>("code_verifier", codeVerifier), new KeyValuePair <string, string>("client_id", clientId), new KeyValuePair <string, string>("client_secret", clientSecret) }); JsonElement tokens = await client.PostAndReadAsJsonAsync(configuration.TokenEndpoint(), tokenEndpointPostContent); Console.WriteLine(tokens.FormatForDisplay()); }
private static async Task HandleAsync(string accessToken) { using var client = HttpClientFactory.Create(accessToken: accessToken); JsonElement profile = await client.GetAsJsonAsync(Urls.Profile); Console.WriteLine(profile.FormatForDisplay()); }
private static async Task HandleAsync(string serverUrl, string accessToken, string username, string password) { using var client = HttpClientFactory.Create(username, password, accessToken); serverUrl ??= await client.GetCloudServerUrlAsync(); JsonElement home = await client.GetAsJsonAsync(serverUrl, ManicTimeMediaType.V3); Console.WriteLine(home.FormatForDisplay()); }
private static async Task HandleAsync(string clientId, string clientSecret, string refreshToken) { using HttpClient client = HttpClientFactory.Create(); JsonElement configuration = await client.GetAsJsonAsync(Urls.OpenIdConnectConfiguration); var tokenEndpointPostContent = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("grant_type", "refresh_token"), new KeyValuePair <string, string>("refresh_token", refreshToken), new KeyValuePair <string, string>("client_id", clientId), new KeyValuePair <string, string>("client_secret", clientSecret) }); JsonElement tokens = await client.PostAndReadAsJsonAsync(configuration.TokenEndpoint(), tokenEndpointPostContent); Console.WriteLine(tokens.FormatForDisplay()); }
private static async Task HandleAsync(string serverUrl, string accessToken, string username, string password, string timelineKey, DateTime fromTime, DateTime toTime) { using var client = HttpClientFactory.Create(username, password, accessToken); serverUrl ??= await client.GetCloudServerUrlAsync(); JsonElement home = await client.GetAsJsonAsync(serverUrl, ManicTimeMediaType.V3); JsonElement timelines = await client.GetAsJsonAsync(Urls.Timelines(home.GetLinkHref("manictime/timelines"), timelineKey), ManicTimeMediaType.V3); JsonElement timeline = timelines.GetProperty("timelines").EnumerateArray().SingleOrDefault(); if (timeline.ValueKind == JsonValueKind.Undefined) { throw new InvalidOperationException("Timeline not found."); } JsonElement activities = await client.GetAsJsonAsync(Urls.Activities(timeline.GetLinkHref("manictime/activities"), fromTime, toTime), ManicTimeMediaType.V3); Console.WriteLine(activities.FormatForDisplay()); }
public static async Task HandleAsync(string serverUrl, string username, string password) { using HttpClient client = new HttpClient(); var tokenEndpointHref = await GetServerTokenEndpointUrlAsync(client, serverUrl); username ??= ConsoleHelper.ReadValue("Username: "******"Password: "******"grant_type", "password"), new KeyValuePair <string, string>("username", username), new KeyValuePair <string, string>("password", password) }); using HttpRequestMessage tokenEndpointRequest = new HttpRequestMessage(HttpMethod.Post, tokenEndpointHref) { Headers = { Accept = { new MediaTypeWithQualityHeaderValue(MediaTypeNames.Application.Json) } }, Content = tokenEndpointRequestContent }; using HttpResponseMessage tokenEndpointResponse = await client.SendAsync(tokenEndpointRequest); if (tokenEndpointResponse.StatusCode == HttpStatusCode.BadRequest) { JsonElement error = await tokenEndpointResponse.Content.ReadFromJsonAsync <JsonElement>(); if (string.Equals(error.GetProperty("error").GetString(), "invalid_grant", StringComparison.OrdinalIgnoreCase)) { throw new HttpRequestException("Invalid username or password", null, HttpStatusCode.BadRequest); } } tokenEndpointResponse.EnsureSuccessStatusCode(); JsonElement tokens = await tokenEndpointResponse.Content.ReadFromJsonAsync <JsonElement>(); Console.WriteLine(tokens.FormatForDisplay()); }