saber production api php - 需要帮助 - 由于凭据无效,身份验证失败

时间:2016-10-17 12:30:18

标签: php sabre

调用saber rest API时,我收到以下消息。

array(3) { ["access_token"]=> string(252) "my_accesstoken_was_here" ["token_type"]=> string(6) "bearer" ["expires_in"]=> int(604800) }


array(5) { ["status"]=> string(12) "NotProcessed" ["type"]=> string(10) "Validation" ["errorCode"]=> string(31) "ERR.2SG.SEC.INVALID_CREDENTIALS" ["timeStamp"]=> string(29) "2016-10-17T07:06:40.053-05:00" ["message"]=> string(48) "Authentication failed due to invalid credentials" } 

我按照此处的军刀文档:https://developer.sabre.com/docs/read/rest_basics/authentication

我不知道为什么我收到无效的凭据消息,因为我正在使用API​​调用提供给军刀的访问令牌。

以下是我的代码。有什么问题?

<?php

//  get access token 
$client_id= base64_encode("V1:user:group:AA");
$client_secret = base64_encode("my_password");
$token = base64_encode($client_id.":".$client_secret);

$data='grant_type=client_credentials';


    $headers = array(
        'Authorization: Basic '.$token,
        'Accept: */*',
        'Content-Type: application/x-www-form-urlencoded'
    );

    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,"https://api.sabre.com/v2/auth/token");
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch,CURLOPT_POST,1);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $res = curl_exec($ch);
    curl_close($ch);
    var_dump($resf = json_decode($res,1));
    $access_token = $resf['access_token']; // token provided from sabre
    $token_type = $resf['token_type'];
    $expires_in_seconds = $resf['expires_in'];

    // //  END get access token 

    // now to get api data using the provided access token
    $url = 'https://api.test.sabre.com/v1/shop/flights/fares?origin=jfk&destination=lax&lengthofstay=5&pointofsalecountry=US';
    $headers2 = array(
    'Authorization: bearer '.$access_token,
    'protocol: HTTP 1.1 '
     );

    echo "<br><br>";
    $ch2 = curl_init();
    curl_setopt($ch2,CURLOPT_URL,$url);
    curl_setopt($ch2,CURLOPT_HTTPHEADER,$headers2);
    curl_setopt($ch2,CURLOPT_RETURNTRANSFER,1);
    var_dump(json_decode(curl_exec($ch2),1));
    curl_close($ch2);


?>

1 个答案:

答案 0 :(得分:2)

问题是您正在请求PROD访问令牌(api.sabre.com),但是将后续调用发送到测试环境(api.test.sabre.com)。请记住,PROD和TEST凭据和令牌不可互换。

如果您将第二个电话发送到api.sabre.com,您应该没问题。

希望这会有所帮助。 布鲁诺。