• Sign up >>

Poloniex API integration

To add comments, please log in or register
Ivan Camargo
40
Ivan Camargo  

I'm trying to develop a integration with Poloniex API (https://docs.poloniex.com/#introduction), buy I'm facing some difficulties on doing private HTTP connections. 

Here's my code, can anyone help me? Server is returning the message {"error":"Invalid API key\/secret pair."} Thanks in advance.

#include <SHA512.mqh>
#include <JAson.mqh>

double poloniexGetBalances()
{
   string nonce0 = (string)(long)TimeLocal();
   StringAdd(nonce0,"00000");
   long nonce = (long)nonce0;
   
   char result[],data[];
   string headersResp, url_1, method;
   string headersCons="";
   method="GET";
   url_1="tradingApi";
   string url_2;

   url_2="command="+"returnBalances";
   url_2=url_2+"&nonce="+(string)nonce;

   SHA512 hash512;
   string signature=hash512.hmac(url_2,mysecret);
   headersCons="{ \"Key\": "+mykey+", \"Sign\": "+signature+" }\r\n";

   string url="https://poloniex.com/"+url_1+"?"+url_2;

   int res=WebRequest(method,url,headersCons,10000,data,result,headersResp);
   
   if(res==-1) 
   { 
      Print("Erro na conexão com o API da Poloniex.");
      return(0);   
   } 
   if(res==200) 
   { 
      string in=CharArrayToString(result);
      CJAVal js(NULL, jtUNDEF);
      js.Deserialize(in);
      string out=""; 
   }
   
   return(0);
}
Poloniex API Reference
  • docs.poloniex.com
Poloniex provides both HTTP and websocket APIs for interacting with the exchange. Both allow read access to public market data and private read access to your account. Private write access to your account is available via the private HTTP API. The public HTTP endpoint is accessed via GET requests while the private endpoint is accessed via...
Lorentzos Roussos
19781
Lorentzos Roussos  
Ivan Camargo:

I'm trying to develop a integration with Poloniex API (https://docs.poloniex.com/#introduction), buy I'm facing some difficulties on doing private HTTP connections. 

Here's my code, can anyone help me? Server is returning the message {"error":"Invalid API key\/secret pair."} Thanks in advance.

You need a post request for the private endpoint

Ivan Camargo
40
Ivan Camargo  
Lorentzos Roussos:

You need a post request for the private endpoint

Yes I just realized that and changed to "POST", but I still get the same error. Any ideias?
Lorentzos Roussos
19781
Lorentzos Roussos  
Ivan Camargo:
Yes I just realized that and changed to "POST", but I still get the same error. Any ideias?

Hmm , are you still sending the commands like "?command=" ,instead of as parameters ?

Ivan Camargo
40
Ivan Camargo  
I just tried to send them in the body field but still does not work.
double poloniexGetBalances()
{
   string nonce0 = (string)(long)TimeLocal();
   StringAdd(nonce0,"00000");
   long nonce = (long)nonce0;
   
   char result[],data[];
   string headersResp, url_1, method;
   string headersCons="";
   method="POST";
   url_1="tradingApi";
   string url_2;

   url_2="command="+"returnBalances";
   url_2=url_2+"&nonce="+(string)nonce;

   SHA512 hash512;
   string signature=hash512.hmac(url_2,mysecret);
   headersCons="{ \"Key\": "+mykey+", \"Sign\": "+signature+" }\r\n";

   StringToCharArray(url_2,data);

   string url="https://poloniex.com/"+url_1;

   int res=WebRequest(method,url,headersCons,10000,data,result,headersResp);
   
   if(res==-1) 
   { 
      Print("Erro na conexão com o API da Poloniex.");
      return(0);   
   } 
   if(res==200) 
   { 
      string in=CharArrayToString(result);
      CJAVal js(NULL, jtUNDEF);
      js.Deserialize(in);
      string out=""; 
   }
   
   return(0);
}

