Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send key/secret pair with Poloniex API?

Tags:

python

api

I'm trying to write a simple script to validate that I am making an API call correctly, and then I plan to build out a more complex program from there. The error response I am receiving is:

{"error":"Invalid API key\/secret pair."}

The API documentation for what I'm working on can be found at:

https://poloniex.com/support/api/

I've just been incrementing the nonce manually to keep things simple.

My code is:

import urllib
import urllib2
import json
import time
import hmac,hashlib

APIKey = "<my_API_key>"
Secret = "<my_secret>"
post_request
"command=returnBalances"
sign = hmac.new(Secret, post_request, hashlib.sha512).hexdigest()

ret = urllib2.urlopen(urllib2.Request("https://poloniex.com/tradingApi?   key=" + APIKey + "&sign=" + sign + "&nonce=0008"))

print ret.read()
like image 568
user97315 Avatar asked Feb 11 '16 13:02

user97315


People also ask

How do I get my API key and secret key?

To obtain a new API Key and API Secret, log in to your SendSafely account and go to the Edit Profile page. From there you should see an API Access Keys section that allows you to manage your API Keys.

What is an API key pair?

Usually, the API key is a single token that's used to access the REST API. In the computing world, a token is an object that represents the right to perform an operation. By putting two and two together, we can infer that an API key is a code that gives us the right to access an API.


2 Answers

Assuming that your APIKey and Secret are OK, this following version will works:

import urllib
import urllib2
import json
import time
import hmac,hashlib


req={}

APIKey = "<my_API_key>"
Secret = "<my_secret>"

command="returnBalances"

req['command'] = command

req['nonce'] = int(time.time()*1000)
post_data = urllib.urlencode(req)

sign = hmac.new(Secret, post_data, hashlib.sha512).hexdigest()
#print sign
headers = {
    'Sign': sign,
    'Key': APIKey
}

ret = urllib2.urlopen(urllib2.Request('https://poloniex.com/tradingApi', post_data, headers))
jsonRet = json.loads(ret.read())

print jsonRet

If you run this code with your own secret and API key and it still doesn't work. That is sure that your APIKey or secret have typing error !! [ or APIkey is restricted to "withdraw only" or you have selected IP restriction and try to connect from an unknow IP.]

like image 140
A STEFANI Avatar answered Oct 09 '22 04:10

A STEFANI


The API documentation says:

All calls to the trading API are sent via HTTP POST to https://poloniex.com/tradingApi and must contain the following headers:

  • Key - Your API key.
  • Sign - The query's POST data signed by your key's "secret" according to the HMAC-SHA512 method.

Additionally, all queries must include a "nonce" POST parameter.

Even though it says "headers", I think it means POST parameters.

Also looking at the reference Python implementation linked by the docs, it appears that the API wants Key and Sign as headers, and nonce as POST parameter.

Change your request from:

urllib2.Request("https://poloniex.com/tradingApi?   key=" + APIKey + "&sign=" + sign + "&nonce=0008")

to:

data = urllib.urlencode({
    'nonce': '0008',
    # ...
    # insert here the other parameters you need
    # ...
})
headers = {
    'Key': APIKey,
    'Sign': sign,
}
urllib2.Request('https://poloniex.com/tradingApi', data, headers)
like image 30
Andrea Corbellini Avatar answered Oct 09 '22 03:10

Andrea Corbellini