public async Task <TokenResponse> StartAuthorizeAsync(Dictionary <string, string> parameters = null, int port = 80) { if (TryReadTokenFromFile(_tokenSavePath, out var token)) { return(token); } var requestParameters = _credentials.Dictionary(); requestParameters.Add("response_type", "code"); requestParameters.AddRange(parameters); var uri = _discoveryDocument.AuthorizeEndpoint.AppendQueryParameters(requestParameters); BrowserHelper.OpenUrl(uri); var listener = new HttpCallbackListener(port); string code = null; await listener.ListenAsync(message => { var query = message.RequestUri.ToString().Split("?")[1]; if (query.Length == 0) { throw new ArgumentOutOfRangeException(nameof(query), "The request uri does not contain a query."); } code = HttpUtility.ParseQueryString(query)["code"]; listener.Stop(); }); return(await RequestTokenWithCodeAsync(code) ?? throw new ArgumentNullException(nameof(code))); }