Raphael Schubert
Raphael Schubert

Reputation: 359

Consuming Blinktrade Rest API with python given error

I'm new with python. Just few hours learning it.

I'm trying to consume an REST API to get some account infos...

Here is my request:

def getAccountData():
    nonce = int(datetime.datetime.now().timestamp())
    signature = hmac.new(b'TDDh8djV3NwXt53gSrScDul6o6w3HnnZsHuh6HTF9SA', msg=nonce, digestmod=hashlib.sha256).digest()
    print(signature)
    headers = {
            'content-type': 'application/json',
            'APIKey':conf['API_KEY'],
            'Nonce':str(nonce),
        }
    data = {
            "MsgType": "U2",
            "BalanceReqID": 1
        }   
    r = requests.post('https://api.blinktrade.com/tapi/v1/message', data=data, headers=headers)
    print(r.json())

And here is the error:

Traceback (most recent call last): File "foxbit.py", line 51, in getAccountData() File "foxbit.py", line 30, in getAccountData signature = hmac.new(b'TDDh8djV3NwXt53gSrScDul6o6w3HnnZsHuh6HTF9SA', msg=nonce, digestmod=hashlib.sha256).digest() File "C:\Python\Python35\lib\hmac.py", line 144, in new return HMAC(key, msg, digestmod) File "C:\Python\Python35\lib\hmac.py", line 84, in init self.update(msg) File "C:\Python\Python35\lib\hmac.py", line 93, in update self.inner.update(msg) TypeError: object supporting the buffer API required

I'm trying consume this API: https://blinktrade.com/docs/?shell#balance on balance method

with no success.

I'm wanna create and python app to watch my opened orders of Bitcoin. What was happening with this error?? On documentation, says i need do this to work:

{
    "MsgType": "U2",
    "BalanceReqID": 1
}
message='{ "MsgType": "U2", "BalanceReqID": 1 }'

api_url='API_URL_REST_ENDPOINT'
api_key='YOUR_API_KEY_GENERATED_IN_API_MODULE'
api_secret='YOUR_SECRET_KEY_GENERATED_IN_API_MODULE'

nonce=`date +%s`
signature=`echo -n "$nonce" | openssl dgst -sha256 -hex -hmac "$api_secret" | cut -d ' ' -f 2`

curl -X POST "$api_url"              \
  -H "APIKey:$api_key"               \
  -H "Nonce:$nonce"                  \
  -H "Signature:$signature"          \
  -H "Content-Type:application/json" \
  -d "$message"

3 hours trying it and nothing! hehe I need some help here.

Upvotes: 0

Views: 256

Answers (1)

FXCesinha
FXCesinha

Reputation: 403

The main problem is that you are not passing the nonce as a bytearray format when generating the signature.

This is a change after python 3.4

hmac now accepts bytearray as well as bytes for the key argument to the new() function, and the msg parameter to both the new() function and the update() method now accepts any type supported by the hashlib module. (Contributed by Jonas Borgström in bpo-18240.)

https://docs.python.org/3/whatsnew/3.4.html#hmac

Passing a bytearray on nonce should work as expected.

signature = hmac.new(b'SECRET', msg=bytearray(nonce), digestmod=hashlib.sha256).digest()

Also, you are forgetting the Signature header.

You can follow this gist that has very useful python examples, it's actually built on python2, but you still can follow it, and it's very easy to get started. https://gist.github.com/pinhopro/60b1fd213b36d576505e

As a BlinkTrade employee, don't hesite to ask me anything.

Upvotes: 2

Related Questions