How to send a key / secret pair with Poloniex API?

I am trying to write a simple script to verify that I am making an API call correctly, and then I plan to create a more complex program from there. The answer to the error I get is:

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

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

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

I just increased the nonce knob to make 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() 
+6
source share
2 answers

Assuming your APIKey and secret are fine, this next version will work:

 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 secret key and API key, it still doesn't work. It is imperative that your APIKey or secret have an input error! [or APIkey is limited only to "recall" or you have chosen to restrict the IP address and try to connect to the implicit IP address.]

+4
source

The API documentation says:

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

  • The key is your API key.
  • Sign - POST request data signed with your key "secretly" in accordance with the HMAC-SHA512 method.

In addition, all requests must include the "nonce" POST parameter.

Even if he says β€œheaders,” I think it means POST options.

Also looking at the link to the Python implementation associated with documents, it seems that the API wants Key and Sign as headers, and nonce as a POST parameter.

Change your request:

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

in

 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) 
+1
source

All Articles