Пример #1
0
        async Task <string> FetchToken()
        {
            try
            {
                var uri = authEndpoint.ToUriBuilder(SpeechRegion).Uri;

                Debug.WriteLine($"{DateTime.Now} :: Request Uri: {uri}");

                var result = await client.PostAsync(uri, null);

                if (result.IsSuccessStatusCode)
                {
                    Debug.WriteLine("New authentication token retrieved at {0}", DateTime.Now);

                    return(await result.Content.ReadAsStringAsync());
                }

                throw new Exception($"Unable to authenticate, auth endpoint returned: status code {result.StatusCode} ; Reason: {result.ReasonPhrase}");
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Error during auth post: {0}", ex);
                throw;
            }
        }
Пример #2
0
        HttpRequestMessage CreateRequest(OutputMode outputMode)
        {
            try
            {
                var uriBuilder = speechEndpoint.ToUriBuilder(SpeechRegion);

                uriBuilder.Path  += $"/{RecognitionMode.ToString ().ToLower ()}/cognitiveservices/{ApiVersion}";
                uriBuilder.Query += $"language={RecognitionLanguage}&format={outputMode.ToString ().ToLower ()}&profanity={ProfanityMode.ToString ().ToLower ()}";

                Debug.WriteLine($"{DateTime.Now} :: Request Uri: {uriBuilder.Uri}");

                var request = new HttpRequestMessage(HttpMethod.Post, uriBuilder.Uri)
                {
                    Version = HttpVersion.Version11
                };

                request.Headers.TransferEncodingChunked = true;
                request.Headers.ExpectContinue          = true;
                request.Headers.Accept.ParseAdd(Constants.MimeTypes.Json);
                request.Headers.Accept.ParseAdd(Constants.MimeTypes.Xml);
                request.Headers.Host = speechEndpoint.Host;

                switch (AuthenticationMode)
                {
                case AuthenticationMode.SubscriptionKey:
                    request.Headers.Add(Constants.Keys.SubscriptionKey, subscriptionKey);
                    break;

                case AuthenticationMode.AuthorizationToken:
                    request.Headers.Authorization = new AuthenticationHeaderValue(Constants.Keys.Bearer, AuthClient.Token);
                    break;
                }

                return(request);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                throw;
            }
        }