I am trying to make a web application of Sabre Dev Studio using there Rest API. I am using javascript. I collected the required access tokens and client secret for the app .
I wrote this code to send an api request:
var clientId = "V1:abcD123:OPQRST:UVW";
var clientSecret = "aBcdEfG";
// Using jQuery Plugin for Encoding
var authorizationBasic = $.base64.btoa(clientId + ':' + clientSecret);
authorizationBasic = $.base64.btoa(authorizationBasic);
var request = new XMLHttpRequest();
request.open('POST', 'https://api.sabre.com/v2/auth/token HTTP/1.1', true);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
request.setRequestHeader('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36');
request.setRequestHeader('Origin', 'chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo');
request.setRequestHeader('Authorization', 'Basic ' + authorizationBasic);
request.setRequestHeader('Accept', '*/*');
request.send("grant_type=client_credentials");
request.onreadystatechange = function () {
if (this.readyState === 4) {
alert(this.responseText);
}
};
If the request is valid, the API will send a response that contains the access token else it shuld give me an error massage object. But in my case I dont receive anything. The alert function shows me a blank alert window. I dont know where is the problem. Can anyone help me to this problem?
I'm not a javascript expert, but I see at least 2 errors:
var authorizationBasic = $.base64.btoa(clientId + ':' + clientSecret);
You need to do base64 to the clientId and clientSecrect separately, and then to both together with a colon in between
request.open('POST', 'https://api.sabre.com/v2/auth/token HTTP/1.1', true);
I am not sure you can define the HTTP that you'll be using in the URL string, and it does not seem to be required either.
Below is a function that I've tested successfully:
function doFunction() {
var clientId = "V1:abcD123:OPQRST:UVW";
var clientSecret = "aBcdEfG";
var authorizationBasic = window.btoa(window.btoa(clientId) + ':' + window.btoa(clientSecret));
request = new XMLHttpRequest();
var url = "https://api-crt.cert.havail.sabre.com/v2/auth/token";
request.open("POST", url, true);
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
request.setRequestHeader("Authorization", "Basic " + authorizationBasic);
var payload = "grant_type=client_credentials";
request.send(payload);
request.onreadystatechange = function () {
if (request.readyState == 4 && request.status == 200) {
alert(request.responseText);
document.getElementById("txt").value = request.responseText;
}
}
}