经过身份验证的Java Jersey REST调用Bitstamp [英] Authenticated Java Jersey REST call to Bitstamp

查看:125
本文介绍了经过身份验证的Java Jersey REST调用Bitstamp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对Bitstamp进行私有REST调用(请参阅 https://www.bitstamp.net/ api / )。

I am trying to make a private REST call to Bitstamp (see https://www.bitstamp.net/api/).

然而,我得到以下共鸣:

However, I am getting the following resonse:

{error:缺少密钥,签名和nonce参数}

在API规范中,他们编写以下内容:

In the API specification they write the following:


所有私有API调用都需要身份验证。您需要提供3
参数来验证请求:

All private API calls require authentication. You need to provide 3 parameters to authenticate a request:


  • API密钥

  • Nonce

  • 签名

API KEY

要获取API密钥,请转到帐户,安全,然后API访问。
设置权限并点击生成密钥。

To get an API key, go to "Account", "Security" and then "API Access". Set permissions and click "Generate key".

NONCE

Nonce是一个常规整数。它必须随着您提出的每个
请求而增加。在这里阅读更多相关信息。示例:如果您在第一个请求中将nonce
设置为1,则必须在
秒请求中将其设置为至少2。您不需要从1开始。常见的
做法是对该参数使用unix时间。

Nonce is a regular integer number. It must be increasing with every request you make. Read more about it here. Example: if you set nonce to 1 in your first request, you must set it to at least 2 in your second request. You are not required to start with 1. A common practice is to use unix time for that parameter.

SIGNATURE

SIGNATURE

签名是HMAC-SHA256编码的消息,包含:nonce,client
ID和API密钥。必须使用通过API密钥生成的秘密
密钥生成HMAC-SHA256代码。此代码必须转换为
到它的十六进制表示(64个大写字符)。

Signature is a HMAC-SHA256 encoded message containing: nonce, client ID and API key. The HMAC-SHA256 code must be generated using a secret key that was generated with your API key. This code must be converted to it's hexadecimal representation (64 uppercase characters).

我一直试图解决此问题一段时间,一切似乎都井然有序:使用加密创建签名并转换为HEX,在服务器上激活API访问,API密钥正在按顺序等等。当联系Bitstamp时,他们告诉我API正常工作,我收到的错误通常发生在我的密钥,签名和参数未达到其API时。在坚持了一段时间之后,我现在还没有找到解决这个问题的线索。请帮忙。

I have been trying to solve this for some time and everything seems to be in order: the creation of the signature using encryption and convertion to HEX, API access being activated on the server, API keys being in order etc. When contacting Bitstamp they tell me that the API is working properly and that the error I receive usually happens when my key, signature and parameters aren't reaching their API. After being stuck with this for some time now I have no clues left on how to solve this. Please help.

我的代码如下

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import org.codehaus.jettison.json.JSONObject;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.DatatypeConverter;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException


public class Connection {

private static WebResource baseResource;

private static final MediaType responseType = MediaType.APPLICATION_JSON_TYPE;


public Connection() throws NoSuchAlgorithmException {

    Client client = Client.create();
    baseResource = client.resource("https://www.bitstamp.net/api/");

}


public void test() throws NoSuchAlgorithmException, InvalidKeyException {

    String nonce_unixTime = String.valueOf(System.currentTimeMillis() / 1000L);
    String clientID = "xxx";
    String key = "yyy";
    String secret = "zzz";
    String message = nonce_unixTime + clientID + key;

    Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
    SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
    sha256_HMAC.init(secret_key);
    byte[] hash = sha256_HMAC.doFinal(message.getBytes());
    String signature = DatatypeConverter.printHexBinary(hash).toUpperCase();

    // Fetch the resource.
    JSONObject json = baseResource.path("balance/")
            .queryParam("key", key)
            .queryParam("signature", signature)
            .queryParam("nonce", nonce_unixTime)
            .accept(responseType).post(JSONObject.class);

    System.out.println(json.toString());
}
}


推荐答案

这是因为bitstamp在POST的正文中等待params而不是url中的params,所以POST消息的类型必须是MediaType.APPLICATION_FORM_URLENCODED而不是JSONObject.class。

It's because bitstamp waiting for params in POST's body instead of params in url, so the type of POST-message must be MediaType.APPLICATION_FORM_URLENCODED instead of JSONObject.class.

参见如何在HTTP POST请求中发送参数?

如何在泽西岛进行:使用Jersey客户端进行POST操作

这篇关于经过身份验证的Java Jersey REST调用Bitstamp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