Author

Topic: 401 Unauthorized on BTCChina, PHP code (Read 1466 times)

newbie
Activity: 21
Merit: 6
November 15, 2013, 12:59:09 PM
#4
Yep, their sample code is wrong.
Try this : https://gist.github.com/mkraemer/7483878#file-btcchina-api
newbie
Activity: 33
Merit: 0
November 14, 2013, 11:24:55 AM
#3
Code:
        $accessKey = "YOUR_ACCESS_KEY";
        $secretKey = "YOUR_SECRET_KEY";

You replaced your key and secret I presume?

Yeah :-).
newbie
Activity: 21
Merit: 6
November 14, 2013, 05:45:26 AM
#2
Code:
        $accessKey = "YOUR_ACCESS_KEY";
        $secretKey = "YOUR_SECRET_KEY";

You replaced your key and secret I presume?
newbie
Activity: 33
Merit: 0
November 13, 2013, 01:25:41 PM
#1
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:

  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();
}
?>
Jump to: