2

I 'd like invoking the token at APIM 1.10 using ajax.

I tryed this:

$(function() {
  $.ajax({
    url:'https://localhost:9443/token',
    type: 'POST',
    dataType: 'json',
    contentType: 'application/json',
    data:{
      'grant_type':'client_credentials'
    },
    headers:{
    'Authorization': 'Basic Q3dLYmJybWhDUnJpMVJlMDIycWp0UGtxWUlrYTpjYzZQMkVFOEVJRDNqSWRSN2ZnRUoyUDU2ajRh',
   },
   success:function(e){
      $('#token').html(e);
    }
  });
});

But didn't receved nothing. Just a empty option request.

API Manager show this:

    [2016-03-30 14:48:40,340] DEBUG - headers http-incoming-1 >> OPTIONS /token HTTP/1.0
    [2016-03-30 14:48:40,340] DEBUG - headers http-incoming-1 >> X-Forwarded-Host: <My host>
    [2016-03-30 14:48:40,340] DEBUG - headers http-incoming-1 >> X-Forwarded-Server: <My Host>
    [2016-03-30 14:48:40,340] DEBUG - headers http-incoming-1 >> X-Forwarded-For: <My IP>
    [2016-03-30 14:48:40,340] DEBUG - headers http-incoming-1 >> Host: <My Host> 
    [2016-03-30 14:48:40,340] DEBUG - headers http-incoming-1 >> Connection: close
    [2016-03-30 14:48:40,341] DEBUG - headers http-incoming-1 >> User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:41.0) Gecko/20100101 Firefox/41.0
    [2016-03-30 14:48:40,341] DEBUG - headers http-incoming-1 >> Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    [2016-03-30 14:48:40,341] DEBUG - headers http-incoming-1 >> Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3
    [2016-03-30 14:48:40,341] DEBUG - headers http-incoming-1 >> Accept-Encoding: gzip, deflate
    [2016-03-30 14:48:40,341] DEBUG - headers http-incoming-1 >> Origin: null
    [2016-03-30 14:48:40,341] DEBUG - headers http-incoming-1 >> Access-Control-Request-Method: POST
    [2016-03-30 14:48:40,341] DEBUG - headers http-incoming-1 >> Access-Control-Request-Headers: authorization,content-type
    [2016-03-30 14:48:40,341] DEBUG - headers http-incoming-1 >> Pragma: no-cache
    [2016-03-30 14:48:40,341] DEBUG - headers http-incoming-1 >> Cache-Control: no-cache
    [2016-03-30 14:48:40,386] DEBUG - headers http-incoming-1 << HTTP/1.0 200 OK
    [2016-03-30 14:48:40,386] DEBUG - headers http-incoming-1 << Access-Control-Request-Headers: authorization,content-type
    [2016-03-30 14:48:40,386] DEBUG - headers http-incoming-1 << Host: <My Host>
    [2016-03-30 14:48:40,386] DEBUG - headers http-incoming-1 << X-Forwarded-Server: <My Host>
    [2016-03-30 14:48:40,386] DEBUG - headers http-incoming-1 << Origin: null
    [2016-03-30 14:48:40,386] DEBUG - headers http-incoming-1 << Access-Control-Request-Method: POST
    [2016-03-30 14:48:40,386] DEBUG - headers http-incoming-1 << Cache-Control: no-cache
    [2016-03-30 14:48:40,387] DEBUG - headers http-incoming-1 << Pragma: no-cache
    [2016-03-30 14:48:40,387] DEBUG - headers http-incoming-1 << X-Forwarded-Host: <My Host>
    [2016-03-30 14:48:40,387] DEBUG - headers http-incoming-1 << Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3
    [2016-03-30 14:48:40,387] DEBUG - headers http-incoming-1 << Allow: POST
    [2016-03-30 14:48:40,387] DEBUG - headers http-incoming-1 << Accept-Encoding: gzip, deflate
    [2016-03-30 14:48:40,387] DEBUG - headers http-incoming-1 << X-Forwarded-For: <my ip>
    [2016-03-30 14:48:40,387] DEBUG - headers http-incoming-1 << Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    [2016-03-30 14:48:40,387] DEBUG - headers http-incoming-1 << Date: Wed, 30 Mar 2016 17:48:40 GMT
    [2016-03-30 14:48:40,387] DEBUG - headers http-incoming-1 << Server: WSO2-PassThrough-HTTP
    [2016-03-30 14:48:40,387] DEBUG - headers http-incoming-1 << Connection: Close
    [2016-03-30 14:48:40,389] DEBUG - wire << "HTTP/1.0 200 OK[\r][\n]"
    [2016-03-30 14:48:40,390] DEBUG - wire << "Access-Control-Request-Headers: authorization,content-type[\r][\n]"
    [2016-03-30 14:48:40,390] DEBUG - wire << "Host: <my host name>[\r][\n]"
    [2016-03-30 14:48:40,390] DEBUG - wire << "X-Forwarded-Server: <my host>[\r][\n]"
    [2016-03-30 14:48:40,390] DEBUG - wire << "Origin: null[\r][\n]"
    [2016-03-30 14:48:40,390] DEBUG - wire << "Access-Control-Request-Method: POST[\r][\n]"
    [2016-03-30 14:48:40,390] DEBUG - wire << "Cache-Control: no-cache[\r][\n]"
    [2016-03-30 14:48:40,390] DEBUG - wire << "Pragma: no-cache[\r][\n]"
    [2016-03-30 14:48:40,390] DEBUG - wire << "X-Forwarded-Host: <My Host NAME>[\r][\n]"
    [2016-03-30 14:48:40,390] DEBUG - wire << "Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3[\r][\n]"
    [2016-03-30 14:48:40,390] DEBUG - wire << "Allow: POST[\r][\n]"
    [2016-03-30 14:48:40,390] DEBUG - wire << "Accept-Encoding: gzip, deflate[\r][\n]"
    [2016-03-30 14:48:40,390] DEBUG - wire << "X-Forwarded-For: <My computer IP>[\r][\n]"
    [2016-03-30 14:48:40,391] DEBUG - wire << "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8[\r][\n]"
    [2016-03-30 14:48:40,391] DEBUG - wire << "Date: Wed, 30 Mar 2016 17:48:40 GMT[\r][\n]"
    [2016-03-30 14:48:40,391] DEBUG - wire << "Server: WSO2-PassThrough-HTTP[\r][\n]"
    [2016-03-30 14:48:40,391] DEBUG - wire << "Connection: Close[\r][\n]"
