使用Javascript获取对Amadeus测试飞行API进行身份验证 [英] Authenticating Amadeus Test Flight Api With Javascript Fetch

查看:66
本文介绍了使用Javascript获取对Amadeus测试飞行API进行身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在测试Amadeus Flight-Offers Api时遇到问题.身份验证

I am having issues with testing of Amadeus Flight-Offers Api. Authentication

跨域请求被阻止:相同来源策略不允许读取

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://test.api.amadeus.com/v2/shopping/flight-offers?originLocationCode=SYD&destinationLocationCode=BKK&departureDate=2021-02-01&returnDate=2021-02-05&adults=1&max=3. (Reason: header ‘authentication’ is not allowed according to header ‘Access-Control-Allow-Headers’ from CORS preflight response).

这是我的代码

"use_strict";
function getPlane(){
    let url = "https://test.api.amadeus.com/v2/shopping/flight-offers?originLocationCode=SYD&destinationLocationCode=BKK&departureDate=2021-02-01&returnDate=2021-02-05&adults=1&max=3";
    fetch(url,{
        method:"GET",
        headers:{
            "Content-Type":"application/vnd.amadeus+json",
            "Authentication":"Bearer code here"
        },
        mode:"cors",
        catch:"default"
    }).then(function(response){
        if(response.ok){
            return response.json();
        }else{
            throw new Error(error);
        }
    }).then(function(data){
        console.log(data);
        //query data here
        document.getElementById("flight").insertAdjacentHTML('afterbegin',data.data[0].itineraries[0].segments[0].operating.carrierCode)
    }).catch(function(error){
        console.log(error);
    });
}

getPlane();

推荐答案

Amadeus自助服务API调用必须包含值为 Bearer access_token Authorization HTTP标头,使用您生成的访问令牌.在您的代码中添加 Authentication 而不是 Authorization .另外,仅当您请求访问令牌时, Content-Type 才是必需的.有关更多信息,请参见授权指南./p>

The Amadeus Self-Service API calls must contain the Authorization HTTP header with the value Bearer access_token, using the access token you have generated. In your code you add Authentication instead of Authorization. Also, the Content-Type is necessary only when you request the access token. Refer to the authorization guide for more information.

    "use_strict";
    function getPlane(){
        let url = "https://test.api.amadeus.com/v2/shopping/flight-offers?originLocationCode=SYD&destinationLocationCode=BKK&departureDate=2021-02-01&returnDate=2021-02-05&adults=1&max=3";
        fetch(url,{
            method:"GET",
            headers:{
            "Authorization": "Bearer ACCESS_TOKEN_HERE",
            },
            mode:"cors",
            catch:"default"
        }).then(function(response){
            if(response.ok){
                return response.json();
            }else{
                throw new Error(error);
            }
        }).then(function(data){
            console.log(data);
            //query data here
            document.getElementById("flight").insertAdjacentHTML('afterbegin',data.data[0].itineraries[0].segments[0].operating.carrierCode)
        }).catch(function(error){
            console.log(error);
        });
    }

    getPlane();

这篇关于使用Javascript获取对Amadeus测试飞行API进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