【问题标题】:Groovy Rest ApiGroovy 休息 API
【发布时间】:2017-06-28 12:16:45
【问题描述】:

我正在使用 groovy 制作 REST API,我正在提供 Real Shop 集成 Rest Api。但是我得到了soapui和代码连接重置错误。那么你们能帮我解决这个问题吗?

真正的是getSignature方法:

private String getSignature(Map params) throws NoSuchAlgorithmException, InvalidKeyException {

    String plainText = "GET" + "\n" + params.endpointUrl + "\n" + "" + "\n" + timess
    Mac mac = Mac.getInstance("HmacSHA256")
    SecretKeySpec secretKeySpec = new SecretKeySpec(params.password.getBytes(), "HmacSHA256")
    mac.init(secretKeySpec)
    byte[] rawHmac = mac.doFinal(plainText.getBytes())
    return DatatypeConverter.printHexBinary(rawHmac)
}

https://www.real.de/api/v1/?page=rest-api

我的代码:

@Override
protected Map getCategories(Map params) {
    params.authentication.timestamp = (System.currentTimeMillis() /1000L)

    String signature = getSignature(params.authentication)
    def client = new RESTClient( params.authentication.endpointUrl )
    Map response = client.get( path : 'categories',
    headers: [
        'accept': application/json,
        'hm-client' : params.authentication.apiKey,
        'hm-signature' : signature,
        'hm-timestamp' : params.authentication.timestamp,
        //'sslTrustAllCerts': true
    ]
    )

    assert response.status == 200 // HTTP response code
    println response.getData()
    return response
}

【问题讨论】:

    标签: rest grails groovy


    【解决方案1】:

    正如我在您共享的文档中看到的 (https://www.real.de/api/v1/?page=rest-api),如果您的客户端和服务器时间可能彼此不同大约 5 分钟。如果时间超过 5 分钟,则连接将被拒绝。你能检查服务器时间和客户端时间吗?

    • 段落 “HM-Timestamp - 所有请求都必须包含一个 HM-Timestamp 标头,其中包含当前的 Unix Timestamp(以秒为单位),并且在发出请求时必须是最新的。当前时间周围有一个 10 分钟的窗口来解决时钟偏差。换句话说,你的时间戳最多可以比当前服务器时间早5分钟或晚5分钟。如果时间戳与接收请求时的当前服务器时间相差超过5分钟,则该请求将被拒绝。

    【讨论】:

    • 感谢您的回答。我们会及时发送请求。我不认为这是关于它的。你有什么别的想法吗?
    猜你喜欢
    • 2014-11-09
    • 2014-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多