how to rewrite php api request to python?

199 Views Asked by At

I have this php code for calling API, I'm trying to rewrite it into python and I seem I can't get the accesskey configuration right.

Php code:

$privateKey = 'privateKey';
$publicKey = 'publicKey';

$parameters = array('status' => 'upcoming');
$postData = json_encode($parameters);
$accessKey = base64_encode(hash_hmac('sha256', $postData, $privateKey, true));

$ch = curl_init('https://foundico.com/api/v1/icos/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
      'Content-Type: application/json',
      'X-Foundico-Public-Key: '.$publicKey,
      'X-Foundico-Access-Key: '.$accessKey
]);

$response = curl_exec($ch);
curl_close($ch);
echo $response;enter code here

Python code:

import requests.auth
import json
import urllib
import hashlib
import base64
import hmac

public_key = 'mypublickey'
private_key = 'myprivatekey'

parameters = {'status':'upcoming'}
postData=json.dumps(parameters)


accessKey1= hmac.new(private_key.encode("utf8"), postData.encode("utf8"), hashlib.sha256)
accessKey = base64.b64encode(accessKey1.hexdigest().encode('utf8')).decode('utf8')


print(accessKey)
url = 'https://foundico.com/api/v1/icos/'
headers = {'Content-Type': 'application/json',
          'X-Foundico-Public-Key': public_key,
          'X-Foundico-Access-Key': accessKey}
r=requests.post(url,
                #headers=headers,
                #data=postData)
print(r.json()))

Error: {'error': 480, 'message': 'Private or public key is not valid'}

3

There are 3 best solutions below

0
A. Klimov On

In PHP you seem to base64_encode() a binary string returned from hash_hmac() as the fourth parameter to hash_hmac() is true.

However in Python you seem to base64.b64encode() a accessKey1.hexdigest(), i.e. a hex-ified binary string.

You should have a look which methods accessKey1 provides to get a non-hex digest.

2
Pascal Tovohery On

This method is to use http.client library in python Make sure that Certificates.command is installed.

import requests.auth
import json
import http.client
import urllib
import hashlib
import base64
import hmac

public_key = 'mypublickey'
private_key = 'myprivatekey'

parameters = {'status':'upcoming'}
postData=json.dumps(parameters)


accessKey1= hmac.new(private_key.encode("utf8"), postData.encode("utf8"), hashlib.sha256)
accessKey = base64.b64encode(accessKey1.hexdigest().encode('utf8')).decode('utf8')


print(accessKey)
url = 'https://foundico.com'
headers = {'Content-Type': 'application/json',
          'X-Foundico-Public-Key': public_key,
          'X-Foundico-Access-Key': accessKey}
conn = http.client.HTTPSConnection(url)
conn.request('POST', '/api/v1/icos', json_data, headers)

response = conn.getresponse()
print(response.read().decode())
1
Aakash Sharma On
from requests import request
from base64 import b64encode
import hmac
from hashlib import (
    sha256
)
import json

private_key = "private_key".encode()
public_key = "public_key"

post_data = {
    "status": "upcomming"
}

accessKey = b64encode(
    hmac.new(private_key, json.dumps(post_data).encode(), sha256).digest()
).decode()

response = request(
    'POST',
    'https://foundico.com/api/v1/icos/',
    json=post_data,
    headers = {
        'Content-Type': 'application/json',
        'X-Foundico-Public-Key': public_key,
        'X-Foundico-Access-Key': accessKey
    },
    verify=True
)
print(response.json())