0

I've seen threads on this issue but my problem is particularly confusing. I have a free 2 million character subscription, a valid client id and secret. When I run my code I get to call the API a few times successfully (the most I've seen is 75 consecutive successful calls). Then every other call returns a Bad request response: The remote server returned an error: (400) Bad Request.

I create the token once with my credentials and never create it again. I loop through a file, parse it, and submit every parsed string for translation by calling the API. It seems that I reach some sort of limit that I'm now aware of.

When looking at my account, it doesn't seem to be discounting the characters that I've translated already which would make me highly suspicious that I have the wrong credentials when creating the token. I quadruple-checked that and everything seems to be ok.

Any guidance on what I may be missing here would be much appreciated.

Here's the code that creates the token. I do think though that there may be an unknown limitation that I'm not aware of with the free subscription.

static void gettoken()
    {
        //Get access token
        string clientID = "my client id";
        string clientSecret = "my secret";
        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", clientID, 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);
        }
        System.Net.WebResponse webResponse = webRequest.GetResponse();

        System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(AdmAccessToken));
        AdmAccessToken token = (AdmAccessToken)serializer.ReadObject(webResponse.GetResponseStream());

        MyGlobals.headerValue = "Bearer " + token.access_token;
    }

And here's the code that calls the API itself. I call the API method from a loop.

static void RunBing(string sterm)
    {
        //Submit the translation request
        string txtToTranslate = sterm;
        string uri = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text=" + txtToTranslate + "&from=en&to=es";
        System.Net.WebRequest translationWebRequest = System.Net.WebRequest.Create(uri);
        translationWebRequest.Headers.Add("Authorization", MyGlobals.headerValue);
        System.Net.WebResponse response = null;
        try {
            response = translationWebRequest.GetResponse();
        }
        catch (Exception e)
        {
            Console.WriteLine("Term failed: " + sterm);
            Console.WriteLine(e);
            return;
        }
        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());
        MyGlobals.xlation = xTranslation.InnerText;
    } 

After several successful calls to the API, I start to get the following message: System.Net.WebException: The remote server returned an error: (400) Bad Request. at System.Net.HttpWebRequest.GetResponse() at Translate.TranslateText.Program.RunBing(String sterm)

Paulo Hgo
  • 834
  • 1
  • 11
  • 26
  • I see 2 pieces of documentation, one older one ( https://social.msdn.microsoft.com/Forums/en-US/d837a761-eca6-4e86-979c-ff24e2ec3397/what-are-the-limitations-of-the-language-translator-api?forum=microsofttranslator ,which may or may not be true today), "there is a cap of 50 calls per minute per originating IP" and this one, which is current documentation (https://msdn.microsoft.com/en-us/library/ff512387.aspx), "This Token is valid for 10 minutes", even if they give you a 400, you should be able to catch the request and examine the returned error. – starlight54 Aug 16 '16 at 00:31
  • Thanks. I am catching the exception in the example above but all I get from it is that it's considered a bad request. It doesn't say why and given that it works several times before failing I think it may be some sort of limitation but I can't find it anywhere... – Paulo Hgo Aug 16 '16 at 04:34
  • That's a GET request. Can't you use a POST request? I guess there could be some character in the text which requires escaping (`UrlEncode` method). – Bernhard Hiller Aug 16 '16 at 08:54

0 Answers0