2016-06-28 89 views
1

我將MS Translator API實施爲C#控制檯應用程序。 我的訂閱級別是付費帳戶,而不是免費級別。 每次我開始打電話時,第1-5個翻譯都能正常工作。 之後,我得到和無盡的400(壞請求)例外流。Microsoft Translator無法連續返回400

這裏是文本響應我得到:

響應文本:TranslateApiException方法:翻譯()消息: 無法找到與請求credentials.message 關聯的活動Azure的市場廣場翻譯認購 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; } 
} 

}

+1

沒有看代碼 - 前幾個請求正在運行*的事實表明*您可能會受到速率限制。你多久做一次這些請求?你是否遵守API規定的條款和條件? – user1666620

+0

400 - 錯誤的請求也可能意味着您的請求格式不正確。您是否嘗試過在提琴手中觀看該請求以查看是否有問題? – Jake

+0

適用於4個或5個請求。然後不間斷的400。 –

回答

1

看那響應的內容。它將以可讀的形式包含錯誤的原因。 您很可能沒有與您的請求憑據相關聯的訂閱。解決方法:

+0

我最初的帖子很清楚地表明我正在爲一個帳戶付款,並且已經在應用程序中擁有客戶端ID和密鑰。 –

+0

確保訂閱了「Microsoft Translator - Text Translation」。 –