我正在使用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
}
最佳答案
正如我在您共享的文档(https://www.real.de/api/v1/?page=rest-api)中所看到的,如果您的客户端和服务器时间彼此之间可能相差大约5分钟。如果时间超过5分钟,则连接将被拒绝。您可以检查服务器时间和客户端时间吗?
“HM-Timestamp-所有请求都必须包含一个HM-Timestamp header ,其中包含当前Unix时间戳(以秒为单位),并且在发出请求时必须是当前的。当前时间周围有10分钟的窗口,以解决时钟偏移。换句话说,您的时间戳可能比当前服务器时间早5分钟或比现在晚5分钟。如果时间戳与接收请求时的当前服务器时间相差5分钟以上,则该请求将被拒绝。”
关于rest - Groovy Rest Api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44802170/