Esempio n. 1
0
        async Task WebSocketTest(IamTokenData token, int userID)
        {
            CancellationTokenSource cts = new CancellationTokenSource();

            ClientWebSocket clientWebSocket = new ClientWebSocket();

            clientWebSocket.Options.Proxy = null;
            clientWebSocket.Options.SetRequestHeader("Authorization", $"Bearer {token.AccessToken}");

            Uri connection = new Uri($"wss://gateway-syd.watsonplatform.net/speech-to-text/api/v1/recognize?model=fr-FR_BroadbandModel");

            try
            {
                await clientWebSocket.ConnectAsync(connection, cts.Token);

                Console.WriteLine("Connected!");
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed to connect: " + e.ToString());
                return;
            }

            // send opening message and wait for initial delimeter
            Task.WaitAll(clientWebSocket.SendAsync(openingMessage, WebSocketMessageType.Text, true, CancellationToken.None), HandleResults(clientWebSocket, userID));

            // send all audio and then a closing message; simltaneously print all results until delimeter is recieved
            Task.WaitAll(SendAudio(clientWebSocket), HandleResults(clientWebSocket, userID));

            // close down the websocket
            clientWebSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Close", CancellationToken.None).Wait();
        }
Esempio n. 2
0
        public void SpeechToText()
        {
            Credentials  cred  = new Credentials();
            IamTokenData token = GetIAMToken(cred.STTApiKey);

            WebSocketTest(token, userID).Wait();
        }
Esempio n. 3
0
 public TokenManager(TokenOptions options)
 {
     iamUrl = !string.IsNullOrEmpty(options.IamUrl) ? options.IamUrl
         : "https://iam.bluemix.net/identity/token";
     if (!string.IsNullOrEmpty(options.IamApiKey))
     {
         iamApikey = options.IamApiKey;
     }
     if (!string.IsNullOrEmpty(options.IamAccessToken))
     {
         userAccessToken = options.IamAccessToken;
     }
     tokenInfo = new IamTokenData();
     Client    = new WatsonHttpClient(iamUrl);
 }
Esempio n. 4
0
        private IamTokenData RefreshToken()
        {
            IamTokenData result = null;

            try
            {
                if (string.IsNullOrEmpty(tokenInfo.RefreshToken))
                {
                    throw new ArgumentNullException(nameof(tokenInfo.RefreshToken));
                }
                var request = Client.PostAsync(iamUrl);
                request.WithHeader("Content-type", "application/x-www-form-urlencoded");
                request.WithHeader("Authorization", "Basic Yng6Yng=");

                List <KeyValuePair <string, string> > content      = new List <KeyValuePair <string, string> >();
                KeyValuePair <string, string>         grantType    = new KeyValuePair <string, string>("grant_type", "refresh_token");
                KeyValuePair <string, string>         refreshToken = new KeyValuePair <string, string>("refresh_token", tokenInfo.RefreshToken);
                content.Add(grantType);
                content.Add(refreshToken);

                var formData = new FormUrlEncodedContent(content);

                request.WithBodyContent(formData);

                result = request.As <IamTokenData>().Result;

                if (result == null)
                {
                    result = new IamTokenData();
                }
            }
            catch (AggregateException ae)
            {
                throw ae.Flatten();
            }

            return(result);
        }
Esempio n. 5
0
        private IamTokenData RequestToken()
        {
            IamTokenData result = null;

            try
            {
                var request = Client.PostAsync(iamUrl);
                request.WithHeader("Content-type", "application/x-www-form-urlencoded");
                request.WithHeader("Authorization", "Basic Yng6Yng=");

                List <KeyValuePair <string, string> > content      = new List <KeyValuePair <string, string> >();
                KeyValuePair <string, string>         grantType    = new KeyValuePair <string, string>("grant_type", "urn:ibm:params:oauth:grant-type:apikey");
                KeyValuePair <string, string>         responseType = new KeyValuePair <string, string>("response_type", "cloud_iam");
                KeyValuePair <string, string>         apikey       = new KeyValuePair <string, string>("apikey", iamApikey);
                content.Add(grantType);
                content.Add(responseType);
                content.Add(apikey);

                var formData = new FormUrlEncodedContent(content);

                request.WithBodyContent(formData);

                result = request.As <IamTokenData>().Result;

                if (result == null)
                {
                    result = new IamTokenData();
                }
            }
            catch (AggregateException ae)
            {
                throw ae.Flatten();
            }

            return(result);
        }
Esempio n. 6
0
 private void SaveTokenInfo(IamTokenData tokenResponse)
 {
     tokenInfo = tokenResponse;
 }