Lorentzos Roussos
19781
Lorentzos Roussos  

try this : 

  double pxGetBalance()
  {
  string itype="application/x-www-form-urlencoded";
  string iuser_agent="Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36 OPR/65.0.3467.62";
  string nonce0=(string)(long)TimeLocal();
  StringAdd(nonce0,"00000");
  string params[2];
  params[0]="command=returnBalances";
  params[1]="nonce="+nonce0;
  string url2=params[0]+"\r\n"+params[1];
  string ty="Content-Type: "+itype+"\r\n"+;
  SHA512 hash512;
  string signature=hash512.hmac(url2,mysecret);
  ty+="Key:"+mykey+"\r\nSign:"+signature+"\r\n";  
  string ua="user-agent:"+iuser_agent;
  string response=WR_REQUEST(wr_post,"https://poloniex.com/tradingApi",params,ty,ua,NULL,"https://poloniex.com",5000);
  Alert("Response : "+response);
  return(0);
  }
  
//REQUEST CODE 
enum wr_type
{
wr_get=0,//GET
wr_post=1//POST
};
string WR_REQUEST(wr_type request_type,string url,string &params[],string type,string user_agent,string cookie,string referer,uint timeout)
{
string response="not sent";
string headers="";
/*
for headers , in type string include header descriptor (e.g. Content-Type:)
              in user agent string include user agent descriptor (e.g. user-agent:)
*/
if(type!=NULL) headers=type;
if(user_agent!=NULL) headers=headers+"\r\n"+user_agent;
char post[],result[];
int res;
string target_url=url;
string specs="";
//fill specs if they exist 
int params_total=ArraySize(params);
bool noparams=false;
if(params_total==1&&params[0]=="") noparams=true;
if(noparams==false)
{
for(int fp=0;fp<params_total;fp++)
{
specs=specs+params[fp];
if(fp<params_total-1) specs=specs+"&";
}
}
if(request_type==wr_get&&noparams==false) target_url=target_url+"?"+specs;
char data[];
int data_size=0;
int slen=StringLen(specs);
if(request_type==wr_post) data_size=StringToCharArray(specs,data,0,slen,CP_UTF8);
ResetLastError();
string req_string="GET";
if(request_type==wr_post) req_string="POST";
res=WebRequest(req_string,target_url,cookie,referer,timeout,data,data_size,result,headers);
if(res==-1)
{
Print("Error in WebRequest. Error code  =",GetLastError());
MessageBox("Add the address '"+url+"' in the list of allowed URLs on tab 'Expert Advisors'","Error",MB_ICONINFORMATION);
}
else
{
//PrintFormat("The file has been successfully loaded, File size =%d bytes.",ArraySize(result));
int tit=ArraySize(result)-1;
string html="";
for(int xx=0;xx<=tit;xx++)
{
html=html+CharToStr(result[xx]);
}
response=html;
}  
return(response);
ArrayFree(result);
}
//REQUEST CODE ENDS HERE 
Ivan Camargo
40
Ivan Camargo  
Lorentzos Roussos:

try this : 

I removed an extra "+" from your code line -   string ty="Content-Type: "+itype+"\r\n"+; And changed function CharToStr to CharToString.

