java - Kraken API: Problems with authentication (Invalid key) -
i trying implement api of bitcoin exchange kraken in java. unfortunately got stuck @ trying execute authentication in order retrieve private user data.
in particular, playing next implementation: http://pastebin.com/nhjdabh8 documentation of kraken's api here: https://www.kraken.com/help/api
however, far received {"error":["eapi:invalid key"]} . couldn't find error in implementation , tried several different api-keys. maybe have quick @ implementation , flaws in code? or has implemented kraken api?
many thanks!
the instructions authentication are:
http-header: api-key = api key api-sign = message signature using hmac-sha512 of (uri path + sha256(nonce + post data)) , base64 decoded secret api key
post data: nonce = increasing unsigned 64 bit integer otp = two-factor password (if two-factor enabled, otherwise not required) note: in case otp disabled, post-data consists of nonce.
the implementation experimenting is:
import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.io.outputstreamwriter; import java.io.unsupportedencodingexception; import java.net.httpurlconnection; import java.net.url; import java.security.invalidkeyexception; import java.security.messagedigest; import java.security.nosuchalgorithmexception; import javax.crypto.mac; import javax.crypto.spec.secretkeyspec; import org.apache.commons.codec.binary.base64; public class krakenclient { protected static string key = "myapikey"; // api key protected static string secret = "mysecret===="; // api secret protected static string url = "api.kraken.com"; // api base of operations url protected static string version = "0"; // api version public static void main(string[] args) throws exception { queryprivatemethod("balance"); } public static void queryprivatemethod(string method) throws nosuchalgorithmexception, ioexception{ long nonce = system.currenttimemillis(); string path = "/" + version + "/private/" + method; // path "/0/private/balance" string urlcomp = "https://"+url+path; // finish url "https://api.kraken.com/0/private/balance" string postdata = "nonce="+nonce; string sign = createsignature(nonce, path, postdata); postconnection(urlcomp, sign, postdata); } /** * @param nonce * @param path * @param postdata * @return * @throws nosuchalgorithmexception * @throws ioexception */ private static string createsignature(long nonce, string path, string postdata) throws nosuchalgorithmexception, ioexception { homecoming hmac(path+sha256(nonce + postdata), new string(base64.decodebase64(secret))); } public static string sha256hex(string text) throws nosuchalgorithmexception, ioexception{ homecoming org.apache.commons.codec.digest.digestutils.sha256hex(text); } public static byte[] sha256(string text) throws nosuchalgorithmexception, unsupportedencodingexception{ messagedigest md = messagedigest.getinstance("sha-256"); md.update(text.getbytes()); byte[] digest = md.digest(); homecoming digest; } public static void postconnection(string url1, string sign, string postdata) throws ioexception{ url url = new url( url1 ); httpurlconnection connection = (httpurlconnection) url.openconnection(); connection.addrequestproperty("api-key", key); connection.addrequestproperty("api-sign", base64.encodebase64string(sign.getbytes())); // connection.addrequestproperty("api-sign", sign); connection.addrequestproperty("user-agent", "mozilla/4.0"); connection.setrequestmethod( "post" ); connection.setdoinput( true ); connection.setdooutput( true ); connection.setusecaches( false ); // connection.setrequestproperty( "content-type", // "application/x-www-form-urlencoded" ); connection.setrequestproperty( "content-length", string.valueof(postdata.length()) ); outputstreamwriter author = new outputstreamwriter( connection.getoutputstream() ); writer.write( postdata ); writer.flush(); bufferedreader reader = new bufferedreader( new inputstreamreader(connection.getinputstream()) ); ( string line; (line = reader.readline()) != null; ) { system.out.println( line ); } writer.close(); reader.close(); } public static string hmac(string text, string secret){ mac mac =null; secretkeyspec key = null; // create new secret key seek { key = new secretkeyspec( secret.getbytes( "utf-8"), "hmacsha512" ); } catch( unsupportedencodingexception uee) { system.err.println( "unsupported encoding exception: " + uee.tostring()); homecoming null; } // create new mac seek { mac = mac.getinstance( "hmacsha512" ); } catch( nosuchalgorithmexception nsae) { system.err.println( "no such algorithm exception: " + nsae.tostring()); homecoming null; } // init mac key. seek { mac.init( key); } catch( invalidkeyexception ike) { system.err.println( "invalid key exception: " + ike.tostring()); homecoming null; } // encode text secret seek { homecoming new string( mac.dofinal(text.getbytes( "utf-8"))); } catch( unsupportedencodingexception uee) { system.err.println( "unsupported encoding exception: " + uee.tostring()); homecoming null; } } }
remove "/" prefix of path variable.
string path = version + "/private/" + method; // path "0/private/balance"
java api authentication
No comments:
Post a Comment