访问令牌在艾玛迪斯测试开发人员中仅设置为 30 分钟



我目前正在创建一个旅行社网站,我使用Amadeus搜索低票价等,但每次访问令牌在30分钟内过期时,我都会得到状态401(访问令牌已过期)。并通过他们的网站反复请求另一个访问令牌并将其粘贴到我的代码中。请问,是否有解决方案,以便它会自动更改我的代码中的访问令牌

我目前使用此代码来请求访问代码

**

$url = "https://test.api.amadeus.com/v1/security/oauth2/token";
$curls = curl_init();
curl_setopt($curls, CURLOPT_URL, $url);
curl_setopt($curls, CURLOPT_POST, true);
curl_setopt($curls, CURLOPT_POSTFIELDS, 'grant_type=client_credentials&client_id={apikey}&client_secret={apisecret}');
curl_setopt($curls, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
$token = curl_exec($curls);
curl_close($curls);
$tokenresult = json_decode($token,true);
print_r ($tokenresult);

**

但是每次我刷新我的网站时,它也会显示

"type": "amadeusOAuth2Token",
            "username": "",
            "application_name": "Thesis",
            "client_id": "",
            "token_type": "Bearer",
            "access_token": "*****",
            "expires_in": 1799,
            "state": "approved",
            "scope": ""

这很烦人。

这是我使用access_token的代码

$ch = curl_init("https://test.api.amadeus.com/v1/shopping/flight-offers?origin=$client_flyingfrom&destination=$client_flyingto&departureDate=$client_departing&returnDate=$client_returning&nonStop=false&currency=PHP&max=2");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
   'Content-Type: application/json',
   'Authorization: Bearer *****' 
   ));
$data = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
$json = json_decode($data,true);
print_r($json);

你能帮我吗? 有没有办法自动更改我的访问令牌,而无需在我的代码中复制和粘贴新的访问令牌? 非常感谢大家

您可以在此处找到帮助您实施 oauth2 授权过程的指南。

令牌的生存时间为 30 分钟,但您可以随时请求新令牌(无需通过门户,您有一个 API)。门户上的那个实际上只是为了让你在不编写代码的情况下使用 API。

您发布的第一个代码是实现 oauth2 进程来获取access_token,您只需每 30 分钟调用一次即可获取一个新代码(甚至在此之前 - 由您决定)。在此调用中,您将保存access_token并将其添加到下一次 API 调用的标头中(在指南中进行了说明)。

由于这一行,它会将其打印到您的网站中

print_r ($tokenresult)

最新更新