首页 > 解决方案 > Microsoft Azure 文本翻译器 - 订阅密钥不起作用

问题描述

我正在尝试使用 Microsoft Azure 的文本翻译器在控制台中构建一个基本的文本翻译器。但是,我的订阅密钥根本不起作用。我已经多次生成它们,并手动输入它们。请协助。我已经留下了钥匙以供进一步澄清。感谢您的阅读,感谢您的帮助。

代码生成此错误:

if (null == subscriptionKey) { throw new Exception("请设置/导出环境变量:" + key_var);

 // This sample requires C# 7.1 or later for async/await.

    using System;
    using System.Collections.Generic;
    using System.Net.Http;
    using System.Text;
    using System.Threading.Tasks;
    // Install Newtonsoft.Json with NuGet
    using Newtonsoft.Json;

    namespace TranslateTextSample
    {
        /// <summary>
        /// The C# classes that represents the JSON returned by the Translator Text API.
        /// </summary>
        public class TranslationResult
        {
            public DetectedLanguage DetectedLanguage { get; set; }
            public TextResult SourceText { get; set; }
            public Translation[] Translations { get; set; }
        }

        public class DetectedLanguage
        {
            public string Language { get; set; }
            public float Score { get; set; }
        }

        public class TextResult
        {
            public string Text { get; set; }
            public string Script { get; set; }
        }

        public class Translation
        {
            public string Text { get; set; }
            public TextResult Transliteration { get; set; }
            public string To { get; set; }
            public Alignment Alignment { get; set; }
            public SentenceLength SentLen { get; set; }
        }

        public class Alignment
        {
            public string Proj { get; set; }
        }

        public class SentenceLength
        {
            public int[] SrcSentLen { get; set; }
            public int[] TransSentLen { get; set; }
        }

        class Program
        {
            private const string key_var="b1f43a68dce24b0280360691ad68bc75";
            private static readonly string subscriptionKey = Environment.GetEnvironmentVariable(key_var);

            private const string endpoint_var = "https://consoletexttranslator.cognitiveservices.azure.com/sts/v1.0/issuetoken";
            private static readonly string endpoint = Environment.GetEnvironmentVariable(endpoint_var);

            static Program()
            {
                if (null == subscriptionKey)
                {
                    throw new Exception("Please set/export the environment variable: " + key_var);
                }
                if (null == endpoint)
                {
                    throw new Exception("Please set/export the environment variable: " + endpoint_var);
                }
            }

            // Async call to the Translator Text API
            static public async Task TranslateTextRequest(string subscriptionKey, string endpoint, string route, string inputText)
            {
                object[] body = new object[] { new { Text = inputText } };
                var requestBody = JsonConvert.SerializeObject(body);

                using (var client = new HttpClient())
                using (var request = new HttpRequestMessage())
                {
                    // Build the request.
                    request.Method = HttpMethod.Post;
                    request.RequestUri = new Uri(endpoint + route);
                    request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
                    request.Headers.Add("Ocp-Apim-Subscription-Key", subscriptionKey);

                    // Send the request and get response.
                    HttpResponseMessage response = await client.SendAsync(request).ConfigureAwait(false);
                    // Read response as a string.
                    string result = await response.Content.ReadAsStringAsync();
                    TranslationResult[] deserializedOutput = JsonConvert.DeserializeObject<TranslationResult[]>(result);
                    // Iterate over the deserialized results.
                    foreach (TranslationResult o in deserializedOutput)
                    {
                        // Print the detected input languge and confidence score.
                        Console.WriteLine("Detected input language: {0}\nConfidence score: {1}\n", o.DetectedLanguage.Language, o.DetectedLanguage.Score);
                        // Iterate over the results and print each translation.
                        foreach (Translation t in o.Translations)
                        {
                            Console.WriteLine("Translated to {0}: {1}", t.To, t.Text);
                        }
                    }
                }
            }

            static async Task Main(string[] args)
            {
                // This is our main function.
                // Output languages are defined in the route.
                // For a complete list of options, see API reference.
                // https://docs.microsoft.com/azure/cognitive-services/translator/reference/v3-0-translate
                string route = "/translate?api-version=3.0&to=de&to=it&to=ja&to=th";
                // Prompts you for text to translate. If you'd prefer, you can
                // provide a string as textToTranslate.
                Console.Write("Type the phrase you'd like to translate? ");
                string textToTranslate = "Hello, Tommy.";
               await TranslateTextRequest(subscriptionKey, endpoint, route, textToTranslate);
                Console.WriteLine("Press any key to continue.");
                Console.ReadKey();
            }
        }
    }

标签: c#azuremicrosoft-cognitive

解决方案


请尝试下面的代码,它对我有用:

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
// Install Newtonsoft.Json with NuGet
using Newtonsoft.Json;

namespace TranslateTextSample
{
    /// <summary>
    /// The C# classes that represents the JSON returned by the Translator Text API.
    /// </summary>
    public class TranslationResult
    {
        public DetectedLanguage DetectedLanguage { get; set; }
        public TextResult SourceText { get; set; }
        public Translation[] Translations { get; set; }
    }

    public class DetectedLanguage
    {
        public string Language { get; set; }
        public float Score { get; set; }
    }

    public class TextResult
    {
        public string Text { get; set; }
        public string Script { get; set; }
    }

    public class Translation
    {
        public string Text { get; set; }
        public TextResult Transliteration { get; set; }
        public string To { get; set; }
        public Alignment Alignment { get; set; }
        public SentenceLength SentLen { get; set; }
    }

    public class Alignment
    {
        public string Proj { get; set; }
    }

    public class SentenceLength
    {
        public int[] SrcSentLen { get; set; }
        public int[] TransSentLen { get; set; }
    }

    class Program
    {
        private const string subscriptionKey = "<your translator API key>";

        private const string endpoint = "https://api.cognitive.microsofttranslator.com";


        static Program()
        {
            if (null == subscriptionKey)
            {
                throw new Exception("Please set/export the environment variable: " + subscriptionKey);
            }
            if (null == endpoint)
            {
                throw new Exception("Please set/export the environment variable: " + endpoint);
            }
        }

        // Async call to the Translator Text API
        static public async Task TranslateTextRequest(string subscriptionKey, string endpoint, string route, string inputText)
        {
            object[] body = new object[] { new { Text = inputText } };
            var requestBody = JsonConvert.SerializeObject(body);

            using (var client = new HttpClient())
            using (var request = new HttpRequestMessage())
            {
                // Build the request.
                request.Method = HttpMethod.Post;
                request.RequestUri = new Uri(endpoint + route);
                request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
                request.Headers.Add("Ocp-Apim-Subscription-Key", subscriptionKey);

                // Send the request and get response.
                HttpResponseMessage response = await client.SendAsync(request).ConfigureAwait(false);
                // Read response as a string.
                string result = await response.Content.ReadAsStringAsync();
                TranslationResult[] deserializedOutput = JsonConvert.DeserializeObject<TranslationResult[]>(result);
                // Iterate over the deserialized results.
                foreach (TranslationResult o in deserializedOutput)
                {
                    // Print the detected input languge and confidence score.
                    Console.WriteLine("Detected input language: {0}\nConfidence score: {1}\n", o.DetectedLanguage.Language, o.DetectedLanguage.Score);
                    // Iterate over the results and print each translation.
                    foreach (Translation t in o.Translations)
                    {
                        Console.WriteLine("Translated to {0}: {1}", t.To, t.Text);
                    }
                }
            }
        }


        static void Main(string[] args)
        {
            MainAsync(args).GetAwaiter().GetResult();
            Console.ReadKey();
            Console.WriteLine("press anykey to exit");
        }

        static async Task MainAsync(string[] args)
        {

             string route = "/translate?api-version=3.0&to=de&to=it&to=ja&to=th";
            // Prompts you for text to translate. If you'd prefer, you can
            // provide a string as textToTranslate.

            string textToTranslate = "Hello, Tommy.";
            await TranslateTextRequest(subscriptionKey, endpoint, route, textToTranslate);

        }



    }
}

在运行此控制台应用程序之前,请在此处将“subscriptionKey”的值替换为您自己的键值: 在此处输入图像描述

结果 :

在此处输入图像描述

如果有任何不清楚的地方,请随时告诉我:)


推荐阅读