Exemple #1
0
        /// <summary>
        /// Retrieves an oAuth authentication token to be used on the translate
        /// API request. The result string needs to be passed as a bearer token
        /// to the translate API.
        ///
        /// You can find client ID and Secret (or register a new one) at:
        /// https://datamarket.azure.com/developer/applications/
        /// </summary>
        /// <param name="apiKey">The client ID of your application</param>
        /// <returns></returns>
        public string GetBingAuthToken(string apiKey = null, string ignored = null)
        {
            string authBaseUrl = "https://api.cognitive.microsoft.com/sts/v1.0/issueToken";

            if (string.IsNullOrEmpty(apiKey))
            {
                apiKey = DbResourceConfiguration.Current.BingClientId;
            }

            if (string.IsNullOrEmpty(apiKey))
            {
                ErrorMessage = "Subscription key must be provided";
                return(null);
            }
            // POST Auth data to the oauth API
            string res;

            try
            {
                var web = new HttpUtilsWebClient(new HttpRequestSettings()
                {
                    HttpVerb = "POST"
                });
                web.Encoding = Encoding.UTF8;
                web.Headers.Add("Ocp-Apim-Subscription-Key", apiKey);
                res = web.UploadString(authBaseUrl, "");
            }
            catch (Exception ex)
            {
                ErrorMessage = ex.GetBaseException().Message;
                return(null);
            }
            return(res);
        }
        private void SaveMarkdownImages(string htmlText, string basePath)
        {
            try
            {
                var doc = new HtmlDocument();
                doc.LoadHtml(htmlText);

                // send up normalized path images as separate media items
                var images = doc.DocumentNode.SelectNodes("//img");
                if (images != null)
                {
                    foreach (HtmlNode img in images)
                    {
                        string imgFile = img.Attributes["src"]?.Value;
                        if (imgFile == null)
                        {
                            continue;
                        }

                        if (imgFile.StartsWith("http://") || imgFile.StartsWith("https://"))
                        {
                            string imageDownloadPath = Path.Combine(basePath, Path.GetFileName(imgFile));

                            try
                            {
                                var http = new HttpUtilsWebClient();
                                http.DownloadFile(imgFile, imageDownloadPath);
                            }
                            catch // just continue on errorrs
                            { }
                        }
                    }
                }
            }
            catch // catch so thread doesn't crash
            {
            }
        }