private void RenewAccessToken()
        {
            AccessTokenInfo newAccessToken = PostHttp(AccessUri, this.requestBody);

            //swap the new token with old one
            //Note: the swap is thread unsafe
            this.token = newAccessToken;
            Console.WriteLine(string.Format("Renewed token for user: {0} is: {1}",
                                            this.clientId,
                                            this.token.access_token));
        }
        public Authentication(string clientId, string clientSecret)
        {
            this.clientId     = clientId;
            this.clientSecret = clientSecret;

            // If clientid or client secret has special characters, encode before sending request
            this.requestBody = 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"));

            this.token = PostHttp(AccessUri, this.requestBody);

            // renew the token every specfied minutes
            accessTokenRenewer = new Timer(new TimerCallback(OnTokenExpiredCallback),
                                           this,
                                           TimeSpan.FromMinutes(RefreshTokenDuration),
                                           TimeSpan.FromMilliseconds(-1));
        }
        private AccessTokenInfo PostHttp(string accessUri, string requestDetails)
        {
            //Prepare OAuth request
            WebRequest webRequest = WebRequest.Create(accessUri);

            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.Method      = "POST";
            byte[] bytes = Encoding.ASCII.GetBytes(requestDetails);
            webRequest.ContentLength = bytes.Length;
            using (Stream outputStream = webRequest.GetRequestStream())
            {
                outputStream.Write(bytes, 0, bytes.Length);
            }
            using (WebResponse webResponse = webRequest.GetResponse())
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(AccessTokenInfo));
                //Get deserialized object from JSON stream
                AccessTokenInfo token = (AccessTokenInfo)serializer.ReadObject(webResponse.GetResponseStream());
                return(token);
            }
        }
Esempio n. 4
0
        public static byte[] TtsAudioOutput(string lang, string voiceName, AudioFormat format, string text, float prosodyRate = 1.0f)
        {
            byte[] output = null;

            AccessTokenInfo token       = auth.GetAccessToken();
            string          accessToken = token.access_token;
            string          uri         = "https://speech.platform.bing.com/synthesize";

            HttpWebRequest webRequest     = (HttpWebRequest)WebRequest.Create(uri);
            string         ImpressionGUID = Guid.NewGuid().ToString();

            webRequest.ContentType = "application/ssml+xml";
            webRequest.UserAgent   = "QueuingMachine";
            string formatName = (format == AudioFormat.Silk) ? "ssml-16khz-16bit-mono-silk" : "riff-16khz-16bit-mono-pcm";

            webRequest.Headers.Add("X-MICROSOFT-OutputFormat", formatName);
            webRequest.Headers.Add("X-Search-AppId", "07D3234E49CE426DAA29772419F436CA");
            webRequest.Headers.Add("X-Search-ClientID", "1ECFAE91408841A480F00935DC390960");

            webRequest.Headers.Add("Authorization", "Bearer " + token.access_token);
            webRequest.Method = "POST";

            string bodyTemplate   = "<speak version=\"1.0\" xmlns=\"http://www.w3.org/2001/10/synthesis\" xmlns:mstts=\"http://www.w3.org/2001/mstts\" xmlns:emo=\"http://www.w3.org/2009/10/emotionml\" xml:lang=\"{0}\">{1}<emo:emotion><emo:category name=\"CALM\" value=\"1.0\"/><prosody rate=\"{2:F1}\">{3}</prosody></emo:emotion></voice></speak>";
            string voiceTag       = "<voice name=\"" + voiceName + "\">";
            string deviceLanguage = lang;
            string encodedXml     = text.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace("\"", "&quot;").Replace("'", "&apos;");

            if (prosodyRate < 0.1f)
            {
                prosodyRate = 0.1f;
            }
            else if (prosodyRate > 2.0f)
            {
                prosodyRate = 2.0f;
            }

            string body = string.Format(bodyTemplate, deviceLanguage, voiceTag, prosodyRate, encodedXml);

            byte[] bytes = Encoding.UTF8.GetBytes(body);
            webRequest.ContentLength = bytes.Length;
            using (Stream outputStream = webRequest.GetRequestStream())
            {
                outputStream.Write(bytes, 0, bytes.Length);
            }

            WebResponse webResponse = webRequest.GetResponse();

            using (Stream stream = webResponse.GetResponseStream())
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    int count = 0;
                    do
                    {
                        byte[] buf = new byte[1024];
                        count = stream.Read(buf, 0, 1024);
                        ms.Write(buf, 0, count);
                    } while (stream.CanRead && count > 0);
                    output = ms.ToArray();
                }
            }
            return(output);
        }
        public Authentication(string clientId, string clientSecret)
        {
            this.clientId = clientId;
            this.clientSecret = clientSecret;

            // If clientid or client secret has special characters, encode before sending request 
            this.requestBody = 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"));

            this.token = PostHttp(AccessUri, this.requestBody);

            // renew the token every specfied minutes
            accessTokenRenewer = new Timer(new TimerCallback(OnTokenExpiredCallback),
                                           this,
                                           TimeSpan.FromMinutes(RefreshTokenDuration),
                                           TimeSpan.FromMilliseconds(-1));
        }
 private void RenewAccessToken()
 {
     AccessTokenInfo newAccessToken = PostHttp(AccessUri, this.requestBody);
     //swap the new token with old one
     //Note: the swap is thread unsafe
     this.token = newAccessToken;
     Console.WriteLine(string.Format("Renewed token for user: {0} is: {1}",
                       this.clientId,
                       this.token.access_token));
 }