使用PHP集成Sabre Dev API时遇invalid_client错误求助
Sabre API集成报错排查与代码优化
问题情况
使用PHP结合测试凭证集成Sabre API时,收到如下报错:
Array ( [error] => invalid_client [error_description] => Wrong clientID or clientSecret )
原实现代码
$saberUrl = 'https://api.platform.sabre.com/v2/auth/token'; $credentials = [ 'user_id' => 'V1:uname:DEVCENTER:AA', 'password' => 'pwd' ]; $base64EncodeConcat =base64_encode(base64_encode($credentials['user_id']) . ':' . base64_encode($credentials['password'])); // Initialize cURL session $curl = curl_init($saberUrl); // Set cURL options curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HTTPHEADER, array( 'Content-Type: application/x-www-form-urlencoded', 'Authorization: Basic ' . $base64EncodeConcat )); // Include the grant_type parameter in the POST fields curl_setopt($curl, CURLOPT_POSTFIELDS, "grant_type=client_credentials"); // Execute cURL session $response = curl_exec($curl); // Close cURL session curl_close($curl); // Process the response $data = json_decode($response, true); // Output the data print_r($data);
排查建议与代码优化
核心问题点
原代码的Base64编码逻辑错误:Sabre要求将user_id:password的拼接字符串直接做一次Base64编码,而非分别编码后再拼接编码。
优化后的代码
$saberUrl = 'https://api.platform.sabre.com/v2/auth/token'; $credentials = [ 'user_id' => 'V1:uname:DEVCENTER:AA', // 替换为你的真实测试user_id 'password' => 'pwd' // 替换为你的真实测试password ]; // 正确编码方式:直接拼接user_id和password后做一次Base64编码 $base64Credentials = base64_encode($credentials['user_id'] . ':' . $credentials['password']); $curl = curl_init($saberUrl); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HTTPHEADER, [ 'Content-Type: application/x-www-form-urlencoded', 'Authorization: Basic ' . $base64Credentials ]); curl_setopt($curl, CURLOPT_POSTFIELDS, "grant_type=client_credentials"); // 添加cURL错误捕获,便于排查请求问题 if(curl_errno($curl)) { echo 'CURL Error: ' . curl_error($curl); exit; } $response = curl_exec($curl); curl_close($curl); $data = json_decode($response, true); print_r($data);
额外排查项
- 确认测试凭证的正确性:登录Sabre开发者后台,检查
user_id和password是否与配置一致,注意格式是否符合V1:<用户名>:DEVCENTER:<后缀>的要求。 - 检查网络环境:确保服务器能正常访问Sabre的API域名,无防火墙或代理拦截。
- 验证请求参数:确保
grant_type参数正确传递为client_credentials,Content-Type头设置正确。
内容的提问来源于stack exchange,提问作者Kesi Hammy
相关产品推荐
相关产品推荐