[2016-03-30 14:48:40,391] DEBUG - wire << "[\r][\n]"

Does somebody know how can I do this?

Thanks,

Eliezer Trajano

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105

1 Answers1

2

Here the problem is content type.The content type should be application/x-www-form-urlencoded according to documentation and you need to remove the json data type as well.With above changes the ajax code should be as follows

$(function() {
  $.ajax({
    url:'https://localhost:9443/oauth2/token',
    type: 'POST',
    contentType: 'application/x-www-form-urlencoded',
    data:{
      'grant_type':'client_credentials'
    },
    headers:{
    'Authorization': 'Basic Q3dLYmJybWhDUnJpMVJlMDIycWp0UGtxWUlrYTpjYzZQMkVFOEVJRDNqSWRSN2ZnRUoyUDU2ajRh',
   },
   success:function(e){
      $('#token').html(e);
    }
  });
});
ajanthan
  • 431
  • 3
  • 4
  • Thanks Ajanthan for your answer! But now the error is a empty option with "302 found" . – Eliezer Trajano Mar 30 '16 at 18:41
  • The token endpoint is also wrong .it should be https://localhost:9443/oauth2/token .I have corrected in original post. – ajanthan Mar 30 '16 at 18:55
  • 1
    Works witdh: `code` $.ajax({ url:'https://:8243/token', contentType: 'application/x-www-form-urlencoded', type: 'POST', data:{ 'grant_type':'client_credentials' }, headers:{ 'Authorization': 'Basic aGJvYlJwM0RnYWNuOGVPb194elY5R3VtSjlRYTp2aDIzQ3Rwd2VaV0xmR2JmTHFBZzNrTlBiZ1Fh', }, success:function(e){ alert(e); } }); });`code` – Eliezer Trajano Mar 30 '16 at 21:30
  • Thanks a lot; Works welll!! – Eliezer Trajano Mar 30 '16 at 21:32