Obtención del token de acceso a la API de traducción de Bing a través de javascript

I'm writing a web app and would like to use the Bing translation API since Google charges for theirs. I've registered on the Azure Marketplace and registered an app as well. The problem is that I haven't found out how to get the actual access token I need. I tried some AJAX calls in plunker but all of them hit a 'No 'Access-Control-Allow-Origin' header is present on the requested resource' wall. The only instructions I find on obtaining the access token are all in a different language (php, C#, Java) while I really need it to be Javascript. Is there a way to get the access token this way at all?

Currently I'm trying to call the service this way:

 function createCORSRequest(method, url) {
      var xhr = new XMLHttpRequest();

      if ("withCredentials" in xhr) {

        // Check if the XMLHttpRequest object has a "withCredentials" property.
        // "withCredentials" only exists on XMLHTTPRequest2 objects.
        xhr.open(method, url, true);

      } else if (typeof XDomainRequest != "undefined") {

        // Otherwise, check if XDomainRequest.
        // XDomainRequest only exists in IE, and is IE's way of making CORS requests.
        xhr = new XDomainRequest();
        xhr.open(method, url);

      } else {

        // Otherwise, CORS is not supported by the browser.
        xhr = null;

      return xhr;

  var xhr = createCORSRequest('POST', 'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13?client_id=[CLIENT_ID_HERE]&client_secret=[CLIENT_SECRET_HERE]&scope=http://api.microsofttranslator.com&grant_type=client_credentials&');
  xhr.onloadend = function(data){

preguntado el 12 de junio de 14 a las 10:06

1 Respuestas

I just went through the same thing, and unfortunately, it seems the answer is that it is not adecuado to do this in Javascript. (Unless it's Node.js running on the server end.)

To obtain the access token from Javascript (e.g. on a web page), you're likely trying to send the clientID e clientSecret in the clear. For this reason, the examples provided show how to obtain the access token via server-side languages.

So what I plan to do is provide a PHP implementation on my website and obtain the access token from my site via the PHP script, then send that to the Translate API.

Once I realized what I needed to look for, I found a great summary of all the steps and example code at http://webspeechapi.blogspot.ca/2013/04/how-to-use-new-bing-translator-api-with.html which has both the server-side PHP and the Javascript to fetch the access token from that, then send it all to the Translate service.

respondido 07 nov., 14:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.