通过Javascript调用Sabre Dev Studio API
我正在尝试使用RESTAPI制作Sabre Dev Studio的web应用程序。我正在使用javascript。我收集了应用程序所需的访问令牌和客户端密码通过Javascript调用Sabre Dev Studio API,javascript,rest,oauth-2.0,sabre,Javascript,Rest,Oauth 2.0,Sabre,我正在尝试使用RESTAPI制作Sabre Dev Studio的web应用程序。我正在使用javascript。我收集了应用程序所需的访问令牌和客户端密码 我编写此代码是为了发送api请求: 如果请求有效,API将发送一个包含访问令牌的响应,否则它应该给我一个错误消息对象。但就我而言,我什么也没有收到。警报功能向我显示一个空白警报窗口。我不知道问题出在哪里。有人能帮我解决这个问题吗?我不是javascript专家,但我至少看到两个错误: var authorizationBasic=$.ba
我编写此代码是为了发送api请求:
如果请求有效,API将发送一个包含访问令牌的响应,否则它应该给我一个错误消息对象。但就我而言,我什么也没有收到。警报功能向我显示一个空白警报窗口。我不知道问题出在哪里。有人能帮我解决这个问题吗?我不是javascript专家,但我至少看到两个错误:
您需要分别对clientId和clientSecrect执行base64,然后在两者之间加上冒号
我不确定您是否可以在URL字符串中定义要使用的HTTP,而且它似乎也不是必需的
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;
}
}
}
FWIW在我的头撞到墙上一段时间后,如果有人需要,我就用fetch()编写了一个通用身份验证方法。它使用auth api返回的JSON对象返回承诺
static authenticate(clientId, clientSecret, baseURL = 'https://api.test.sabre.com/', apiPath = 'v2/auth/token') {
let authToken = {};
return fetch(`${baseURL}${apiPath}?grant_type=client_credentials`, {
method: 'POST',
headers: {
'Accept': '*/*',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + window.btoa(window.btoa(clientId) + ':' + window.btoa(clientSecret))
}
}).then((response) => response.json()).then(response => {
if (response){
return Promise.resolve(response);
} else {
console.error('SabreAPI authenticate Error - no response');
return Promise.reject(response);
}
}).catch(error => {
console.error('SabreAPI authenticate Error: '+error);
return Promise.reject(error);
});
}
用法:
authenticate('myId', 'mySecret').then(response => {
let authToken = {};
authToken.access_token = response.access_token;
authToken.expires_in = response.expires_in;
authToken.token_type = response.token_type;
authToken.expiration_datestamp = new Date(new Date().getTime() + 1000 * (response.expires_in));
}).catch(error => {
... do whatever you want to catch the error
});
之后,您可以通过传递“Bearer”来执行常规的fetch-GET/PUT/POST/DELETE调用:头中的authToken.access\u-token;你可能需要先摆脱它,而不是积极的
authenticate('myId', 'mySecret').then(response => {
let authToken = {};
authToken.access_token = response.access_token;
authToken.expires_in = response.expires_in;
authToken.token_type = response.token_type;
authToken.expiration_datestamp = new Date(new Date().getTime() + 1000 * (response.expires_in));
}).catch(error => {
... do whatever you want to catch the error
});