Closed langfors closed 10 years ago
It could be that either the service is temporarily down or BTC-China changed their API unannounced. They had really bad docs to begin with and in fact I've never personally seen any docs. I'll log into their site and see if I can find anything.
Nope. I cannot get access to the API docs because I don't have two-factor auth enabled. If you have access and can post the docs here, then I can take a look. See screeny.
This should be the full doco; let me know if I can do anything else. Thanks again for looking at this! Cheers.
Thanks. I saw this url on your attachment: http://btcchina.org/api-trade-documentation-en
I'm getting a Exception in thread "main" javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No X509TrustManager implementation available
whenever I run any BTCChina demo class. That's strange since the cert is a GoDaddy one. You'd think it would be default in the JVM.
Given that and my lack of free time ATM, I don't have time to look into your issue. I apologize for that.
I'd be happy to accept a PR and answer any questions if you have them.
Has anyone been able to verify / solve this problem? I seem to be having the same issue. I'm developing for iOS and have verified that my auth is correct (by checking hash / tonce / poststring etc with demo code which works) but still i keep getting "401 Unauthorized" errors. Please help :)
I did have some problems accessing BTCChina a few weeks back; after I changed the url to https://vip.btcchina.com, it was OK:
Exchange exc = ExchangeFactory.INSTANCE.createExchange(BTCChinaExchange.class.getCanonicalName());
final ExchangeSpecification spec = exc.getExchangeSpecification();
spec.setSslUri("https://vip.btcchina.com");
exc.applySpecification(spec);
BTW @timmolter , I think the current way to override default exchange settings could be improved to make it less error-prone and more user-friendly. I don't have time to do anything about it right now though.
Thanks for your reply mmazi! Unfortunately it did not help me since i'm still getting 401s. I know this has nothing to do with XChange since this is iOS code, but i've been stuck on this for two weeks and getting frustrated. That being said XChange is an awesome library, i often check to see the newest APIs :)
Hopefully someone can spot an error in my approach below: Thanks again for any help!
(i have checked the tonce, access key and Basic Auth header with demo code that works, they are OK)
postFields: (
"tonce=1384187641746740",
"accesskey=22ad7998-f149-44b9-a5d2-e90c9b13757f",
"requestmethod=post",
"id=1",
"method=getAccountInfo",
"params="
)
headers: {
Authorization = "Basic MjJhZDc5OTgtZjE0OS00NGI5LWE1ZDItZTkwYzliMTM3NTdmOjE4ZmYzZDllNjM2YjQ0OWYwMGM2MjQwOWYwYjFmYTVmM2YyZmYyOTI=";
"Json-Rpc-Tonce" = 1384187641746740;
}
resultstring: 401 Unauthorized
I recently added the vip subdomain, and pushed the change. Regarding the 401 error, I cannot give any feedback on that as I haven't tried to use any of the authenticated API calls to BTC-China. I have been successful at using the market data API calls though. Of course, there is no authorization required for that, so it's a different ball game.
All I can recommend is that you either contact BTCChina for help or try to contact the original author who wrote our BTC-China code, and see if he knows how to get it to work.
@mmazi When you have some time, let me know what your idea is, and I'll implement it. Thanks as always!
I was able to work around this by using the python example given in the API docs.
The server seems to accept this format:
result = bc.buy("500.0","0.001")
*Let me clarify my results: I get the error message for no apparent when calling sellOrder and buyOrder using the PHP example but both work correctly when using the Python example.
For what its worth; the BTCCPollingAccountService.getAccountInfo() calls; which is an authenticated call, works just fine.
On Tue, Nov 12, 2013 at 7:37 AM, devinbethel notifications@github.comwrote:
I was able to work around this by using the python example given in the API docs.
— Reply to this email directly or view it on GitHubhttps://github.com/timmolter/XChange/issues/191#issuecomment-28289791 .
Is the python example provided by official works? I doubt. It takes me long time to test, and I make all the efforts to make the request the same as the php one, but php one works, python one didn't.
Hi all, I had the similar issue. I was using the official PHP code to get the account info which works fine. But when I try to change the code to either sellOrder or buyOrder I got 401 error. For the code I changed the value of the var_dump function to 'buyOrder', array(2630, 0.1) . I also changed http_build_query() and json_code() function
'params' => array("price" => $params[0],"amount" => $params[1],),
Im not sure if this is the right way of assigning the values for 'params'. Is there any other place I should have changed? .
Thank you for any suggestion.
Cheers
Did you try adding params to signature function?. The example code in PHP has comment the $params var.
The problem is that http_build_query substitutes the comma which delimits the items in the method parameter..
Check https://gist.github.com/mkraemer/7483878#file-btcchina-api for a working example :)
@3lm4dn0 @mkraemer Thank you for your kind reply and the example. I really would like to have my code work if possible (it's easier for me to understand and modify). So now I changed the code as following and left the rest the same as the demo.
$signature = http_build_query(array(
'tonce' => $ts,
'accesskey' => $accessKey,
'requestmethod' => 'post',
'id' => 1,
'method' => $method,
'params' => implode(',', $params),
));
in try()
try {
var_dump(request('buyOrder', [2700,0.1]));
} catch (Exception $e) {
echo "Error:".$e->getMessage();
}
But it still does not work :(.
Any suggestion?
Thank you so much.
Cheers.
@mkraemer Update. I replaced the http_build_query with your sprintf function and it works now! :D Thanks so much!
Regards
@foolooo I have same problem with you. can you give me full code. thank you very much :)
Hi @andyabc123 , I put @mkraemer 's functions into the demo code. Here is the buyOrder code
<?php
function sign($method, $params = array()){
$accessKey = "replace-this";
$secretKey = "replace-this";
$mt = explode(' ', microtime());
$ts = $mt[1] . substr($mt[0], 2, 6);
$signature = sprintf(
'tonce=%s&accesskey=%s&requestmethod=post&id=%s&method=%s¶ms=%s',
$ts,
$accessKey,
1, //id number
$method,
implode(',', $params)
);
var_dump($signature);
$hash = hash_hmac('sha1', $signature, $secretKey);
return array(
'ts' => $ts,
'hash' => $hash,
'auth' => base64_encode($accessKey.':'. $hash),
);
}
function request($method, $params){
$sign = sign($method, $params);
$options = array(
CURLOPT_HTTPHEADER => array(
'Authorization: Basic ' . $sign['auth'],
'Json-Rpc-Tonce: ' . $sign['ts'],
),
);
$postData = json_encode(array(
'method' => $method,
'params' => $params,
'id' => 1,
));
//echo "this is postdata ="; //probe alert added
print($postData);
$headers = array(
'Authorization: Basic ' . $sign['auth'],
'Json-Rpc-Tonce: ' . $sign['ts'],
);
//echo "\n"."headers = ".$headers."\n"; // probe added
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT,
'Mozilla/4.0 (compatible; BTC China Trade Bot; '.php_uname('a').'; PHP/'.phpversion().')'
);
curl_setopt($ch, CURLOPT_URL, 'https://api.btcchina.com/api_trade_v1.php');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
//echo "\n"."postData = ".$postData."\n"; // probe added
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
// run the query
$res = curl_exec($ch);
//echo "base64 = ".$sign['auth']."\n"; // probe added
return $res;
/**/
}
try {
var_dump(request('buyOrder', [5355.660000,0.000000]));
} catch (Exception $e) {
echo "Error:".$e->getMessage();
}
?>
You will need to replace the accessKey and the secretKey, and the price and amount.
Hope it helps.
Regards
Thank you very much,it's work now;)
Andy
ÔÚ 2013Äê11ÔÂ25ÈÕ£¬6:22£¬Bo notifications@github.com дµÀ£º
Hi @andyabc123 , I put @mkraemer 's functions into the demo code. Here is the buyOrder code
<?php function sign($method, $params = array()){
$accessKey = "replace-this"; $secretKey = "replace-this"; $mt = explode(' ', microtime()); $ts = $mt[1] . substr($mt[0], 2, 6); $signature = sprintf( 'tonce=%s&accesskey=%s&requestmethod=post&id=%s&method=%s¶ms=%s', $ts, $accessKey, 1, //id number $method, implode(',', $params) ); var_dump($signature); $hash = hash_hmac('sha1', $signature, $secretKey); return array( 'ts' => $ts, 'hash' => $hash, 'auth' => base64_encode($accessKey.':'. $hash), );
}
function request($method, $params){ $sign = sign($method, $params);
$options = array( CURLOPT_HTTPHEADER => array( 'Authorization: Basic ' . $sign['auth'], 'Json-Rpc-Tonce: ' . $sign['ts'], ), ); $postData = json_encode(array( 'method' => $method, 'params' => $params, 'id' => 1, )); //echo "this is postdata ="; //probe alert added print($postData); $headers = array( 'Authorization: Basic ' . $sign['auth'], 'Json-Rpc-Tonce: ' . $sign['ts'], ); //echo "\n"."headers = ".$headers."\n"; // probe added $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; BTC China Trade Bot; '.php_uname('a').'; PHP/'.phpversion().')' ); curl_setopt($ch, CURLOPT_URL, 'https://api.btcchina.com/api_trade_v1.php'); curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); //echo "\n"."postData = ".$postData."\n"; // probe added curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // run the query $res = curl_exec($ch); //echo "base64 = ".$sign['auth']."\n"; // probe added return $res; /**/
}
try { var_dump(request('buyOrder', [5355.660000,0.000000])); } catch (Exception $e) { echo "Error:".$e->getMessage(); }
?>
You will need to replace the accessKey and the secretKey, and the price and amount.
Hope it helps.
Regards
¡ª Reply to this email directly or view it on GitHub.
Faced 401 error yesterday on Android. The core of problem was in BTCChinaDigest (digestParams): It waited for requestJson string format: {"id":1,"method":"getAccountInfo","params":[]} But actually got: {"params":[],"method":"getAccountInfo","id":1} So string didn't match Pattern.compile("{\"id\":([0-9]),\"method\":\"([^\"])\",\"params\":[([^]]*)]}", ... And we got wrong signature and authentication problem.
Hello,
Firstly, thanks for an incredible library! Love it!
I am having trouble using the new BTCChina support. I always get a 401; even when using the BTCChinaTradeDemo example. Any advice or help appreciated. Here is the trace output: