douhuiwan5141 2016-02-16 06:12
浏览 94

Bter API请求错误Python3 - 错误:无效的密钥或符号

I have been trying to get the Bter.com API working with Python3 and feel like it should work.

Currently I'm getting this error when sending the request through:

{'code': 5, 'result': 'false', 'message': 'Error: invalid key or sign'}

Below you will find the example in PHP they have on their site:

function bter_query($path, array $req = array()) {
    // API settings, add your Key and Secret at here
    $key = '';
    $secret = '';

    // generate a nonce to avoid problems with 32bits systems
    $mt = explode(' ', microtime());
    $req['nonce'] = $mt[1].substr($mt[0], 2, 6);

    // generate the POST data string
    $post_data = http_build_query($req, '', '&');
    $sign = hash_hmac('sha512', $post_data, $secret);

    // generate the extra headers
    $headers = array(
        'KEY: '.$key,
        'SIGN: '.$sign,
    );

    //!!! please set Content-Type to application/x-www-form-urlencoded if it's not the default value

    // curl handle (initialize if required)
    static $ch = null;
    if (is_null($ch)) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_USERAGENT, 
            'Mozilla/4.0 (compatible; Bter PHP bot; '.php_uname('a').'; PHP/'.phpversion().')'
            );
    }
    curl_setopt($ch, CURLOPT_URL, 'https://bter.com/api/'.$path);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

    // run the query
    $res = curl_exec($ch);

    if ($res === false) throw new Exception('Curl error: '.curl_error($ch));
    //echo $res;
    $dec = json_decode($res, true);
    if (!$dec) throw new Exception('Invalid data: '.$res);
    return $dec;
}

I have tried to replicate this as best as I could but still get the error as mentioned above.

Here is my code:

 def __nonce(self):
   if self.__wait_for_nonce: time.sleep(1)
   self.__nonce_v = str(time.time()).split('.')[0]

 def __api_call(self,method,params):
  self.__nonce()
  params['nonce'] = str(self.__nonce_v)
  params = urlencode(params)
  sign = hmac.new(self.__api_secret.encode(), params.encode(), hashlib.sha512).hexdigest()
  headers = {"Content-type": "application/x-www-form-urlencoded",
             "Key": self.__api_key,
             "Sign": sign}
  response = requests.post("https://bter.com/api/1/private/"+method, params, headers)
  data = response.json()
  return data

I'm struggling to find the error. I have contacted Bter support which have just referred me back to the API Docs. I have also tried generating a new API key and secret multiple times now and have the same error. I feel it must be in the way that I'm signing the signature in the header.

Any help would be greatly appreciated.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
    • ¥30 ppOCRLabel导出识别结果失败
    • ¥15 Centos7 / PETGEM
    • ¥15 csmar数据进行spss描述性统计分析
    • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
    • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
    • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
    • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
    • ¥15 运动想象脑电信号数据集.vhdr
    • ¥15 三因素重复测量数据R语句编写,不存在交互作用