how to send key/secret pair with poloniex api?

  • Last Update :
  • Techknowledgy :

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

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)
import urllib.parse
import json
import time
import hmac,hashlib
import httpx

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

req={}
req['nonce'] = int(time.time()*1000)
req['command'] = "returnDepositAddresses"

# encoding post_data for sign calculation
encoded_post_data = urllib.parse.urlencode(req).encode('utf-8')

sign = hmac.new(bytes(Secret, 'utf-8'), encoded_post_data, hashlib.sha512).hexdigest()

headers = {
    'Sign': sign,
    'Key': APIKey
}

with httpx.Client() as client:
    url = 'https://poloniex.com/tradingApi'
    response = client.post(url, headers=headers, data=req)

data = response.json()

print(data)

Suggestion : 2

Assuming that your APIKey and Secret are anycodings_api OK, this following version will works:,Also looking at the reference Python anycodings_api implementation linked by the docs, it anycodings_api appears that the API wants Key and Sign anycodings_api as headers, and nonce as POST parameter.,I've just been incrementing the nonce anycodings_api manually to keep things simple.,The API documentation for what I'm working anycodings_api on can be found at:

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

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

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()

Assuming that your APIKey and Secret are anycodings_api 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

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)
import urllib.parse
import json
import time
import hmac,hashlib
import httpx

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

req={}
req['nonce'] = int(time.time()*1000)
req['command'] = "returnDepositAddresses"

# encoding post_data for sign calculation
encoded_post_data = urllib.parse.urlencode(req).encode('utf-8')

sign = hmac.new(bytes(Secret, 'utf-8'), encoded_post_data, hashlib.sha512).hexdigest()

headers = {
    'Sign': sign,
    'Key': APIKey
}

with httpx.Client() as client:
    url = 'https://poloniex.com/tradingApi'
    response = client.post(url, headers=headers, data=req)

data = response.json()

print(data)

Suggestion : 3

If you would like to post, please check out the MrExcel Message Board FAQ and register here. If you forgot your password, you can reset your password. , MrExcel Publishing MrExcel Homepage MrExcel Bookstore MrExcel Seminars Excel Consulting Services

[COLOR = #333333][FONT= Roboto] Sub httpPost()[/FONT][/COLOR] [COLOR = #333333][FONT= Roboto] Dim XMLHTTP[/FONT][/COLOR] [COLOR = #333333][FONT= Roboto] Dim result As String[/FONT][/COLOR] [COLOR = #333333][FONT= Roboto] Dim argumentString[/FONT][/COLOR] [COLOR = #333333][FONT= Roboto] argumentString = "currencypair=SC_BTC&rate=.00000120&amount=10" [/FONT][/COLOR] [COLOR = #333333][FONT= Roboto] Set XMLHTTP = CreateObject("MSXML2.XMLHTTP.6.0")[/FONT][/COLOR] [COLOR = #333333][FONT= Roboto] XMLHTTP.Open "POST", _[/FONT][/COLOR] [COLOR = #333333][FONT= Roboto]
                        "[URL]https://poloniex.com/tradingApi?key=(KEY[/URL] HERE)&sign=(SECRET HERE)&nonce=1", False[/FONT][/COLOR] [COLOR = #333333][FONT= Roboto] XMLHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" [/FONT][/COLOR] [COLOR = #333333][FONT= Roboto] XMLHTTP.setRequestHeader "Content-type", "application/x-www-form-urlencoded" [/FONT][/COLOR] [COLOR = #333333][FONT= Roboto] XMLHTTP.send argumentString[/FONT][/COLOR] [COLOR = #333333][FONT= Roboto] result = XMLHTTP.responsetext[/FONT][/COLOR] [COLOR = #333333][FONT= Roboto] MsgBox result[/FONT][/COLOR] [COLOR = #333333][FONT= Roboto] Set XMLHTTP = Nothing[/FONT][/COLOR] [COLOR = #333333][FONT= Roboto] End Sub[/FONT][/COLOR]

Suggestion : 4

To use the private api commands you first need an api key and secret (supplied by poloniex). When creating the instance of poloniex.Poloniex you can pass your api key and secret to the object like so:,Poloniex API wrapper for Python 2.7 and 3 with websocket support,Almost every api command can be called this way. This wrapper also checks that the command you pass to the command arg is a valid command to send to poloniex, this helps reduce api errors due to typos.,You can also subscribe and start the websocket thread when creating an instance of PoloniexSocketed by using the subscribe and start args:

Install:

pip install--upgrade poloniexapi

All api calls are done through an instance of poloniex.Poloniex. You can use the instance as follows:

#
import this package
from poloniex
import Poloniex

# make an instance of poloniex.Poloniex
polo = Poloniex()

# show the ticker
print(polo('returnTicker'))

Using the instances __call__ method (shown above) you can pass the command string as the first argument to make an api call. The poloniex.Poloniex class also has 'helper' methods for each command that will help 'sanitize' the commands arguments. For example, Poloniex.returnChartData('USDT_BTC', period=777) will raise PoloniexError("777 invalid candle period").

# using a 'helper'
method
print(polo.returnChartData(currencyPair = 'BTC_LTC', period = 900))
# bypassing 'helper'
print(polo(command = 'returnChartData', args = {
   'currencyPair': 'BTC_LTC',
   'period': 900
}))

Public trade history:

print(polo.marketTradeHist('BTC_ETH'))

Private trade history:

print(polo.returnTradeHistory('BTC_ETH'))