java - 我正在尝试使用 java api 访问我的 coinbase pro 帐户,但它返回 403: Forbidden response code
问题描述
我已经交叉检查了我的公钥、密钥和密码是否正确。仍然 eclipse 使用 403 Forbidden 响应代码终止程序。但是,当我使用 cmd 运行 curl 时,它确实提供了结果。在 cmd 中运行 sysout 结果提供了来自沙盒 url 的所有帐户信息,而使用 eclipse 时并非如此。
为什么?
我正在使用来自 coinbase pro api 文档的 Java 客户端库,正式名称为 GDAX。
import com.coinbase.exchange.api.accounts.Account;
public class Testing {
private final String publicKey;
private final String passphrase;
private final String baseUrl;
private final String secret;
public Testing(String publicKey, String passphrase, String secret, String baseUrl) throws
IOException, NoSuchAlgorithmException, InvalidKeyException {
this.publicKey = publicKey;
this.passphrase = passphrase;
this.baseUrl = baseUrl;
this.secret = secret;
}
private Account[] getAccounts() throws IOException, InvalidKeyException, NoSuchAlgorithmException {
String timestamp = Instant.now().getEpochSecond() + "";
String path = "/accounts";
URL url = new URL(baseUrl + path);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestProperty("CB-ACCESS-KEY", publicKey);
con.setRequestProperty("CB-ACCESS-SIGN", signMessage(timestamp, "GET", path));
con.setRequestProperty("CB-ACCESS-TIMESTAMP", timestamp);
con.setRequestProperty("CB-ACCESS-PASSPHRASE", passphrase);
con.setRequestProperty("content-type", "application/json");
System.out.println("curl -H \"CB-ACCESS-KEY: " + publicKey+ "\" "
+ "-H \"CB-ACCESS-SIGN: " + signMessage(timestamp,"GET", path) + "\" "
+ "-H \"CB-ACCESS-TIMESTAMP: " + timestamp + "\" "
+ "-H \"CB-ACCESS-PASSPHRASE: " + passphrase + "\" " + baseUrl + path);
con.setConnectTimeout(5000);
con.setReadTimeout(5000);
int responsecode = con.getResponseCode();
String status = con.getResponseMessage();
if (responsecode == 200) {
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
in.close();
con.disconnect();
Gson gson = new Gson();
Account[] accounts = gson.fromJson(content.toString(), Account[].class);
Arrays.stream(accounts).forEach(a -> {
System.out.println("Account: " + a.getCurrency() + ", "
+ "Balance: " + a.getBalance().toPlainString() + ", "
+ "Available balance: " + a.getAvailable().toPlainString() + ", "
+ "Held: " + a.getHold().toPlainString());
});
return accounts;
} else {
throw new RuntimeException("Something went wrong. Response from server: " + status);
}
}
private String signMessage(String timestamp, String method, String path) throws NoSuchAlgorithmException, InvalidKeyException {
String prehash = timestamp + method + path;
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
byte[] secretDecoded = Base64.getDecoder().decode(secret);
SecretKeySpec secret_key = new SecretKeySpec(secretDecoded, "HmacSHA256");
sha256_HMAC.init(secret_key);
return Base64.getEncoder().encodeToString(sha256_HMAC.doFinal(prehash.getBytes()));
}
public static void main(String[] args) {
Testing t = null;
try {
t = new Testing("api key",
"passphrase",
"secret key",
"https://api-public.sandbox.pro.coinbase.com");
t.getAccounts();
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
}
}
}
解决方案
推荐阅读
- kubernetes - 用于重新部署的 Terraform ConfigMap 哈希
- url - 从 URL 填写表格 - postget 或如何?
- azure - 使用 Azure 数据工厂 V2 按 LastModifiedDate 复制新的和更改的文件
- php - 如何使用 Axios 将 ReactJs 中的表单提交到 PHP 处理程序(PHP miler 到电子邮件表单数据)?
- java - 正则表达式根据第一次出现的允许分隔符拆分过滤器字符串并获取键、运算符、值
- php - 在 Wordpress 主题中更改“加载更多”按钮的最佳方法是什么?
- swift - 在 Internet 连接可用时将多个存储的数据从已保存的本地数据库发送到服务器 iOS
- r - Radial Svm 特征的非线性变换
- python - 提取三个最大值并将其放在单独的列中
- python-2.7 - 如何为 many2many 字段设置默认值