Exemple #1
0
        private AccessTokenInfo GetToken()
        {
            WebRequest webRequest = WebRequest.Create("https://oxford-speech.cloudapp.net/token/issueToken");

            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.Method      = "POST";

            byte[] bytes = Encoding.ASCII.GetBytes(_requestDetails);
            webRequest.ContentLength = bytes.Length;

            try
            {
                using (Stream outputStream = webRequest.GetRequestStream())
                {
                    outputStream.Write(bytes, 0, bytes.Length);
                }

                using (WebResponse webResponse = webRequest.GetResponse())
                {
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(AccessTokenInfo));
                    AccessTokenInfo            token      = (AccessTokenInfo)serializer.ReadObject(webResponse.GetResponseStream());
                    return(token);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                return(null);
            }
        }
Exemple #2
0
        public bool GenerateAuthenticationToken(string clientId, string clientSecret)
        {
            Authentication auth = new Authentication(clientId, clientSecret);

            try
            {
                _token = auth.Token;

                if (_token != null)
                {
                    _authorizationToken = $"Bearer {_token.access_token}";

                    return(true);
                }
                else
                {
                    RaiseOnError(new AudioErrorEventArgs("Failed to generate authentication token."));
                    return(false);
                }
            }
            catch (Exception ex)
            {
                RaiseOnError(new AudioErrorEventArgs($"Failed to generate authentication token - {ex.Message}"));

                return(false);
            }
        }
Exemple #3
0
        public Authentication(string clientId, string clientSecret)
        {
            _requestDetails = string.Format("grant_type=client_credentials&client_id={0}&client_secret={1}&scope={2}",
                                            HttpUtility.UrlEncode(clientId),
                                            HttpUtility.UrlEncode(clientSecret),
                                            HttpUtility.UrlEncode("https://speech.platform.bing.com"));

            _token = GetToken();

            _tokenRenewer = new Timer(new TimerCallback(OnTokenExpiredCallback),
                                      this,
                                      TimeSpan.FromMinutes(TokenRefreshInterval),
                                      TimeSpan.FromMilliseconds(-1));
        }
Exemple #4
0
 private void OnTokenExpiredCallback(object stateInfo)
 {
     try
     {
         AccessTokenInfo newAccessToken = GetToken();
         _token = newAccessToken;
     }
     catch (Exception ex)
     {
         Console.WriteLine(string.Format("Failed renewing access token. Details: {0}", ex.Message));
     }
     finally
     {
         _tokenRenewer.Change(TimeSpan.FromMinutes(TokenRefreshInterval), TimeSpan.FromMilliseconds(-1));
     }
 }