I try to connect to the poloniex.com API https://poloniex.com/support/api/ which 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. The nonce parameter is an integer which must always be greater than the previous nonce used.)
But I always get
{"error":"Invalid
API key\/secret pair."}
My hmac512Digest works fine, I've checked it.
There must be something wrong in my code.
Can someone please Help?
public class Pol2 {
public static String POLONIEX_SECRET_KEY = "12345";
public static String POLONIEX_API_KEY = "ABX";
public static void main(String[] args) {
try {
accessPoloniex();
} catch (IOException e) {
e.printStackTrace();
}
}
public static final void accessPoloniex() throws IOException {
final String nonce = String.valueOf(System.currentTimeMillis());
String connectionString = "https://poloniex.com/tradingApi";
String queryArgs = "command=returnBalances";
String hmac512 = hmac512Digest(queryArgs, POLONIEX_SECRET_KEY);
// Produce the output
ByteArrayOutputStream out = new ByteArrayOutputStream();
Writer writer = new OutputStreamWriter(out, "UTF-8");
writer.append(queryArgs);
writer.flush();
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost(connectionString);
post.addHeader("Key", POLONIEX_API_KEY); //or setHeader?
post.addHeader("Sign", hmac512); //or setHeader?
post.setEntity(new ByteArrayEntity(out.toByteArray()));
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("command", "returnBalances"));
params.add(new BasicNameValuePair("nonce", nonce));
CloseableHttpResponse response = null;
Scanner in = null;
try {
post.setEntity(new UrlEncodedFormEntity(params));
response = httpClient.execute(post);
HttpEntity entity = response.getEntity();
in = new Scanner(entity.getContent());
while (in.hasNext()) {
System.out.println(in.next());
}
EntityUtils.consume(entity);
} finally {
in.close();
response.close();
}
}
}
I struggled with this myself and finally got it to work. Here's a very basic, working example:
public class PoloTest {
public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException, ClientProtocolException, IOException {
String key = "YOUR API KEY HERE";
String secret = "YOUR API SECRET HERE";
String url = "https://poloniex.com/tradingApi";
String nonce = String.valueOf(System.currentTimeMillis());
String queryArgs = "command=returnBalances&nonce=" + nonce;
Mac shaMac = Mac.getInstance("HmacSHA512");
SecretKeySpec keySpec = new SecretKeySpec(secret.getBytes(), "HmacSHA512");
shaMac.init(keySpec);
final byte[] macData = shaMac.doFinal(queryArgs.getBytes());
String sign = Hex.encodeHexString(macData);
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost(url);
post.addHeader("Key", key);
post.addHeader("Sign", sign);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("command", "returnBalances"));
params.add(new BasicNameValuePair("nonce", nonce));
post.setEntity(new UrlEncodedFormEntity(params));
CloseableHttpResponse response = httpClient.execute(post);
HttpEntity responseEntity = response.getEntity();
System.out.println(response.getStatusLine());
System.out.println(EntityUtils.toString(responseEntity));
}
}
I've looked into the Python example that they've linked to on their page. The nonce parameter must be MAC'ed along with the command and the final MAC is appended in Hex-encoded format:
String queryArgs = "command=returnBalances&nonce=" + nonce;
String hmac512 = hmac512Digest(queryArgs, POLONIEX_SECRET_KEY);
Also, the following
ByteArrayOutputStream out = new ByteArrayOutputStream();
Writer writer = new OutputStreamWriter(out, "UTF-8");
writer.append(queryArgs);
writer.flush();
//...
post.setEntity(new ByteArrayEntity(out.toByteArray()));
can be reduced to
post.setEntity(new ByteArrayEntity(queryArgs.getBytes("UTF-8")));
The nonce parameter must be MAC'ed along with the command...
If a hash is a one way function, and Polo have no idea what nonce I might choose, (or when, if i'm using UTC), how can Polo ever extract anything meaningful from what I send them.
Related
I tried searching for this error. There are many results on google for this search but nothing proved useful to me.
This is my web service method
#GET
#Path("/values")
public String test() {
return "{\"x\":5,\"y\":6}";
}
This is my client code
public class Check {
public static void main(String[] args){
String url = "http://localhost:8181/math/webapi/values";
HttpClient httpClient = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
try {
HttpResponse response = httpClient.execute(request);
String value = response.toString();
JSONObject json = new JSONObject(value);
int i = json.getInt("x");
System.out.println(i);
}catch (Exception e) {
e.printStackTrace();
}
}
The above code is a starter code and it is for learning how to use it. If this is solved, I have to apply the knowledge in another application. The client side code, I want to use the logic in android.
EDIT
public class Check {
public static void main(String[] args){
String url = "http://localhost:8181/math/webapi/values";
HttpClient httpClient = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
try {
HttpResponse response = httpClient.execute(request);
InputStream value = response.getEntity().getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(value));
String jsonValue = br.readLine();
JSONObject json = new JSONObject(jsonValue);
int i = json.getInt("x");
System.out.println(i);
}catch (Exception e) {
e.printStackTrace();
}
}
Fairly certain response.toString doesn't do what you think it does, as it's not listed in the documentation.
I believe you need to use response.getEntity, and then entity.getContent, which gives you an InputStream to read the content from. Then pass that stream into your parser.
Try this code. Use IOUtils as mentioned. it will work.
public class Check {
public static void main(String[] args){
String url = "http://localhost:8181/math/webapi/values";
HttpClient httpClient = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
try {
HttpResponse response = httpClient.execute(request);
InputStream value = response.getEntity().getContent();
String jsonValue = IOUtils.toString(value);
JSONObject json = new JSONObject(jsonValue);
int i = json.getInt("x");
System.out.println(i);
}catch (Exception e) {
e.printStackTrace();
}
}
I've been trying to get this work for a while now. Im working on an app where the user signs in with a username and password which uses a httppost request to post to the server. i get the correct response, and during the post i store the session cookie that the server gives me. (I store it in a cookie store)
But when i try to click a link on the menu ( which does a second http post) after i logged in, the servers gives me a message saying that i am not logged in. But i send the cookie that i recieved in the first post to the server in the second post, yet the server does not recognize that i am logged in. To test this more easily i used the chrome plug in "Postman" which lets you post to websites easily. The only time it worked was when i log in to the website using chrome then use Postman to do the second post, which successfully gives me a response. however, when i use Postman to log in, then also use postman to attempt the second post , again, "Not logged in". Im guessing that the cookie is not being stored properly in the app. How could i go about fixing this? I read some stuff about storing the cookies in something called "Shared Preferences", is that possibly a fix? If so, what is it and how could i store the cookies there?
public static String executeHttpPost(String url, ArrayList<NameValuePair> postParameters) throws Exception {
BufferedReader in = null;
try {
LoginLayout.httpClient = new DefaultHttpClient();
HttpPost request = new HttpPost(url);
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
request.setEntity(formEntity);
CookieStore cookiestore = LoginLayout.httpClient.getCookieStore();
HttpResponse response = LoginLayout.httpClient.execute(request);
List<Cookie> cookies = LoginLayout.httpClient.getCookieStore().getCookies();
cookiestore.addCookie(cookie);
cookie = cookies.get(0);
cookieValue = "ASPSESSIONIDCQTCRACT=" + cookiestore.getCookies();
System.out.println("The cookie" + cookieValue);
List<Cookie> cookiess = cookiestore.getCookies();
cookiee = cookies.get(0);
Header[] headers = response.getAllHeaders();
System.out.println("length" + headers.length);
for (int i=0; i < headers.length; i++) {
Header h = headers[i];
System.out.println( "Header names: "+h.getName());
System.out.println( "Header Value: "+h.getValue());
}
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
// System.out.println( mCookie);
String result = sb.toString();
return result;
}
finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} }
Here is the getter so i can access the cookie from the cookie store in the next activity
public static String getCookie(){
return cookiee.getName() +"="+cookiee.getValue();
}
Here is the second post where i try to retrieve the stored cookie, which it seems to do sucessfully, however the server doesnt recognize i am logged in
public static String executeHttpPost(String url, ArrayList<NameValuePair> postParameters) throws Exception {
BufferedReader in = null;
try {
HttpPost request = new HttpPost(url);
request.setHeader("Cookie", LoginLayout.getCookie());
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
request.setEntity(formEntity);
HttpResponse response = LoginLayout.httpClient.execute(request);
Header[] headers = response.getAllHeaders();
System.out.println("length" + headers.length);
for (int i=0; i < headers.length; i++) {
Header h = headers[i];
System.out.println( "Header names: "+h.getName());
System.out.println( "Header Value: "+h.getValue());
}
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
//System.out.println( mCookie);
String result = sb.toString();
return result;
}
finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
You have to make sure that your HttpClient is using the same HttpContext on each request.
The CookieStore is associated with the HttpContext so create a new instance of HttpContext will create a new CookieStore as well.
The best way I found is to create a static instance of HttpContext and use it on every request.
Below I added an part of a class I'm using in my apps:
public class ApiClient {
// Constants
private final static String TAG = "ApiClient";
private final static String API_URL = "your-url";
// Data
private static ApiClient mInstance;
private HttpClient mHttpClient;
private ThreadSafeClientConnManager mConnectionManager;
private HttpPost mPost;
/*
* we need it static because otherwise it will be recreated and the session
* will be lost
*/
private static HttpContext mHttpContext;
private HttpParams mParams;
private Context mContext;
public ApiClient(Context pContext) {
mParams = new BasicHttpParams();
mContext = pContext;
if (null == mHttpContext) {
CookieStore cookieStore = new BasicCookieStore();
mHttpContext = new BasicHttpContext();
mHttpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
}
ConnManagerParams.setMaxTotalConnections(mParams, 300);
HttpProtocolParams.setVersion(mParams, HttpVersion.HTTP_1_1);
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
mConnectionManager = new ThreadSafeClientConnManager(mParams, schemeRegistry);
mHttpClient = new DefaultHttpClient(mConnectionManager, mParams);
}
public static ApiClient getInstance(Context pContext) {
if (null == mInstance) {
return (mInstance = new ApiClient(pContext));
} else {
return mInstance;
}
}
public void testPOST() {
List<NameValuePair> requestParams = new ArrayList<NameValuePair>();
requestParams.add(new BasicNameValuePair("param1", "value1"));
requestParams.add(new BasicNameValuePair("param2", "value2"));
mPost = new HttpPost(API_URL);
try {
mPost.setEntity(new UrlEncodedFormEntity(requestParams, HTTP.UTF_8));
HttpResponse responsePOST = mHttpClient.execute(mPost, mHttpContext);
HttpEntity resEntity = responsePOST.getEntity();
String result = EntityUtils.toString(resEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
To test it try setting the correct API_URL and
ApiClient api = ApiClient.getInstance(somContext);
api.testPOST();
I'm trying to post a comment from desktop application to Trac.
I'm using apache http client library in this project here is a link
Here is my code, sorry if it's hard to read
public class TestComment {
private static String cookie;
public static void main(String[] args) throws Exception {
CookieHandler.setDefault(new CookieManager());
DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://localhost:8080/mytrac/login");
BasicHeader authHeader = new BasicHeader("Authorization", "Basic " + encodedPassword("admin", "123123"));
httpGet.addHeader(authHeader);
HttpResponse response = defaultHttpClient.execute(httpGet);
List<Cookie> cookies = defaultHttpClient.getCookieStore().getCookies();
String token = null;
if(!cookies.isEmpty()){
for (int i = 0; i < cookies.size(); i++) {
System.out.println("- " + cookies.get(i).toString());
token = cookies.get(i).toString().substring(43, 67);
System.out.println(token);
}
}
setCookie(token);
responseLog(response);
HttpPost httpPost = new HttpPost("http://localhost:8080/mytrac/ticket/2#comment:5");
httpPost.setHeader(authHeader);
httpPost.setHeader("Host", "localhost:8080");
httpPost.setHeader("User-Agent", "Mozilla/5.0");
httpPost.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
httpPost.setHeader("Accept-Language", "en-US,en;q=0.8");
httpPost.setHeader("Connection", "keep-alive");
httpPost.setHeader("Referer", "http://localhost:8080/mytrac/ticket/2");
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("__FORM_TOKEN", token));
formparams.add(new BasicNameValuePair("comment", "Test comment"));
formparams.add(new BasicNameValuePair("field_reporter", "admin"));
httpPost.setEntity(new UrlEncodedFormEntity(formparams));
response = defaultHttpClient.execute(httpPost);
responseLog(response);
System.out.println(response.getStatusLine());
}
private static String encodedPassword(String username, String password) {
byte[] encodedPassword = (username + ":" + password).getBytes();
BASE64Encoder base64Encoder = new BASE64Encoder();
return base64Encoder.encode(encodedPassword);
}
private static void responseLog(org.apache.http.HttpResponse httpResponse) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpResponse. getEntity().getContent()));
StringBuffer stringBuffer = new StringBuffer();
String line1;
while ((line1 = bufferedReader.readLine()) != null) {
stringBuffer.append(line1 + "\n");
}
System.out.println(stringBuffer) ;
}
public static String getCookie() {
cookie = cookie.substring(cookie.indexOf(":") + 1);
return cookie;
}
public static void setCookie(String cookie) {
TestComment.cookie = cookie;
}
}
When I run this code I get 200 code it tells ok and I even get my comment in Text-Aria form, but don't post it. When I post a comment in browser the code is 303. Where am I wrong, may be I am on totaly wrong way?
We resolved the problem
I didn't know but i just needed to send one more form, we need to get view time from trac and send it as a form:
formparams.add(new BasicNameValuePair("view_time", view_time));
now it works
I need to send http POST request from mobile android application to the server side applcation.
This request need to contain json message in body and some key-value parametres.
I am try to write this method:
public static String makePostRequest(String url, String body, BasicHttpParams params) throws ClientProtocolException, IOException {
Logger.i(HttpClientAndroid.class, "Make post request");
HttpPost httpPost = new HttpPost(url);
StringEntity entity = new StringEntity(body);
httpPost.setParams(params);
httpPost.setEntity(entity);
HttpResponse response = getHttpClient().execute(httpPost);
return handleResponse(response);
}
Here i set parametres to request throught method setParams and set json body throught setEntity.
But it isn't work.
Can anybody help to me?
You can use a NameValuePair to do this..........
Below is the code from my project where I used NameValuePair to sent the xml data and receive the xml response, this will provide u some idea about how to use it with JSON.
public String postData(String url, String xmlQuery) {
final String urlStr = url;
final String xmlStr = xmlQuery;
final StringBuilder sb = new StringBuilder();
Thread t1 = new Thread(new Runnable() {
public void run() {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(urlStr);
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
1);
nameValuePairs.add(new BasicNameValuePair("xml", xmlStr));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
Log.d("Vivek", response.toString());
HttpEntity entity = response.getEntity();
InputStream i = entity.getContent();
Log.d("Vivek", i.toString());
InputStreamReader isr = new InputStreamReader(i);
BufferedReader br = new BufferedReader(isr);
String s = null;
while ((s = br.readLine()) != null) {
Log.d("YumZing", s);
sb.append(s);
}
Log.d("Check Now",sb+"");
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
t1.start();
try {
t1.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Getting from Post Data Method "+sb.toString());
return sb.toString();
}
I'm trying to write a POST call to Google Translate with Jersey 1.5. This is my code:
package main;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.core.util.MultivaluedMapImpl;
import javax.ws.rs.core.MultivaluedMap;
public class Main {
private static String GOOGLE_TRANSLATE_URL = "https://www.googleapis.com/language/translate/v2";
private static String translateString(String sourceString, String sourceLanguage, String targetLanguage) {
String response;
Client c = Client.create();
WebResource wr = c.resource(GOOGLE_TRANSLATE_URL);
MultivaluedMap<String, String> params = new MultivaluedMapImpl();
params.add("q", sourceString);
params.add("source", sourceLanguage);
params.add("target", targetLanguage);
params.add("key", "xxxx");
wr.header("X-HTTP-Method-Override", "GET");
response = wr.post(String.class, params);
return response;
}
public static void main(String[] args) {
System.out.println(translateString("Hello", "en", "sv"));
}
}
When I run this, all I get back is this: com.sun.jersey.api.client.UniformInterfaceException: POST https://www.googleapis.com/language/translate/v2 returned a response status of 404.
I've managed to accomplish this with a simple cURL command like so:
curl --header "X-HTTP-Method-Override: GET" -d key=xxxx -d q=Hello -d source=en -d target=sv https://www.googleapis.com/language/translate/v2
Thanks in advance!
I suspect that POST with zero Content-Length is not something a normal HTTP server will accept. The RFC does not define this case, but the main assumption of POST is that you're sending a message body.
Looking at the Google API, they mention the following
You can also use POST to invoke the API if you want to send more data in a single request. The q parameter in the POST body must be less than 5K characters. To use POST, you must use the X-HTTP-Method-Override header to tell the Translate API to treat the request as a GET (use X-HTTP-Method-Override: GET).
This means that instead of adding q, source and target parameters in the URL, you need to do so in the POST body. I'm not familiar with the Jersey API, from a brief look you just need to add params as an explicit second parameter to the .post call, remove the queryParams() call, and set the Content-Length properly.
I think the best and correct way is this
private static final String gurl = "www.googleapis.com";
private static final String gpath = "/language/translate/v2/detect";
public String detectLangGooglePost(String text) throws SystemException {
List<NameValuePair> qparams = new ArrayList<NameValuePair>();
qparams.add(new BasicNameValuePair("key", key));
URI uri;
try {
uri = URIUtils.createURI("https", gurl, -1, gpath, URLEncodedUtils.format(qparams, "UTF-8"), null);
} catch (URISyntaxException e) {
throw new SystemException("Possibly invalid URI parameters", e);
}
HttpResponse response = getPostResponse(uri, text);
StringBuilder builder = getBuilder(response);
String language = getLanguage(builder);
return language;
}
private HttpResponse getPostResponse(URI uri, String text) throws SystemException {
List<NameValuePair> qparams = new ArrayList<NameValuePair>();
qparams.add(new BasicNameValuePair("q", text));
HttpResponse response;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(uri);
httpPost.addHeader("X-HTTP-Method-Override", "GET");
try {
httpPost.setEntity(new UrlEncodedFormEntity(qparams));
response = httpclient.execute(httpPost);
} catch (Exception e) {
throw new SystemException("Problem when executing Google get request", e);
}
int sc = response.getStatusLine().getStatusCode();
if (sc != HttpStatus.SC_OK)
throw new SystemException("google status code : " + sc);
return response;
}
private StringBuilder getBuilder(HttpResponse response) throws SystemException {
HttpEntity entity = response.getEntity();
if (entity == null)
throw new SystemException("response entity null");
StringBuilder builder = new StringBuilder();
BufferedReader in = null;
String str;
try {
in = new BufferedReader(new InputStreamReader(entity.getContent()));
while ((str = in.readLine()) != null)
builder.append(str);
} catch (IOException e) {
throw new SystemException("Reading input stream of http google response entity problem", e);
} finally {
IOUtils.closeQuietly(in);
}
if (builder.length() == 0)
throw new SystemException("content stream of response entity empty has zero length");
return builder;
}
private String getLanguage(StringBuilder builder) throws SystemException {
JSONObject data = null;
JSONArray detections = null;
String language = null;
JSONObject object = (JSONObject) JSONValue.parse(builder.toString());
if (object == null)
throw new SystemException("JSON parsing builder object returned null");
if (object.containsKey("data") == false)
throw new SystemException("JSONObject doesn't contain data key");
data = (JSONObject) object.get("data");
detections = (JSONArray) data.get("detections");
if (detections == null)
throw new SystemException("JSON detections is null");
JSONObject body = (JSONObject) ((JSONArray) detections.get(0)).get(0);
if (body == null)
throw new SystemException("detections body is null");
if (body.containsKey("language") == false)
throw new SystemException("language key is null");
language = (String) body.get("language");
if (language == null || language.equals(unknown))
throw new SystemException("Google lang detection - resulting language : " + language);
return language;
}
I was able to send very long text like this!
Client:
MultivaluedMap<String,String> formData = new MultivaluedMapImpl();
formData.add("text", text);
WebResource resource = Client.create().resource(getBaseURI()).path("text2rdf");
return resource.type("application/x-www-form-urlencoded").post(String.class, formData);
Server:
#POST
#Produces("text/whatever")
public String textToRdf (
#FormParam("text") String text) {...
I switched to Apache HttpClient 4.x and solved it like this instead:
public class Main {
private static String GOOGLE_TRANSLATE_URL = "https://www.googleapis.com/language/translate/v2";
private static String GOOGLE_API_KEY = "xxxx";
private static String translateString(String sourceString, String sourceLanguage, String targetLanguage) {
String response = null;
// prepare call
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(GOOGLE_TRANSLATE_URL+"?q="+sourceString+"&source="+sourceLanguage+"&target="+targetLanguage+"&key="+GOOGLE_API_KEY);
post.setHeader("X-HTTP-Method-Override", "GET");
try {
// make the call
ResponseHandler<String> responseHandler = new BasicResponseHandler();
response = client.execute(post, responseHandler);
} catch (IOException e) {
// todo: proper error handling
}
return response;
}
public static void main(String[] args) {
System.out.println(translateString("hello", "en", "sv"));
}
}
Don't really know why this works better than Jersey, but it works. Thanks for trying to help!