none
Code doesn't work from Obtain an Access Token - Microsoft Translator

    Question

  • Hi,

    I create a simple project ASP.NET and I add in my webform the code from the page http://msdn.microsoft.com/en-us/library/hh454950.aspx

    I subscribe and register my application.

    I launch the code

    I obtain an issue when the back read is done

                try
                { // get the response
    
                    WebResponse webResponse = webRequest.GetResponse();
                    if (webResponse == null)
                    { return null; }
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(MTAccessToken));
                    MTAccessToken token = (MTAccessToken)serializer.ReadObject(webResponse.GetResponseStream());
                    return token;
                }
    
                catch (WebException ex)
                {
                   textBox1.Text = "HttpPost: Response error";
                }
                return null;
    

    {System.Net.WebException: The remote server returned an error: (400) Bad Request.

       at System.Net.HttpWebRequest.GetResponse()

       at Mappy.WebForm1.HttpPost(String uri, String parameters) in C:\Users\obssample\Documents\Visual Studio 2010\Projects\Mappy\Mappy\WebForm1.aspx.cs:line 122}

     

    I notice other people encountered the same kind of issue

    http://social.microsoft.com/Forums/en/translator/thread/a5919a7e-f8c4-46d7-822c-5161d2a991a7

    I suppose I need to wait a little time for the application to be validated? (maybe 24 hours).

    Is it true?

    Best regards,

    Alexandre

     


    Best regards, Alexandre http://alexandrebarault.wordpress.com
    Thursday, November 17, 2011 1:52 PM

Answers

  • Hi Alexandre,

    Are you still having the issue?  If so, can you send us the detailed error response to help investigate?

    You can display the full error response by changing your catch block to the following:

    catch (WebException ex)
    {
        textBox1.Text = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
    }

    • Marked as answer by alexandreb Thursday, November 24, 2011 5:43 PM
    Wednesday, November 23, 2011 4:42 PM
    Moderator

All replies

  • Hi,

    I try one more time, always same issue....

    Do you have any feedback?

    Best regards,

    Alexandre


    Best regards, Alexandre http://alexandrebarault.wordpress.com
    Thursday, November 17, 2011 3:15 PM
  • Hi,

    I am not familiar with Translator API. For ACS side, as long as you’ve configured everything on the ACS portal, and you’re using the correct code, you will be able to obtain the token. But I guess you’re not using your own ACS. Translator API has its own ACS rules.

      >> I obtain an issue when the back read is done

    Since the issue is related to Translator API, I would like to suggest you to post the question on the Bing Developer forum: http://www.bing.com/community/developer/default.aspx, there are more related experts in that forums.

     

    Best Regards,

    Ming Xu.


    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework
    Friday, November 18, 2011 5:29 AM
    Moderator
  • Hi,

    I follow your advise.

    But this forum is a little bit strange, my post needs to be moderate, I need to search how to post a message.

    Hum weird.

    Thank you.

    I try this morning and same issue.

    I use my own ACS I think.

    I did .  Subscribe to the Microsoft Translator API on Azure Marketplace, 2. Register your application and 3. Obtain an Access Token

    "You can obtain a token by making an HTTP POST to the token service. The parameters for the token request are passed in the HTTP request body and are URL encoded."=> I'm supposed obtain the token by call the token service but not :(

     


    Best regards, Alexandre http://alexandrebarault.wordpress.com
    Friday, November 18, 2011 9:17 AM
  • Hi Alexandre,

     

    The sample code has a bug in it and it does not handle the form parameter encoding properly.

    We'll publish the update shortly.

     

    Thanks,

    Max

    Friday, November 18, 2011 9:10 PM
    Owner
  • Here's the one line fix so you don't need to wait for the sample update:

    Current  sample code:

    string request = string.Format("grant_type=client_credentials&client_id={0}&client_secret={1}&scope=http://api.microsofttranslator.com", clientId, clientSecret);

    New code:

    string request = string.Format("grant_type=client_credentials&client_id={0}&client_secret={1}&scope=http://api.microsofttranslator.com", clientId, HttpUtility.UrlEncode(clientSecret));

    Thanks,

    Max

    Friday, November 18, 2011 9:59 PM
    Owner
  • Hi, Regarding your answer I modify my code by the proposal.

    I still obtain the same error on the same place.

    try
                { // get the response
    
                    WebResponse webResponse = webRequest.GetResponse();
                    if (webResponse == null)
                    { return null; }
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(MTAccessToken));
                    MTAccessToken token = (MTAccessToken)serializer.ReadObject(webResponse.GetResponseStream());
                    return token;
                }
    
                catch (WebException ex)
                {
                   textBox1.Text = "HttpPost: Response error";
                }
                return null;
    

     

     

    {System.Net.WebException: The remote server returned an error: (400) Bad Request.

       at System.Net.HttpWebRequest.GetResponse()

       at Mappy.WebForm1.HttpPost(String uri, String parameters) in C:\Users\obssample\Documents\Visual Studio 2010\Projects\Mappy\Mappy\WebForm1.aspx.cs:line 122}

     


    Best regards, Alexandre http://alexandrebarault.wordpress.com
    Tuesday, November 22, 2011 9:53 AM
  • Hi,

    Today I gave the Translator SOAP API a try, and indeed as Max pointed out, using the sample code provided on MSDN, I get a 400 error. But after I URL encode the client secret, everything works fine. I don't see you've modified your code according to Max's suggestion. Can you update the code and try again?
     
    By the way, in the process, you're really dealing with Translator API and Translator's ACS only. Since you're not using your own ACS, if you have further questions, you can also ask them on the Translator forum.

     

    Best Regards,

    Ming Xu.


    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework
    Wednesday, November 23, 2011 2:58 PM
    Moderator
  • Hi Alexandre,

    Are you still having the issue?  If so, can you send us the detailed error response to help investigate?

    You can display the full error response by changing your catch block to the following:

    catch (WebException ex)
    {
        textBox1.Text = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
    }

    • Marked as answer by alexandreb Thursday, November 24, 2011 5:43 PM
    Wednesday, November 23, 2011 4:42 PM
    Moderator
  • Hi,

    Thank you for your help.

    Finally I add the code new StreamReader(ex.Response.GetResponseStream()).ReadToEnd()

    And I obtain another exception not the same 

    {"error":"invalid_client","error_description":"ACS50012: Authentication failed. \r\nTrace ID: 6e834a5e-1ac7-4740-a672-dd732639fafe\r\nTimestamp: 2011-11-24 17:39:38Z"}

    So I change my informations related to the registered application and it works!!

    Thank you,

    Best regards,

    Alexandre


    Best regards, Alexandre http://alexandrebarault.wordpress.com
    Thursday, November 24, 2011 5:45 PM
  • OMG!!!!!! Pleeeeeeaaaaaassssseeeeeee Microsoft!!!!!!! LET EVERYBODY KNOW that client_id IS NOT ALPHANUMERICAL!!!!!!!

    Two things:

    - clientId is really (has to be) client_id

    - upon app registration client_id is NUMBERS ONLY!

    • Proposed as answer by Irnuk Saturday, February 22, 2014 8:13 PM
    Saturday, December 03, 2011 8:31 PM
  • Thanks Nstiac! It only worked for me when I created a client id using only numbers....

    Saturday, February 22, 2014 8:13 PM
  • THIS was IT! Thank you very much Nstiac and Irnuk!!!

    :)

    Wednesday, March 26, 2014 10:39 PM