Java Sample Code - Invalid Session - Error Code 2000 Response
phamsarah opened this issue · 17 comments
Hello, I don't think the java sample code is correct. After resolving the JsonObject exception, I used the same key, secret, and org id values that I was getting a response for on the Nicehash API website. With those same values, the java sample code returns:
`INFO: Accounts Object: {"error_id":"36b23e04-77a7-49e6-880a-df693eb15786","errors":[{"code":2000,"message":"Invalid session"}]}
Im thinking the sha256 and all the signing is wrong.
Keep in mind that there are two api servers: api-test and api2 and you need to use a separately created api key for each. This was my problem. This is the site to create a test key: https://test.nicehash.com/
@makeSmartio Yep, I replaced 'URL_ROOT' with "https://api2.nicehash.com/. Were you able to get the Java demo to work?
I have not tried - I am using the PHP code. What do your headers look like?
@makeSmartio The headers in the Java sample code are:
String nonce = UUID.randomUUID().toString();
String digest = Api.hashBySegments(this.apiSecret, this.apiKey, time, nonce, this.orgId, request.getMethod(), request.getURI().getPath(), request.getURI().getQuery(), null);
request.setHeader("X-Time", time);
request.setHeader("X-Nonce", nonce);
request.setHeader("X-Auth", this.apiKey+":"+digest);
request.setHeader("X-Organization-Id", this.orgId);
And these were my values I got:
X-Time = 1649858859704
X-Nonce = 2630df9c-4348-446f-b7d9-1ea460f8c313
X-Auth = [API-KEY]:[DIGEST]
X-OrgID = [Value that is correct]
And I know that my X-Auth and X-OrgID are correct because I've put those values into the Nicehash docs: https://www.nicehash.com/docs/rest and I got a 200 response code with output from the Accounts call
This is the GET request:
Request: GET https://api2.nicehash.com/main/api/v2/accounting/accounts2 HTTP/1.1
This looks correct to me. I am not expert enough to help further!
Use this values as reference:
content: key + "\x00" + time + "\x00" + nonce + "\x00\x00" + org + "\x00\x00" + reqMeth + "\x00" + path + "\x00" + query
input: 5d02e683-d864-481a-b548-83a91d64370116499302276929bdf3ef9-af54-450c-f7f5-3a8aae2b4faf9e376d1e-b83c-42cb-80df-6bb5b20b9942GET/main/api/v2/poolssize=1000
result: 95c97002ba145b0a074e785bf81cc7021e92a6c932c5af4224e6a8aaf939203c
header params:
X-Time: 1649930227692
X-Nonce: 9bdf3ef9-af54-450c-f7f5-3a8aae2b4faf
X-Auth: 5d02e683-d864-481a-b548-83a91d643701:95c97002ba145b0a074e785bf81cc7021e92a6c932c5af4224e6a8aaf939203c
X-Organization-Id: 9e376d1e-b83c-42cb-80df-6bb5b20b9942
Had the same issue. I noticed that I had double slashes in request URI. That solved invalid session problem :)
@kamilpajak Were you able to get the Java sample code to work?
code seems to be working .. there was indeed problem with fetching total balance ... and it has been fixed - however error 2000 usually indicates problem with api key or using production credentials on test env or vice-versa ...
➜ java git:(master) ✗ java -classpath /Users/bl4z/Documents/Code/tmp/rest-clients-demo/java/target/classes:/Users/bl4z/.m2/repository/org/apache/httpcomponents/httpclient/4.5.13/httpclient-4.5.13.jar:/Users/bl4z/.m2/repository/org/apache/httpcomponents/httpcore/4.4.13/httpcore-4.4.13.jar:/Users/bl4z/.m2/repository/commons-logging/commons-logging/1.2/commons-logging-1.2.jar:/Users/bl4z/.m2/repository/commons-codec/commons-codec/1.11/commons-codec-1.11.jar:/Users/bl4z/.m2/repository/com/google/code/gson/gson/2.8.5/gson-2.8.5.jar Hpo
Apr 30, 2022 8:58:18 AM Hpo <init>
INFO: server time: 1651301898987
Apr 30, 2022 8:58:19 AM Hpo <init>
INFO: algo settings: {"algorithm":"X16R","title":"X16R","enabled":true,"order":33,"displayMiningFactor":"MH","miningFactor":"1000000.00000000","displayMarketFactor":"TH","marketFactor":"1000000000000.00000000","minimalOrderAmount":"0.00100000","minSpeedLimit":"0.00010000","maxSpeedLimit":"100.00000000","priceDownStep":"-0.00100000","minimalPoolDifficulty":"8.00000000","port":3366,"color":"#AAAAAA","ordersEnabled":true,"enabledMarkets":"EU","displayPriceFactor":"TH","priceFactor":"1000000000000.00000000"}
Apr 30, 2022 8:58:19 AM Hpo <init>
INFO: balance: 36.39857830TBTC
...
@bl4z Can you please try the java sample code I uploaded to this repo? https://github.com/phamsarah/NiceHash/tree/main
Can you please input into Exch.java and run that and this will confirm whether or not the Java sample code works. I didn't change API.java.
By the way, I used the correct production environment with correct production credentials. The same credentials I put into the nicehash api website that returned the correct response.
- Update, deleting this comment
@bl4z Idk what was wrong, but .... I created a brand new API key and used those instead, which had the same exact permissions as the last key. But somehow this key worked and the other didn't..... even though these keys were set to never expire. Thank you for your help!
np np -- keep up good work