Bitcoin Forum
May 02, 2024, 10:35:40 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: 401 Unauthorized on BTCChina, PHP code  (Read 1459 times)
Dirk83 (OP)
Newbie
*
Offline Offline

Activity: 33
Merit: 0


View Profile
November 13, 2013, 05: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:

<?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();
}
?>
1714689340
Hero Member
*
Offline Offline

Posts: 1714689340

View Profile Personal Message (Offline)

Ignore
1714689340
Reply with quote  #2

1714689340
Report to moderator
Every time a block is mined, a certain amount of BTC (called the subsidy) is created out of thin air and given to the miner. The subsidy halves every four years and will reach 0 in about 130 years.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1714689340
Hero Member
*
Offline Offline

Posts: 1714689340

View Profile Personal Message (Offline)

Ignore
1714689340
Reply with quote  #2

1714689340
Report to moderator
Mushai
Newbie
*
Offline Offline

Activity: 16
Merit: 0


View Profile
November 14, 2013, 09:45:26 AM
 #2

Code:
        $accessKey = "YOUR_ACCESS_KEY"; 
        $secretKey = "YOUR_SECRET_KEY";

You replaced your key and secret I presume?
Dirk83 (OP)
Newbie
*
Offline Offline

Activity: 33
Merit: 0


View Profile
November 14, 2013, 03:24:55 PM
 #3

Code:
        $accessKey = "YOUR_ACCESS_KEY"; 
        $secretKey = "YOUR_SECRET_KEY";

You replaced your key and secret I presume?

Yeah :-).
Mushai
Newbie
*
Offline Offline

Activity: 16
Merit: 0


View Profile
November 15, 2013, 04:59:09 PM
 #4

Yep, their sample code is wrong.
Try this : https://gist.github.com/mkraemer/7483878#file-btcchina-api
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!