繁体   English   中英

使用Web API时出现401授权要求错误

[英]401 Authorization Required Error in Using Web API

我是使用Web-API的新手。有一个Web-API,通过它我可以以Json格式获取响应。我在jsp usinh jquery Ajax中使用此API。 这是web-API的结构。

http://pnrbuddy.com/api/check_pnr/pnr/<10 digit PNR number>/format/<xml|json> / pbapikey / <your public api key> / pbapisign(private key) / <HMAC signature> 

请查看API参数名称及其含义。

我试图通过传递值作为使用此。

http://pnrbuddy.com/api/check_pnr/pnr/1234567890/format/json/pbapikey/c70a600b160e9a1fd4c6aa772e63e612/pbapisign/71988fc2bacfe995e96e65c1cbfed3dc2ba613c8

Buu执行时出现错误,如..

401 Authorization Required.

并在响应firebug.this消息。

Reload the page to get source for: http://pnrbuddy.com/api/check_pnr/pnr/1234567890/format/json/pbapikey/c70a600b160e9a1fd4c6aa772e63e612/pbapisign/71988fc2bacfe995e96e65c1cbfed3dc2ba613c8

如何解决这个问题。

我的HMAC生成代码。

            String mykey = "Private Key";
    String test = "pnrjson6b17f33e25e2d8197462d1c6bcb0b130";
    try {
        Mac mac = Mac.getInstance("HmacSHA1");
        SecretKeySpec secret = new SecretKeySpec(mykey.getBytes(),
                "HmacSHA1");
        mac.init(secret);
        byte[] digest = mac.doFinal(test.getBytes());
        for (byte b : digest) {
            System.out.format("%02x", b);
        }
        System.out.println();
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

我的新API URL值。

http://pnrbuddy.com/api/check_pnr/pnr/1234567890/format/json/pbapikey/6b17f33e25e2d8197462d1c6bcb0b130/pbapisign/ebb4279d9b8cc3e011a7d7a46248c4ac456a6ca3

我仍然收到相同的授权错误。 为什么?

这是我的客户端代码。

<script type="text/javascript">
        $(document).ready(function () {
            $("#click").click(function () {

                $.ajax({
                    type: "POST",
                    url: "http://pnrbuddy.com/api/check_pnr/pnr/1234567890/format/json/pbapikey/6b17f33e25e2d8197462d1c6bcb0b130/pbapisign/8dd7f570509ab9b7d27cc182982d49ca63fb45d6",
                    contentType: "application/json",
                    async: false,
                    success: function (data) {
                        $("#response").html(data.d);
                    }

                });
            });
        });
    </script>


</head>
<body>
    <button id="click">GetPNR Status</button>
    <div id="response"></div>
</body>

首先,我认为您需要在pnrbuddy中注册为开发人员(步骤在我的评论中)。 您需要使用自己的密钥来访问api。

然后参考此: http : //pnrbuddy.com/hauth/api?page=keys

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM