我将MS Translator API实现为C#控制台应用程序。 我的订阅级别是高级付费帐户,而不是免费级别。 每次我开始调用它,前1-5个翻译工作正常。 在此之后,我得到了无休止的400(不良请求)例外。
以下是我收到的文字回复:
响应文本:TranslateApiException方法:Translate()消息: 找不到有效的Azure Market Place转换程序订阅 与请求credentials.message相关联 ID = 3832.V2_Rest.Translate.117038D9
我错过了什么?我肯定在代码中包含应用程序ID和密钥?
我是否打算提供一些额外的凭据?
这是我的翻译班: 有什么想法吗?
// --------------------------------------------------------------------
public class Translator
{
private string AccessToken;
private DateTime TokenExpirationDate;
// ----------------------------------------------------------------
public Translator()
{
AccessToken = "";
TokenExpirationDate = new DateTime(2000, 1, 1);
}
// --------------------------------------------------------------
public void GetAccessToken()
{
if (AccessToken != "" && DateTime.Now < TokenExpirationDate)
{
Console.WriteLine("Translator: usng existing token");
return;
}
AccessToken = "";
string clientID = "<-removed->";
string clientSecret = "<-also-removed->";
String strTranslatorAccessURI = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13";
String strRequestDetails =
string.Format("grant_type=client_credentials&client_id={0}&client_secret={1} &scope=http://api.microsofttranslator.com", HttpUtility.UrlEncode(clientID),
HttpUtility.UrlEncode(clientSecret));
System.Net.WebRequest webRequest = System.Net.WebRequest.Create(strTranslatorAccessURI);
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "POST";
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(strRequestDetails);
webRequest.ContentLength = bytes.Length;
using (System.IO.Stream outputStream = webRequest.GetRequestStream())
{
outputStream.Write(bytes, 0, bytes.Length);
}
WebResponse webResponse = null;
try
{
webResponse = webRequest.GetResponse();
}
catch (Exception ex)
{
AccessToken = "";
Console.WriteLine("Exception: " + ex.Message);
}
if (webResponse != null)
{
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(AdmAccessToken));
AdmAccessToken token = (AdmAccessToken)serializer.ReadObject(webResponse.GetResponseStream());
AccessToken = token.access_token;
TokenExpirationDate = DateTime.Now.AddSeconds(Convert.ToDouble(token.expires_in));
if (AccessToken.Length > 0) Console.WriteLine("Translator: got an access token.");
}
}
// -------------------------------------------------------------------
public string Translate(string textToTranslate, string destLanguageCode)
{
Console.WriteLine("Translator(" + destLanguageCode + "):" + textToTranslate);
string uri = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text=" + System.Web.HttpUtility.UrlEncode(textToTranslate) + "&from=en&to=" + destLanguageCode;
System.Net.WebRequest translationWebRequest = System.Net.WebRequest.Create(uri);
translationWebRequest.Headers.Add("Authorization", "Bearer " + AccessToken);
System.Net.WebResponse response = null;
try
{
response = translationWebRequest.GetResponse();
}
catch (Exception ex)
{
Console.WriteLine("Translator: Fail: " + ex.Message);
Console.WriteLine("Exception: " + ex.Message);
}
if (response != null)
{
System.IO.Stream stream = response.GetResponseStream();
System.Text.Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
System.IO.StreamReader translatedStream = new System.IO.StreamReader(stream, encode);
System.Xml.XmlDocument xTranslation = new System.Xml.XmlDocument();
xTranslation.LoadXml(translatedStream.ReadToEnd());
Console.WriteLine("Translator(" + destLanguageCode + "):" + xTranslation.InnerText);
return xTranslation.InnerText;
}
return "";
}
}
// ------------------------------------------------------------------------
public class AdmAccessToken
{
public string access_token { get; set; }
public string token_type { get; set; }
public string expires_in { get; set; }
public string scope { get; set; }
}
}
答案 0 :(得分:1)
查看回复的内容。它将包含人类可读形式的错误原因。 您可能没有与请求凭据相关联的订阅。解决这个问题: