0

I have serious problem with changing PHP sample code to equivalent in python. Here is an example PHP code:

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);
}

Thank you in advance for any help.

UPDATE:

My code is:

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

Result: {"error":501,"errorMsg":"Invalid API key","time":1486049060}

5
  • Yesterday I've spend few hours trying to send correct request via python requests and urllib2 now i've deleted whole code because it was NOT working at all. Every request was finished with wrong auth message. If someone just could let me know how should post data look like. I don't know PHP so it's hard for me to figure it out. Commented Feb 2, 2017 at 11:47
  • Have you tried Google: "Post data with python"? Commented Feb 2, 2017 at 11:50
  • Yep , I think I'ts all about im sending just wrong data. apiurl = "bitmarket.pl/api2" apidata = hmac.new("info", secret_key_here, hashlib.sha512).hexdigest() getapi = requests.post(apiurl, data=apidata) Commented Feb 2, 2017 at 11:51
  • So give it a shot again and if you get stuck, come back and show us what you've tried (update your question with the code, don't post code in comments) and we can help you from there. Right now, you're basically asking us to do your job for you... Commented Feb 2, 2017 at 11:52
  • I gave already , Im just suck in this form yesterday and now im fighting with it since 7 AM ... If i just knew what is result of that php code. Thanks anyway for your responses. Commented Feb 2, 2017 at 11:55

2 Answers 2

1

The solution is:

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
Sign up to request clarification or add additional context in comments.

Comments

0

Take a look at

Curl issue in subprocess Python

I point out exactly what you are asking for, other then for someone to translate PHP to Python for you (this should get you about 90% of the way there.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.