locked
Microsoft Translator API RRS feed

  • Question

  • Hello ....,

    I'm currently developing a Windows Phone 8 App using Bing Translator API and I got the source code from an official Bing developer page

    I've copied the code as it is and only modified the "from" and "to" ( ex: from English to French ) , I activated my API from Azure Marketplace which allows 2,000,000 characters/month 

    and While testing my code I frequently get an error like this

    An exception of type 'System.Net.WebException' occurred in System.Windows.ni.dll but was not handled in user code

    Like this

    I've searched everywhere and I can't find a solution for it , I tried using a different access token ( client id and client secret ) and I'm still getting this error


    Mohamed H. Sa'ead


    Tuesday, August 6, 2013 3:01 AM

Answers

  • this is it

    the unauthorized is from your code trying to access the ResultTextBox from another thread.  you are getting unauthorizedAccessException "Invalid cross-thread access" from the line

                    Deployment.Current.Dispatcher.BeginInvoke(() => TranslatedText = strTest);
                    ResultsTextBox.Text = "Translating(" + FromLanguageCode + ") :" + Environment.NewLine + strTextToTranslate +
                                     Environment.NewLine + "To (" + ToLanguageCode + ") :" + Environment.NewLine +
                                     TranslatedText;

    if you change that to wrap it inside the Dispatcher thread, then it will be working as expected.

     		Deployment.Current.Dispatcher.BeginInvoke(() =>
                        {
                            TranslatedText = strTest;
                            ResultsTextBox.Text = "Translating(" + FromLanguageCode + ") :" + Environment.NewLine + strTextToTranslate +
                                     Environment.NewLine + "To (" + ToLanguageCode + ") :" + Environment.NewLine +
                                     TranslatedText;
                        });

    I see that you did that in your original code (in the first post with the screen shot), so maybe this isn't the issue? 
    Wednesday, September 4, 2013 9:36 PM

All replies

  • When you say "frequently", do you mean that it is an intermittent error (ie sometimes it works, sometimes it doesn't?)
    • Proposed as answer by Vikram Dendi Tuesday, August 13, 2013 9:32 PM
    • Unproposed as answer by MohamedHesham Thursday, August 22, 2013 12:24 AM
    Tuesday, August 13, 2013 8:48 PM
  • yep , but 80% it doesn't work

    Mohamed H. Sa'ead

    Tuesday, August 13, 2013 9:36 PM
  • this seems to suggest an encoding issue with the token.

    more info would be in here:
    http://social.msdn.microsoft.com/Forums/en-US/bb624bc7-1985-4a38-92bb-526be2aeab8f/about-appid-and-access-token

    Also, can you point me to the specific link to the sample code that you copy from?  want to make sure its accuracy.

    thanks

    Tuesday, August 20, 2013 8:05 PM
  • Yeah sure here's the sample code : http://blogs.msdn.com/b/translation/p/windowsphone8.aspx

    and I don't think it's about encoding the token :)

    Thanks in advance Alex


    Mohamed H. Sa'ead

    Thursday, August 22, 2013 12:23 AM
  • Alex, are you there?

    Mohamed H. Sa'ead

    Thursday, August 29, 2013 2:05 AM
  • can you paste the query string that you actually use to send the translate request?

    And also the authToken that you are adding to the header?

    I'm not sure what could be wrong, the sample does look right.

    Thursday, August 29, 2013 10:12 PM
  • this is inside the requeststreamready

    string client_ID = "RegisteredBot";
                string client_Secret = "iQYOAXlWVT3ZJhrEH6NNdx3l0lMS/k+nqAxsDLgjQ4Y=";
                string strRequestDetails = string.Format("grant_type=client_credentials&client_id={0}&client_secret={1}&scope=http://api.microsofttranslator.com", client_ID, HttpUtility.UrlEncode(client_Secret));

    and this is the query 

    string uri = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text=" + System.Net.HttpUtility.UrlEncode(strTextToTranslate) + "&from=" + FromLanguageCode + "&to=" + ToLanguageCode;
                    System.Net.WebRequest translationWebRequest = System.Net.HttpWebRequest.Create(uri);

    and the error happens here at the translationready function

    HttpWebRequest request = (HttpWebRequest)ar.AsyncState;


    Mohamed H. Sa'ead

    Friday, August 30, 2013 1:37 PM
  • can you try a console app with the same code?  I can't repro this at all.  I put all the sample code as a console app and run it a bunch of times using your credential to see if i can repro the issue.

    when your app crash, is the token.access_token null? or does it have value?

    I'm sorry i couldn't be more help, but the sample code looks right to me.

    Friday, August 30, 2013 11:38 PM
  • If you can kindly check the solution/project it's gathered here

    http://sdrv.ms/16ZklbN

    I think the console works ( a friend of mine tried it ) , I'm wondering why it's not working on Windows Phone 8

    Thanks in advance


    Mohamed H. Sa'ead

    Saturday, August 31, 2013 8:24 PM
  • i can confirm the repro on the win 8 emulator...now i'm trying to figure out why.  stay tuned.
    Wednesday, September 4, 2013 9:07 PM
  • this is it

    the unauthorized is from your code trying to access the ResultTextBox from another thread.  you are getting unauthorizedAccessException "Invalid cross-thread access" from the line

                    Deployment.Current.Dispatcher.BeginInvoke(() => TranslatedText = strTest);
                    ResultsTextBox.Text = "Translating(" + FromLanguageCode + ") :" + Environment.NewLine + strTextToTranslate +
                                     Environment.NewLine + "To (" + ToLanguageCode + ") :" + Environment.NewLine +
                                     TranslatedText;

    if you change that to wrap it inside the Dispatcher thread, then it will be working as expected.

     		Deployment.Current.Dispatcher.BeginInvoke(() =>
                        {
                            TranslatedText = strTest;
                            ResultsTextBox.Text = "Translating(" + FromLanguageCode + ") :" + Environment.NewLine + strTextToTranslate +
                                     Environment.NewLine + "To (" + ToLanguageCode + ") :" + Environment.NewLine +
                                     TranslatedText;
                        });

    I see that you did that in your original code (in the first post with the screen shot), so maybe this isn't the issue? 
    Wednesday, September 4, 2013 9:36 PM
  • Thanks Alex a lot for your help , I used another code that fixed the error and I got it working finally but you've helped me a lot really and I hope I can return this favor 

    Here's a vote and a mark as answer for your efforts , I'm really grateful for you :)


    Mohamed H. Sa'ead

    Thursday, September 5, 2013 12:20 AM