Skip to content

Instantly share code, notes, and snippets.

@yoshprogrammer
Created January 14, 2016 17:57
Show Gist options
  • Save yoshprogrammer/0b7eb433b18ce1deb66b to your computer and use it in GitHub Desktop.
Save yoshprogrammer/0b7eb433b18ce1deb66b to your computer and use it in GitHub Desktop.
/*-----------------------------------------------------------------------------------------------------------------------------------------------------------*/
package com.unitnet.weebly;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import java.net.URI;
/**
* Created by paries_2 on 1/9/2016.
*/
public class HttpGetWithEntity extends HttpEntityEnclosingRequestBase {
public HttpGetWithEntity() {
super();
}
public HttpGetWithEntity(URI uri) {
super();
setURI(uri);
}
public HttpGetWithEntity(String uri) {
super();
setURI(URI.create(uri));
}
@Override
public String getMethod() {
return HttpGet.METHOD_NAME;
}
}
/*-----------------------------------------------------------------------------------------------------------------------------------------------------------*/
Then this is GET that works
/*------------------------------------------------------------------------------------------------------------------------------------------------------------*/
package com.unitnet.weebly;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
import org.apache.http.HttpResponse;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* Created by paries_2 on 1/9/2016.
*/
public class GetAccount {
final static Logger logger = Logger.getLogger(GetAccount.class);
public static void main(String[] args) throws IOException {
Properties props = new Properties();
InputStream is = new FileInputStream("D:\\Work\\UnitNet\\UnitNet2010\\src\\com\\unitnet\\weebly\\log4jWEBBY.properties");
try {
props.load(is);
} finally {
try {
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
PropertyConfigurator.configure(props);
if (logger.isDebugEnabled()) {
logger.debug("This is debug");
}
String WeeblyAPIKey = "xxxx";
String WeeblyAPISecret = "xxxx";
JSONArray jsonArray = new JSONArray();
String secrethash = "GET" + "\n" + "account" + "\n" + jsonArray.toJSONString();
try {
byte[] keyBytes = WeeblyAPISecret.getBytes();
SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA256");
// Get an hmac_sha1 Mac instance and initialize with the signing key
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(signingKey);
// Compute the hmac on input data bytes
byte[] rawHmac = mac.doFinal(secrethash.getBytes());
// Convert raw bytes to Hex
byte[] hexBytes = new Hex().encode(rawHmac);
String hash = new String(hexBytes, "UTF-8");
byte[] encodedBytes = Base64.encodeBase64(hash.getBytes());
System.out.println("encodedBytes " + new String(encodedBytes));
hash = new String(encodedBytes);
System.out.println("hash " + hash);
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpGetWithEntity myGet = new HttpGetWithEntity("https://api.weeblycloud.com/account");
myGet.setEntity(new StringEntity("[]"));
myGet.setHeader("Content-Type", "application/json");
myGet.setHeader("X-Public-Key", WeeblyAPIKey);
myGet.setHeader("X-Signed-Request-Hash", hash);
HttpResponse response = client.execute(myGet);
System.out.println(response.getProtocolVersion());
System.out.println(response.getStatusLine().getStatusCode());
System.out.println(response.getStatusLine().getReasonPhrase());
JSONParser parser = new JSONParser();
Object obj = parser.parse(EntityUtils.toString(response.getEntity(), "UTF-8"));
JSONObject jsonResponse = (JSONObject) obj;
System.out.println("----------------------------\n" + jsonResponse.toString() + "\n--------------------------------");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment