gpt4 book ai didi

java - GDAX API 错误请求 400

转载 作者:行者123 更新时间:2023-11-30 00:10:18 25 4
gpt4 key购买 nike

我在签署 GDAX API 消息时遇到问题。 GDAX API 文档指出状态 400 ~“错误请求 – 请求格式无效”。你能告诉我我需要更改什么吗?

谢谢!


protected Void doInBackground(String... params) {

try {
URL url = new URL(baseUrl+params[0]);

// Create the urlConnection
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();


urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);

urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("CB-ACCESS-KEY", key);
urlConnection.setRequestProperty("CB-ACCESS-SIGN", generate(params[0], "GET", "", String
.valueOf(System.currentTimeMillis())));
String timestamp = String.valueOf(System.currentTimeMillis());
urlConnection.setRequestProperty("CB-ACCESS-TIMESTAMP", timestamp);
urlConnection.setRequestProperty("CB-ACCESS-PASSPHRASE", passphrase);

urlConnection.setRequestMethod("GET");

int statusCode = urlConnection.getResponseCode();

if (statusCode == 200) {
InputStream inputStream = new BufferedInputStream(urlConnection.getInputStream());

String response = convertInputStreamToString(inputStream);
Log.d("TAG", String.valueOf(response));
} else {
Log.d("TAG", String.valueOf(statusCode));
}

} catch (Exception e) {
Log.d("TAG", e.getLocalizedMessage());
}
return null;
}

生成 CB-ACCESS-SIGN

public String generate(String requestPath, String method, String body, String timestamp) {
try {
String prehash = timestamp + method.toUpperCase() + requestPath + body;
byte[] secretDecoded = Base64.decode(secretKey, Base64.DEFAULT);

SecretKeySpec keyspec = new SecretKeySpec(secretDecoded, "HmacSHA256");
Mac sha256 = GDAXConstants.SHARED_MAC;
sha256.init(keyspec);
String shadone = Base64.encodeToString(sha256.doFinal(prehash.getBytes()),Base64.DEFAULT);
return shadone;
} catch (InvalidKeyException e) {
e.printStackTrace();
throw new RuntimeException(new Error("Cannot set up authentication headers."));
}
}

最佳答案

对于 C# 代码,我遇到了同样的问题。问题是我没有添加用户代理 header 。当我这样做时,一切都很完美。我的工作代码是:

HttpClient gdaxClient = new HttpClient();
gdaxClient.DefaultRequestHeaders.Add("User-Agent", ".NET Framework Test Client");

您的 java 代码将是:

urlConnection.setRequestProperty("User-Agent", "Java Test Client");

关于java - GDAX API 错误请求 400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48247177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com