2017-02-02 13 views
0

PHPのサンプルコードをPythonで同等のものに変更することに深刻な問題があります。 PHPコードの例を次に示します。Bitmarket.pl API PHPをPythonに翻訳する

function bitmarket_api($method, $params = array()) 

{ 
    $key = "klucz_jawny"; 
    $secret = "klucz_tajny"; 

    $params["method"] = $method; 
    $params["tonce"] = time(); 

    $post = http_build_query($params, "", "&"); 
    $sign = hash_hmac("sha512", $post, $secret); 
    $headers = array(
     "API-Key: " . $key, 
     "API-Hash: " . $sign, 
    ); 

    $curl = curl_init(); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($curl, CURLOPT_URL, "https://www.bitmarket.pl/api2/"); 
    curl_setopt($curl, CURLOPT_POST, true); 
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post); 
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); 
    $ret = curl_exec($curl); 

    return json_decode($ret); 
} 

ありがとうございました。

UPDATE:

私のコードは次のとおり

apiurl = "https://www.bitmarket.pl/api2/" 
key = "mypubkey" 
secret = "myceretkey" 

apicommand = "info" 
tonce = time.time() 

params = str(apicommand) + " " + str(tonce) 

postdata = (params + " " + "&") 

signdata = hmac.new(postdata, secret, hashlib.sha512).hexdigest() 

headerapi = { "API-Key: ": key, 
"API-Hash: " : signdata} 



getapi = requests.post(apiurl, data=headerapi ,params=postdata) 
print getapi.text 

結果:{ "エラー":501、 "errorMsg内容": "無効なAPIキー"、 "時間":1486049060}

+0

昨日、私が正しい要求を送信しようと数時間を費やしてきましたPythonのリクエストとurllib2経由で、今は全部のコードが削除されました。すべてのリクエストが間違った認証メッセージで終了しました。誰かがちょうどデータがどのようにポストされるべきかを私に知らせることができれば。私はPHPを知らないので、それを理解することは難しいです。 – MichalM

+0

Googleを使ったことがありますか?「Pythonでデータを投稿する」? –

+0

うん、私は私が間違ったデータを送ることについて私が思うのはすべてだと思う。 apiurl = "https://www.bitmarket.pl/api2/" apidata = hmac.new( "info"、secret_key_here、hashlib.sha512).hexdigest() getapi = requests.post(apiurl、data = apidata) ) – MichalM

答えて

0

あなたが他の、求めているまさに私が指摘

Curl issue in subprocess Python

を見てみましょうその後、あなたのためのPythonにPHPを翻訳するために誰かのために(これはあなたが道の約90%を取得する必要があります。)

1

解決策は以下のとおりです。

def mergeTwoDicts(x, y): 
    z = x.copy() 
    z.update(y) 
    return z 


def bitMarketPlApiCall(method, params = {}): 
    postDataAsDict = mergeTwoDicts(params, { 
     'method': method, 
     'tonce': int(time.time()) 
    }) 
    postParamsAsString = "&".join([param + '=' +  str(postDataAsDict[param]) for param in postDataAsDict]) 

    postHeaders = { 
     'API-Key': publicKey, 
     'API-Hash': hmac.new(secretKey, postParamsAsString, hashlib.sha512).hexdigest() 
} 

    request_response = requests.post('https://www.bitmarket.pl/api2/', data = postParamsAsString, headers = postHeaders) 

    return request_response.text