Azure tranlate API调用正在返回并返回40100
我使用此HTTP Get请求获取翻译的承载令牌:Azure tranlate API调用正在返回并返回40100,azure,microsoft-cognitive,Azure,Microsoft Cognitive,我使用此HTTP Get请求获取翻译的承载令牌: https://api.cognitive.microsoft.com/sts/v1.0/issueToken?Subscription-Key=1fo8xxx 使用返回的承载器,我希望使用此API端点翻译短文本: https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&to=de 我在标题中写下: Content-Type: applicatio
https://api.cognitive.microsoft.com/sts/v1.0/issueToken?Subscription-Key=1fo8xxx
使用返回的承载器,我希望使用此API端点翻译短文本:
https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&to=de
我在标题中写下:
Content-Type: application/json; charset=UTF-8.
[
{"Text":"I would really like to drive your car around the block a few times."}
]
我在身体里放了这个:
Content-Type: application/json; charset=UTF-8.
[
{"Text":"I would really like to drive your car around the block a few times."}
]
我使用的是邮递员,因此在授权选项卡中,我选择了持票人并将其插入旁边的字段:
Bearer <result from the first API call>
您的请求需要“OCP Apim订阅密钥”标题。看看官方的例子:
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
// Install Newtonsoft.Json with NuGet
using Newtonsoft.Json;
/// <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; }
}
private const string key_var = "TRANSLATOR_TEXT_SUBSCRIPTION_KEY";
private static readonly string subscriptionKey = Environment.GetEnvironmentVariable(key_var);
private const string endpoint_var = "TRANSLATOR_TEXT_ENDPOINT";
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);
}
using (var client = new HttpClient())
using (var request = new HttpRequestMessage())
{
// Build the request.
// Set the method to Post.
request.Method = HttpMethod.Post;
// Construct the URI and add headers.
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();
// Deserialize the response using the classes created earlier.
TranslationResult[] deserializedOutput = JsonConvert.DeserializeObject<TranslationResult[]>(result);
// Iterate over the deserialized results.
foreach (TranslationResult o in deserializedOutput)
{
// Print the detected input language 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);
}
}
}
Console.Read();
}
使用系统;
使用System.Net.Http;
使用系统文本;
使用System.Threading.Tasks;
//使用NuGet安装Newtonsoft.Json
使用Newtonsoft.Json;
///
///表示翻译文本API返回的JSON的C#类。
///
公共类TranslationResult
{
公共检测语言检测语言{get;set;}
公共文本结果源文本{get;set;}
公共翻译[]翻译{get;set;}
}
公共类检测语言
{
公共字符串语言{get;set;}
公共浮动分数{get;set;}
}
公共类文本结果
{
公共字符串文本{get;set;}
公共字符串脚本{get;set;}
}
公共课翻译
{
公共字符串文本{get;set;}
公共文本结果音译{get;set;}
{get;set;}的公共字符串
公共对齐{get;set;}
公共句子长度SentLen{get;set;}
}
公共阶级联盟
{
公共字符串Proj{get;set;}
}
公共类句子长度
{
public int[]SrcSentLen{get;set;}
公共int[]TransSentLen{get;set;}
}
private const string key\u var=“TRANSLATOR\u TEXT\u SUBSCRIPTION\u key”;
私有静态只读字符串subscriptionKey=Environment.GetEnvironmentVariable(key\u var);
私有常量字符串端点\u var=“TRANSLATOR\u TEXT\u endpoint”;
私有静态只读字符串endpoint=Environment.GetEnvironmentVariable(endpoint_var);
静态程序()
{
if(null==subscriptionKey)
{
抛出新异常(“请设置/导出环境变量:“+key\u var”);
}
if(null==端点)
{
抛出新异常(“请设置/导出环境变量:”+endpoint_var);
}
使用(var client=new HttpClient())
使用(var request=new HttpRequestMessage())
{
//生成请求。
//将方法设置为Post。
request.Method=HttpMethod.Post;
//构造URI并添加头。
request.RequestUri=新Uri(端点+路由);
request.Content=newstringcontent(requestBody,Encoding.UTF8,“application/json”);
添加(“Ocp Apim订阅密钥”,subscriptionKey);
//发送请求并获得响应。
HttpResponseMessage response=await client.SendAsync(请求).ConfigureAwait(错误);
//将响应读取为字符串。
字符串结果=wait response.Content.ReadAsStringAsync();
//使用前面创建的类反序列化响应。
TranslationResult[]反序列化输出=JsonConvert.DeserializeObject(结果);
//迭代反序列化的结果。
foreach(反序列化输出中的TranslationResult o)
{
//打印检测到的输入语言和置信度分数。
WriteLine(“检测到的输入语言:{0}\n可信度分数:{1}\n”,o.DetectedLanguage.language,o.DetectedLanguage.score);
//迭代结果并打印每个翻译。
foreach(o.Translations中的t翻译)
{
WriteLine(“翻译成{0}:{1}”,t.to,t.Text);
}
}
}
Console.Read();
}
您的请求需要“OCP Apim订阅密钥”标题。看看官方的例子:
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
// Install Newtonsoft.Json with NuGet
using Newtonsoft.Json;
/// <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; }
}
private const string key_var = "TRANSLATOR_TEXT_SUBSCRIPTION_KEY";
private static readonly string subscriptionKey = Environment.GetEnvironmentVariable(key_var);
private const string endpoint_var = "TRANSLATOR_TEXT_ENDPOINT";
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);
}
using (var client = new HttpClient())
using (var request = new HttpRequestMessage())
{
// Build the request.
// Set the method to Post.
request.Method = HttpMethod.Post;
// Construct the URI and add headers.
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();
// Deserialize the response using the classes created earlier.
TranslationResult[] deserializedOutput = JsonConvert.DeserializeObject<TranslationResult[]>(result);
// Iterate over the deserialized results.
foreach (TranslationResult o in deserializedOutput)
{
// Print the detected input language 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);
}
}
}
Console.Read();
}
使用系统;
使用System.Net.Http;
使用系统文本;
使用System.Threading.Tasks;
//使用NuGet安装Newtonsoft.Json
使用Newtonsoft.Json;
///
///表示翻译文本API返回的JSON的C#类。
///
公共类TranslationResult
{
公共检测语言检测语言{get;set;}
公共文本结果源文本{get;set;}
公共翻译[]翻译{get;set;}
}
公共类检测语言
{
公共字符串语言{get;set;}
公共浮动分数{get;set;}
}
公共类文本结果
{
公共字符串文本{get;set;}
公共字符串脚本{get;set;}
}
公共课翻译
{
公共字符串文本{get;set;}
公共文本结果音译{get;set;}
{get;set;}的公共字符串
公共对齐{get;set;}
公共句子长度SentLen{get;set;}
}
公共阶级联盟
{
公共字符串Proj{get;set;}
}
公共类句子长度
{
public int[]SrcSentLen{get;set;}
公共int[]TransSentLen{get;set;}
}
private const string key\u var=“TRANSLATOR\u TEXT\u SUBSCRIPTION\u key”;
私有静态只读字符串subscriptionKey=Environment.GetEnvironmentVariable(key\u var);
私有常量字符串端点\u var=“TRANSLATOR\u TEXT\u endpoint”;
私有静态只读字符串endpoint=Environment.GetEnvironmentVariable(endpoint_var);
静态程序()
{
if(null==subscriptionKey)
{
抛出新异常(“请设置/导出环境变量:“+key\u var”);
}
if(null==端点)
{
抛出新异常(“请设置/导出环境变量:”+endpoint_var);
}
使用(var client=new HttpClient())
使用(var request=new HttpRequestMessage())
{
//生成请求。
//将方法设置为Post。
request.Method=HttpMethod.Post;
//构造URI并添加头。
request.RequestUri=新Uri(端点+路由);
request.Content=newstringcontent(requestBody,Encoding.UTF8,“application/json”);
添加(“Ocp Apim订阅密钥”,subscriptionKey);
//发送请求并获得响应。
HttpRespon