Title: 401 Unauthorized on BTCChina, PHP code Post by: Dirk83 on November 13, 2013, 05:25:41 PM Hi,
For BTCChina I use the provided PHP code, but I always get a 401 Unauthorized error. Anyone know how to fix this problem? Thanks, Dirk --- This is the code: <?php function sign($method, $params = array()){ $accessKey = "YOUR_ACCESS_KEY"; $secretKey = "YOUR_SECRET_KEY"; $mt = explode(' ', microtime()); $ts = $mt[1] . substr($mt[0], 2, 6); $signature = http_build_query(array( 'tonce' => $ts, 'accesskey' => $accessKey, 'requestmethod' => 'post', 'id' => 1, 'method' => $method, 'params' => '', //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, )); print($postData); $headers = array( 'Authorization: Basic ' . $sign['auth'], 'Json-Rpc-Tonce: ' . $sign['ts'], ); $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); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // run the query $res = curl_exec($ch); return $res; /**/ } try { var_dump(request('getAccountInfo', array())); } catch (Exception $e) { echo "Error:".$e->getMessage(); } ?> Title: Re: 401 Unauthorized on BTCChina, PHP code Post by: Mushai on November 14, 2013, 09:45:26 AM Code: $accessKey = "YOUR_ACCESS_KEY"; You replaced your key and secret I presume? Title: Re: 401 Unauthorized on BTCChina, PHP code Post by: Dirk83 on November 14, 2013, 03:24:55 PM Code: $accessKey = "YOUR_ACCESS_KEY"; You replaced your key and secret I presume? Yeah :-). Title: Re: 401 Unauthorized on BTCChina, PHP code Post by: Mushai on November 15, 2013, 04:59:09 PM Yep, their sample code is wrong.
Try this : https://gist.github.com/mkraemer/7483878#file-btcchina-api |