I'm still getting the same error - invalid key/secret... I tried to delete my API key twice and generate new key/secret but I'm still getting the same error :(

Any ideias?

Lorentzos Roussos
19781
Lorentzos Roussos  
Ivan Camargo:

I removed an extra "+" from your code line -   string ty="Content-Type: "+itype+"\r\n"+; And changed function CharToStr to CharToString.

I'm still getting the same error - invalid key/secret... I tried to delete my API key twice and generate new key/secret but I'm still getting the same error :(

Any ideias?

replace "+"\r\n"+" in url2 calculation with "+"&"+" 

Ivan Camargo
40
Ivan Camargo  
Lorentzos Roussos:

replace "+"\r\n"+" in url2 calculation with "+"&"+" 

Still getting the same error


double pxGetBalance()
{
   string itype="application/x-www-form-urlencoded";
   string iuser_agent="Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36 OPR/65.0.3467.62";
   string nonce0=(string)(long)TimeLocal();
   StringAdd(nonce0,"00000");
   string params[2];
   params[0]="command=returnBalances";
   params[1]="nonce="+nonce0;
   string url2=params[0]+"&"+params[1];
   string ty="Content-Type: "+itype+"\r\n";
   SHA512 hash512;
   string signature=hash512.hmac(url2,mysecret);
   ty+="Key:"+mykey+"\r\nSign:"+signature+"\r\n";  
   string ua="user-agent:"+iuser_agent;
   string response=WR_REQUEST(wr_post,"https://poloniex.com/tradingApi",params,ty,ua,NULL,"https://poloniex.com",5000);
   Alert("Response : "+response);
   return(0);
}
  
//REQUEST CODE 
enum wr_type
{
   wr_get=0,//GET
   wr_post=1//POST
};
string WR_REQUEST(wr_type request_type,string url,string &params[],string type,string user_agent,string cookie,string referer,uint timeout)
{
   string response="not sent";
   string headers="";
   /*
   for headers , in type string include header descriptor (e.g. Content-Type:)
                 in user agent string include user agent descriptor (e.g. user-agent:)
   */
   if(type!=NULL) headers=type;
   if(user_agent!=NULL) headers=headers+"\r\n"+user_agent;
   char post[],result[];
   int res;
   string target_url=url;
   string specs="";
   //fill specs if they exist 
   int params_total=ArraySize(params);
   bool noparams=false;
   if(params_total==1&&params[0]=="") noparams=true;
   if(noparams==false)
   {
      for(int fp=0;fp<params_total;fp++)
      {
      specs=specs+params[fp];
      if(fp<params_total-1) specs=specs+"&";
      }
   }
   if(request_type==wr_get&&noparams==false) target_url=target_url+"?"+specs;
   char data[];
   int data_size=0;
   int slen=StringLen(specs);
   if(request_type==wr_post) data_size=StringToCharArray(specs,data,0,slen,CP_UTF8);
   ResetLastError();
   string req_string="GET";
   if(request_type==wr_post) req_string="POST";
   res=WebRequest(req_string,target_url,cookie,referer,timeout,data,data_size,result,headers);
   if(res==-1)
   {
      Print("Error in WebRequest. Error code  =",GetLastError());
      MessageBox("Add the address '"+url+"' in the list of allowed URLs on tab 'Expert Advisors'","Error",MB_ICONINFORMATION);
   }
   else
   {
      //PrintFormat("The file has been successfully loaded, File size =%d bytes.",ArraySize(result));
      int tit=ArraySize(result)-1;
      string html="";
      for(int xx=0;xx<=tit;xx++)
      {
         html=html+CharToString(result[xx]);
      }
      response=html;
   }  
   return(response);
   ArrayFree(result);
}
//REQUEST CODE ENDS HERE 
Ivan Camargo
40
Ivan Camargo  

SOLVED!!!!

Changed the line:

res=WebRequest(req_string,target_url,cookie,referer,timeout,data,data_size,result,headers);

for

res=WebRequest(req_string,target_url,headers,timeout,data,result,resheader);

Also removed an extra "\r\n" on header string.


Thank you very much for you time Lorentzos!

Lorentzos Roussos
19781
Lorentzos Roussos  
Ivan Camargo:

SOLVED!!!!

Changed the line:

res=WebRequest(req_string,target_url,cookie,referer,timeout,data,data_size,result,headers);

for

res=WebRequest(req_string,target_url,headers,timeout,data,result,resheader);

Also removed an extra "\r\n" on header string.


Thank you very much for you time Lorentzos!

Great . No problem

To add comments, please log in or register