Amadeus API Authentication

2019-08-04 18:48发布

问题:

I just try make an request against amadeus API

I already have API KEY and API Secret, I pass in XHR header Authorization with value of my API key

This is my JQuery:

$.ajax({
            type: "get",
            url: "https://test.api.amadeus.com/v1/shopping/flight-dates?origin=MIA&destination=SFOˆduration=15&nonStop=true",
            dataType: 'json',
            async: true,
            beforeSend: function(xhr) {
                xhr.setRequestHeader('Authorization',
                    'Basic ' + amadeusApiKey);
            },                
            success: function(json) {
                console.log(json);
            }
        });

but the response is HTTP code 401 and body

    {
    "errors": [
      {
        "code": 38190,
        "title": "Invalid access token",
        "detail": "The access token provided in the Authorization header is invalid",
        "status": 401
      }
    ],
}

Should I make some encode with the API Key and Secret Key, I don`t find anything in the documentation

回答1:

The API you are trying to target is secured using Client credentials grant type.

Before making the call, you should generate an access token using your API key/secret, please refer to this guide. Once you have generated an access token, your request should look like:

$.ajax({
        type: "get",
        url: "https://test.api.amadeus.com/v1/shopping/flight-dates?origin=MIA&destination=SFOˆduration=15&nonStop=true",
        dataType: 'json',
        async: true,
        beforeSend: function(xhr) {
            xhr.setRequestHeader('Authorization',
                'Bearer ' + amadeusAccessToken);
        },                
        success: function(json) {
            console.log(json);
        }
    });

Alternatively, Amadeus provides some SDKs to abstract this complexity.



标签: api amadeus