如何使用带有GetLanguageNames的Text Translation API获取语言名称

时间:2017-02-13 12:41:47

标签: c# dotnet-httpclient microsoft-cognitive

我无法使用Text Translation API获取语言名称。我用这种方法获得了可用的语言:

public async Task<List<string>> GetLanguagesForTranslateAsync()
{
    string requestUri = "https://api.microsofttranslator.com/v2/http.svc/GetLanguagesForTranslate";
    string accessToken = await this.auth.GetTokenAsync();
    string response = await this.GetAsync(requestUri, accessToken);
    XDocument xml = XDocument.Parse(response);
    List<string> languages = new List<string>();
    foreach (XElement element in xml.Root.Descendants())
    {
        languages.Add(element.Value);
    }
    return languages;
}

private async Task<string> GetAsync(string url, string bearerToken)
{
    using (HttpClient httpClient = new HttpClient())
    {
        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", bearerToken);
        HttpResponseMessage response = await httpClient.GetAsync(url);
        return await response.Content.ReadAsStringAsync();
    }
}

我在这种情况下得到一个带有“en”,“es”,“fr”或“zh-CHT”等语言代码的XML,但我想得到“英语”,“西班牙语”,“法语”或“中国传统的)”。看the documentation我尝试了这种方法:

public async Task<List<string>> GetLanguageNamesAsync(string locale, List<string> languageCodes)
{
    string requestUri = "https://api.microsofttranslator.com/v2/http.svc/GetLanguageNames";
    string content = $"locale=en&languageCodes=en,es,fr,zh-CHT";
    string accessToken = await this.auth.GetTokenAsync();
    string response = await this.PostAsync(requestUri, accessToken, content);
    XDocument xml = XDocument.Parse(response);
    List<string> languages = new List<string>();
    foreach (XElement element in xml.Root.Descendants())
    {
        languages.Add(element.Value);
    }
    return languages;
}

private async Task<string> PostAsync(string url, string bearerToken, string content)
{
    using (HttpClient httpClient = new HttpClient())
    {
        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", bearerToken);
        HttpResponseMessage response = await httpClient.PostAsync(url, new StringContent(content));
        return await response.Content.ReadAsStringAsync();
    }
}

我对此请求的回复:

请求错误

错误状态代码:'InternalServerError'

详细信息:服务器在处理请求时遇到错误。有关更多详细信息,请参阅服务器日志。

真正的XML响应:

<html version="-//W3C//DTD XHTML 2.0//EN"
      xml:lang="en"
      xsi:schemaLocation="http://www.w3.org/1999/xhtml http://www.w3.org/MarkUp/SCHEMA/xhtml2.xsd"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <HEAD>
    <TITLE>Request Error</TITLE>
  </HEAD>
  <BODY>
    <DIV id="content">
      <P class="heading1"><B>Error Status Code:</B> 'InternalServerError'</P>
      <P><B>Details: </B>The server encountered an error processing the request. Please see the server logs for more details.</P>
      <!-- Padding xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-->
    </DIV>
  </BODY>
</html>

你知道发生了什么吗?还有什么我需要能够提出请求吗?

1 个答案:

答案 0 :(得分:2)

确保您可以卷曲GetLanguages。以下适用于我。

curl -k --data "" "https://api.cognitive.microsoft.com/sts/v1.0/issueToken" -H "Ocp-Apim-Subscription-Key:<your Translation Text Key>" > Bearer.txt
set /p Bearer= < Bearer.txt
curl -H "Authorization: Bearer %Bearer%" "https://api.microsofttranslator.com/v1/http.svc/GetLanguageNames"
curl -H "Authorization: Bearer %Bearer%" "https://api.microsofttranslator.com/v2/http.svc/GetLanguagesForTranslate"
curl -H "Authorization: Bearer %Bearer%" "https://api.microsofttranslator.com/v2/Http.svc/Translate?Text=Hello%%20world.&From=en&To=es"