php - Amadeus 测试开发人员中的访问令牌是否仅设置为 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¤cy=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)
推荐阅读
- angular - 获取数组中的特定对象
- javascript - 续集错误:传递给订单/组的未知结构:订购时的文字
- reactjs - 在 mobx-state-tree 中导出接口时是否缺少定义类型?
- flutter - Flutter - 当 onDoubleTap on icon 时滚动到顶部
- java - 如何将 rtp 数据包有效负载字节转换为任何音频数据?
- javascript - 如何拆分 JSON 文件的内容,然后显示用户标签而不是 ID?
- linux - 如果不在另一个匹配模式之后,sed 删除匹配模式的行
- node.js - NODEMAILER - 错误:缺少“PLAIN”的凭据
- android - Android Studio 无法打开文件
- android - RxAndroidBle: BleDisconnectedException: 从 MAC='XX:XX:XX:XX:XX:XX' 断开连接,状态为 0 (GATT_SUCCESS)