0

I am trying to connect the Bing translator to my EXT JS. I'm having trouble trying to get the access token. No matter what I've tried I get 400 bad request.

This is my code:

 var clientId = encodeURI("<my id>"),
    clientSecret = encodeURI("<my secret>"),
    scope = encodeURI("http://api.microsofttranslator.com"),
    grantType = encodeURI("client_credentials");        
    var params = {client_id     :  clientId,
                  client_secret :  clientSecret,
                  scope         :  scope, 
                  grant_type    :  grantType};
    Ext.Ajax.request({
               url     : "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/",
               method  : "POST",
               params  : params,
               success : function(response) {
                            alert("Success");
               },
               fail    : function() {
                            alert("Fail");
               }
    }); 

I am beginning to think it is not possible. I believe this is making a request which https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/ doesn't allow. Maybe it is doing this client side? Or maybe the https url doesn't work in Ajax.request. Any help at all appreciated.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
user1375026
  • 403
  • 4
  • 12
  • 23

3 Answers3

1

As you suggest, it is not possible. Note that you need to use JSONP+GET to make any request to a third-party server, but the Bing Translator API only accepts POST requests. The browser is using GET for your request in spite of indicating POST in your code (use Chrome Developer Tools or similar to check this) because JSONP and POST are not compatible (see here).

Previous version of the Bing Translator API accepted GET requests but this is no longer the case. Solution: you need to implement your own server-side service to get the access token and the translation, and then return the latter to the browser.

By the way, this is a similar code which uses JQuery to get the access token and which does not work for the same reasons:

$.ajax({
  type: "POST",
  url: "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13",
  data: {client_id:"bbcat",client_secret: "SECRETKEY",
     scope: "http://api.microsofttranslator.com", grant_type: "client_credentials"},
  dataType: "jsonp",
  success: function(data) {
    console.log(data);
  },
  error: function(data) {
    console.error(data);
  }
});
Community
  • 1
  • 1
Juan
  • 11
  • 4
0

Not very familiar with Ext.Ajax, but try this:

  1. Try doing a GET instead of a POST
  2. Instead of the params as you have them, put them into a querystring, i.e. https://?clientid=&etc. etc.
0

You can't use Ext.Ajax cross domain, you need to use either JSONP or a form post to get data from a third party domain.

Dawesi
  • 568
  • 3
  • 9