typescript Amadeus航班预订正常,但酒店预订API不正常

w41d8nur  于 2023-02-10  发布在  TypeScript
关注(0)|答案(1)|浏览(191)

所以我第一次集成Amadeus用于机票预订,一切都很顺利,但当我开始集成酒店预订时,问题出现了。如果我使用Amadeus的nodejs库,我会得到一个响应,我的访问令牌无效。这是我的nestjs服务和响应的代码。

async hotelSearch(data) {
  try {
  
    var amadeus = new Amadeus({
      clientId: process.env.API_KEY,
      clientSecret: process.env.API_SECRET
    });
    return await amadeus.shopping.hotelOffers.get(data)

  } catch (error) {
    return error;
  }
}

这是回应。结果-

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

当我为它使用库时,我怎么能得到无效的访问令牌错误??无论如何,在面对这个问题后,我决定使用Axios代替,但仍然没有成功。

async getToken(): Promise<{access_token: string}> {
      try {

        const data = qs.stringify({
          client_id: process.env.API_KEY,
          client_secret: process.env.API_SECRET,
          grant_type: 'client_credentials' 
        });
        
        const config:AxiosRequestConfig = {
          method: 'post',
          maxBodyLength: Infinity,
          url: 'https://test.api.amadeus.com/v1/security/oauth2/token',
          headers: { 
            'Content-Type': 'application/x-www-form-urlencoded'
          },
          data: data
        };
        
        return await axios(config)
          .then((response) => {
            return((response.data));
          })
          .catch((error) => {
            console.log(error);
          });
        
      } catch (error) {
        return error;
      }
    }

  
    async hotelSearch(data) {
      try {
        const tokenData=await this.getToken()
        const refreshToken='Bearer '+ ( tokenData).access_token
        console.log('token', refreshToken)
        const config:AxiosRequestConfig = {
          method: 'get',
          maxBodyLength: Infinity,
          url: 'https://test.api.amadeus.com/v1/reference-data/locations/hotels/by-city',
          data: data,
          headers: {'Authorization':refreshToken},
          
        };
        return await axios(config).then((response) => {
          return(JSON.stringify(response.data));
        })
        .catch((error) => {
          return(error);
        });
      
        // var amadeus = new Amadeus({
        //   clientId: process.env.API_KEY,
        //   clientSecret: process.env.API_SECRET
        // });
        // return await amadeus.shopping.hotelOffers.get(data)

      } catch (error) {
        return error;
      }
    }

而这就是我这次得到的回应。

{
    "message": "Request failed with status code 400",
    "name": "Error",
    "stack": "Error: Request failed with status code 400\n    at createError (E:\\travel-portal\\travel-portal\\node_modules\\axios\\lib\\core\\createError.js:16:15)\n    at settle (E:\\travel-portal\\travel-portal\\node_modules\\axios\\lib\\core\\settle.js:17:12)\n    at IncomingMessage.handleStreamEnd (E:\\travel-portal\\travel-portal\\node_modules\\axios\\lib\\adapters\\http.js:322:11)\n    at IncomingMessage.emit (node:events:525:35)\n    at endReadableNT (node:internal/streams/readable:1359:12)\n    at processTicksAndRejections (node:internal/process/task_queues:82:21)",
    "config": {
        "transitional": {
            "silentJSONParsing": true,
            "forcedJSONParsing": true,
            "clarifyTimeoutError": false
        },
        "transformRequest": [
            null
        ],
        "transformResponse": [
            null
        ],
        "timeout": 0,
        "xsrfCookieName": "XSRF-TOKEN",
        "xsrfHeaderName": "X-XSRF-TOKEN",
        "maxContentLength": -1,
        "maxBodyLength": null,
        "headers": {
            "Accept": "application/json, text/plain, */*",
            "Authorization": "Bearer CMHEjXBrpzE7YxF9O7GKygCtzCxO",
            "Content-Type": "application/json",
            "User-Agent": "axios/0.26.1",
            "Content-Length": 117
        },
        "method": "get",
        "url": "https://test.api.amadeus.com/v1/reference-data/locations/hotels/by-city",
        "data": "{\"cityCode\":\"DEL\",\"radius\":\"5\",\"radiusUnit\":\"KM\",\"checkInDate\":\"2023-03-10\",\"checkOutDate\":\"2023-03-11\",\"adults\":\"2\"}"
    },
    "status": 400
}

我已经交叉检查了负载。当我控制它的时候,不记名令牌是好的,甚至我用fiddler检查了请求,也有头和数据被传递。任何帮助通过任何一种方法来完成这项工作都是非常感谢的。

aemubtdh

aemubtdh1#

amadeus.shopping.hotelOffers.get()终结点已停用,因此你将无法使用它。请安装最新版本的Node库并按如下所示使用新的Hotel Search终结点:

var Amadeus = require(amadeus);
var amadeus = new Amadeus({
    clientId: 'YOUR_API_KEY',
    clientSecret: 'YOUR_API_SECRET'
});

// Get list of available offers in specific hotels by hotel ids
amadeus.shopping.hotelOffersSearch.get({
    hotelIds: 'RTPAR001',
    adults: '2'
}).then(function (response) {
  console.log(response);
}).catch(function (response) {
  console.error(response);
});

您还可以查看migration guide以了解更多详细信息以及如何使用新的Hotel Search版本。

相关问题