Here i am attaching code and a link consist of full code , have a look on it:-
My authorization header seams to be coming of same length as mentioned in official site of payeezy.I have also make my hmacString of same order as mentioned in this link (https://developer.payeezy.com/content/hmac-validation-failure) . After doing all this i am still getting this same issue
public static String excutePost(String urlParameters) throws IOException {
URL url = new URL("https://api-cert.payeezy.com/v1/transactions");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
try {
// Create connection
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", headerContentType);
connection.setRequestProperty("apikey ", apikey);
connection.setRequestProperty("token", MerchantToken);
connection
.setRequestProperty("Authorization", authorizationHeader);
connection.setRequestProperty("timestamp", ""+epoch);
connection.setRequestProperty("nonce", ""+nonce);
connection.setDoOutput(true);
connection.setReadTimeout(30000);
// Send request
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
// Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while ((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
return response.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
Here is full java class code :- http://piratepad.net/ep/pad/view/ro.WwZ9v6FX1a6/latest
I finally solve this error by sending direct String as parameter in api url hit.Here i am posting some of my code which solve my error :-
String str = "{\"amount\":\"1299\",\"merchant_ref\":\"Astonishing-Sale\",\"transaction_type\":\"authorize\",\"credit_card\":{\"card_number\":\"4788250000028291\",\"cvv\":\"123\",\"exp_date\": \"1020\",\"cardholder_name\": \"John Smith\",\"type\": \"visa\"},\"method\": \"credit_card\",\"currency_code\": \"USD\"}";
now this String will be used in generating my authorisation key.
the whole process is defined below :-
getSecurityKeys(apikey, pzsecret,str);
private static Map<String, String> getSecurityKeys(String appId,
String secureId, String payLoad) throws Exception {
Map<String, String> returnMap = new HashMap<String, String>();
try {
returnMap.put(NONCE, Long.toString(nonce));
returnMap.put(APIKEY, appId);
returnMap.put(TIMESTAMP, Long.toString(System.currentTimeMillis()));
returnMap.put(TOKEN, MerchantToken);
returnMap.put(APISECRET, pzsecret);
returnMap.put(PAYLOAD, payLoad);
returnMap.put(AUTHORIZE, getMacValue(returnMap));
authorizationHeader = returnMap.get(AUTHORIZE);
return returnMap;
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
public static String getMacValue(Map<String, String> data) throws Exception {
Mac mac = Mac.getInstance("HmacSHA256");
String apiSecret = data.get(APISECRET);
SecretKeySpec secret_key = new SecretKeySpec(apiSecret.getBytes(),
"HmacSHA256");
mac.init(secret_key);
StringBuilder buff = new StringBuilder();
buff.append(data.get(APIKEY)).append(data.get(NONCE))
.append(data.get(TIMESTAMP));
if (data.get(TOKEN) != null)
buff.append(data.get(TOKEN));
if (data.get(PAYLOAD) != null)
buff.append(data.get(PAYLOAD));
byte[] macHash = mac.doFinal(buff.toString().getBytes("UTF-8"));
String authorizeString = Base64.encodeBase64String(toHex(macHash));
return authorizeString;
}
Now finally you can pass direct String(i.e str) as parameter in hitting post api in java.
hope it helps other to integrate payeezy payment gateway without using any dependencies.
Happy Codeing!!!
You must generate a new timestamp and nonce for every request, i.e., every new request must have its unique timestamp and nonce.
In java, timestamp can be set as System.currentTimeMillis() and nonce can be set using UUID (UUID.randomUUID().toString()).
Finally, make sure that your Authorization is correctly computed (I see they use HMAC-SHA1 using API secret key).
I hope this helps.
Edit: As suspected, it's your HMAC-SHA1 Authorization value that is incorrect. I get the following response when running your code (after few coding of my own).
Connection = keep-alive
Content-Length = 51
Content-Type = application/json
{"code":"403", "message":"HMAC validation Failure"}
Make sure that you compute your HMAC-SHA1 value correctly (as I said above).
See the below (updated) code that you can compile and run for yourself. You will need Java 8 as it comes with Base 64 encoder/decoder now.
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.net.URI;
import java.net.URL;
import java.security.MessageDigest;
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;
import java.util.TimeZone;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
//import org.apache.commons.codec.binary.Base64;
public class MainJava {
private static final String myEncoding = "UTF-8";
private static final String myMessageDigest = "SHA-1";
private static final String myKeySpec = "HmacSHA1";
private static String NEWLINE = "\n";
private static String authorizationHeader;
private static String contentSha1;
// private static String keyId = "230297";
// private static String hmacKey = "tcwR9r1OR85V9bcV5tc7a9d1XkWigjqY";
private static String ApiSecretkey = "0779eb593286b278aaf8cfcf83c8e33bc757d53a8a642b53d24d63bda844da5b";
private static String MerchantToken = "fdoa-a480ce8951daa73262734cf102641994c1e55e7cdf4c02b6";
private static String reportingToken = "e56a0223d0415067";
private static String apikey = "XSjbv8PLDINJ28qXLEYAhcrz8rxKXQ4Y";
private static long nonce;
public static String headerContentType = "application/json";
private static long epoch;
public static void main(String[] args) throws Exception {
String json_string_dataTwo = "{\"type\":\"visa\",\"cardholder_name\":\"John Smith\",\"card_number\":\"4788250000028291\",\"exp_date\":1020,\"cvv\":\"123\"}";
// String json_string =
// "{\"gateway_id\":\"AI2010-01\",\"password\":\"w226638qtot48xu503zumwt2iy46g26q\",\"transaction_type\":\"00\",\"amount\":10,\"cardholder_name\":\"test\",\"cc_number\":\"4111111111111111\",\"cc_expiry\":\"1219\"}";
String json_string_data = "{\"merchant_ref\":\"Astonishing-Sale\",\"transaction_type\":\"authorize\",\"method\":\"credit_card\",\"amount\":1299,\"currency_code\":\"USD\",\"credit_card\":"
+ json_string_dataTwo + "}";
// "{\r\n \"merchant_ref\": \"Astonishing-Sale\",\r\n \"transaction_type\": \"authorize\",\r\n \"method\": \"credit_card\",\r\n \"amount\": \"1299\",\r\n \"currency_code\": \"USD\",\r\n \"credit_card\": {\r\n \"type\": \"visa\",\r\n \"cardholder_name\": \"John Smith\",\r\n \"card_number\": \"4788250000028291\",\r\n \"exp_date\": \"1020\",\r\n \"cvv\": \"123\"\r\n }\r\n}";
epoch = System.currentTimeMillis();// / 1000;
// nonce = UUID.randomUUID().toString();
nonce = Math.abs(SecureRandom.getInstance("SHA1PRNG").nextLong());
contentSha1 = contentSha1(json_string_data);
authorizationHeader = authHeader(epoch, contentSha1);
System.out.println(excutePost(json_string_data));
}
private static String authHeader(long hashTime, String contentSha1) {
String authorizationHeader = null;
try {
String hmacString = "POST" + NEWLINE + "application/json" + NEWLINE + contentSha1 + NEWLINE + hashTime + NEWLINE + apikey + NEWLINE
+ new URI("https://api-cert.payeezy.com/v1/transactions");
return sha1(hmacString, ApiSecretkey);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static String contentSha1(String content) throws Exception {
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] sha1hash = new byte[40];
md.update(content.getBytes("UTF-8"), 0, content.length());
sha1hash = md.digest();
return convertToHex(sha1hash);
}
private static String convertToHex(byte[] data) {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < data.length; i++) {
int halfbyte = data[i] >>> 4 & 0xF;
int two_halfs = 0;
do {
if ((0 <= halfbyte) && (halfbyte <= 9))
buf.append((char) (48 + halfbyte));
else
buf.append((char) (97 + (halfbyte - 10)));
halfbyte = data[i] & 0xF;
} while (two_halfs++ < 1);
}
return buf.toString();
}
// private static String sha1(String s, String keyString) {
// Base64 base64 = new Base64();
// try {
// SecretKeySpec key = new SecretKeySpec(keyString.getBytes("UTF-8"),
// "HmacSHA1");
// Mac mac = Mac.getInstance("HmacSHA1");
// mac.init(key);
// byte[] bytes = mac.doFinal(s.getBytes("UTF-8"));
//
// return new String(base64.encode(bytes));
// } catch (Exception e) {
// throw new RuntimeException(e);
// }
// }
private static String sha1(String s, String keyString) {
byte[] bytes = null;
try {
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(keyString.getBytes(), "HmacSHA256");
sha256_HMAC.init(secret_key);
bytes = sha256_HMAC.doFinal(s.getBytes("UTF-8"));
//return new String(Base64.encodeBase64String(bytes));
} catch (Exception e) {
System.out.println("Error");
}
return Base64.getEncoder().encodeToString(bytes);
}
private static String hashTime() {
String time = getUTCFormattedDate("yyyy-MM-dd'T'HH:mm:ss'Z'");
return time;
}
private static String getUTCFormattedDate(String format) {
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
return dateFormat.format(new Date());
}
public static String excutePost(String urlParameters) throws IOException {
System.out.println(urlParameters);
System.out.println(headerContentType);
System.out.println(MerchantToken);
System.out.println(authorizationHeader);
System.out.println(epoch);
System.out.println(nonce);
URL url = new URL("https://api-cert.payeezy.com/v1/transactions");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
try {
// Create connection
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", headerContentType);
connection.setRequestProperty("apikey ", apikey);
connection.setRequestProperty("token", MerchantToken);
connection.setRequestProperty("Authorization", authorizationHeader);
connection.setRequestProperty("timestamp", "" + epoch);
connection.setRequestProperty("nonce", "" + nonce);
connection.setDoOutput(true);
connection.setReadTimeout(30000);
// Send request
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
// Get Response
InputStream is = null;
int statusCode = connection.getResponseCode();
try {
is = connection.getInputStream();
} catch (IOException e) {
if (statusCode >= 400) {
is = connection.getErrorStream();
}
}
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while ((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
return response.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
}
My only issue is with the character encoding, where I assume UTF-8. I suspect that the error lies elsewhere.
// Send request
byte[] data = urlParameters.getBytes(StandardCharsets.UTF_8);
BufferedOutputStream wr = new BufferedOutputStream(connection.getOutputStream());
wr.writeBytes(data);
wr.close();
// Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is,
StandardCharsets.UTF_8));
And \r, CR, does not serve as line separator (apart from old MacOS).
response.append("\r\n"); // Or '\n'
Related
I have a java application where I send an http request to communicate with the discord api. The http request class is below.
package http;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
public class HttpRequest {
private HttpURLConnection con;
private String postData;
private String charset;
public HttpRequest(final String requestURL, final String charset, final String requestMethod,
final Map<String, String> headers, final String postData) throws IOException {
this.charset = charset;
this.postData = postData;
final URL url = new URL(requestURL);
(this.con = (HttpURLConnection) url.openConnection()).setConnectTimeout(15000);
this.con.setReadTimeout(60000);
this.con.setUseCaches(false);
this.con.setDoOutput(true);
this.con.setDoInput(true);
this.con.setRequestMethod(requestMethod);
if (headers != null && headers.size() > 0) {
for (final String key : headers.keySet()) {
final String value = headers.get(key);
this.con.setRequestProperty(key, value);
}
}
}
public HttpRequest(final String requestURL, final String charset, final String requestMethod,
final Map<String, String> headers) throws IOException {
this(requestURL, charset, requestMethod, headers, null);
}
public HttpRequest(final String requestURL, final String charset, final String requestMethod) throws IOException {
this(requestURL, charset, requestMethod, null);
}
public void addHeader(final String key, final String value) {
this.con.setRequestProperty(key, value);
}
public void setPostData(final String postData) {
this.postData = postData;
}
private byte[] getParamsByte() {
byte[] result = null;
try {
result = this.postData.toString().getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return result;
}
public String finish() throws IOException {
String response = "";
if (this.postData != null) {
final byte[] postDataBytes = this.getParamsByte();
this.con.getOutputStream().write(postDataBytes);
}
final int status = this.con.getResponseCode();
System.out.println("HTTP status code: " + status);
if (status == 200 || status == 204 || status == 201) {
final ByteArrayOutputStream result = new ByteArrayOutputStream();
final byte[] buffer = new byte[1024];
int length;
while ((length = this.con.getInputStream().read(buffer)) != -1) {
result.write(buffer, 0, length);
}
response = result.toString(this.charset);
this.con.disconnect();
} else {
final ByteArrayOutputStream result = new ByteArrayOutputStream();
final byte[] buffer = new byte[1024];
int length;
while ((length = this.con.getErrorStream().read(buffer)) != -1) {
result.write(buffer, 0, length);
}
response = result.toString(this.charset);
this.con.disconnect();
System.out.println("HTTP Request Error:\n" + response);
}
return response;
}
public int statusCode() {
int rc = 0;
try {
rc = this.con.getResponseCode();
} catch (IOException e) {
e.printStackTrace();
}
return rc;
}
}
I send the http request like this:
try {
final HttpRequest rq = new HttpRequest("https://discord.com/api/v8/guilds/"+this.serverID+"/channels", "utf-8", "GET");
rq.addHeader("Content-Type", "application/json");
rq.addHeader("Authorization", "Bot " + this.token);
rq.setPostData(null);
res = rq.finish();
System.out.println(res);
} catch (Exception e) {
e.printStackTrace();
}
When I run the application in eclipse, It works as expected, returning all of the discord channels and status code 200. However, executing the jar file created by running as maven install, I am blocked by cloudflare get 403 forbidden and error code 1010. I am completely confused as to why this is occurring. Please let me know if there is a solution.
My question is how to add basic auth Java Android AsyncTask? Some of developers said it needs to be declared in RequestHandler.java or in doInBackground AsyncTask function. Below is my code:
private void loginTask(String _username, String _password){
final String username = _username;
final String password = _password;
class LoginTask extends AsyncTask<Void,Void,String> {
ProgressDialog loading;
#Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(LoginActivity.this,"Fetching...","Wait...",false,false);
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
}
#Override
protected String doInBackground(Void... params) {
RequestHandler rh = new RequestHandler();
String s = rh.sendGetRequest(App.URL_AUTHENTICATION);
Toast.makeText(LoginActivity.this, s.toString(), Toast.LENGTH_SHORT).show();
return s;
}
}
LoginTask gt = new LoginTask();
gt.execute();
}
RequestHandler class: https://github.com/IntellijSys/AndroidToDoList/blob/master/app/src/main/java/my/intellij/androidtodolist/RequestHandler.java
Try this
RequestHandler rh = new RequestHandler();
// your basic auth username and password
rh.setBasicAuth("username","password");
String s = rh.sendGetRequest(App.URL_AUTHENTICATION);
RequestHandler class with Basic auth
import android.util.Base64;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
/**
* Created by ZERO on 16/08/2016.
*/
public class RequestHandler {
private String username;
private String password;
//Method to send httpPostRequest
//This method is taking two arguments
//First argument is the URL of the script to which we will send the request
//Other is an HashMap with name value pairs containing the data to be send with the request
public String sendPostRequest(String requestURL,
HashMap<String, String> postDataParams) {
URL url;
//StringBuilder object to store the message retrieved from the server
StringBuilder sb = new StringBuilder();
try {
//Initializing Url
url = new URL(requestURL);
//Creating an httmlurl connection
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//set Basic auth
processBasicAuth(conn);
//Configuring connection properties
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
//Creating an output stream
OutputStream os = conn.getOutputStream();
//Writing parameters to the request
//We are using a method getPostDataString which is defined below
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode = conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
sb = new StringBuilder();
String response;
//Reading server response
while ((response = br.readLine()) != null) {
sb.append(response);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
private void processBasicAuth(HttpURLConnection conn) {
if (username != null && password != null) {
try {
String userPassword = username + ":" + password;
byte[] data = userPassword.getBytes("UTF-8");
String base64 = Base64.encodeToString(data, Base64.DEFAULT);
conn.setRequestProperty("Authorization", "Basic " + base64);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
public String sendGetRequest(String requestURL) {
StringBuilder sb = new StringBuilder();
try {
URL url = new URL(requestURL);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
//set Basic auth
processBasicAuth(con);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String s;
while ((s = bufferedReader.readLine()) != null) {
sb.append(s + "\n");
}
} catch (Exception e) {
}
return sb.toString();
}
public String sendGetRequestParam(String requestURL, String id) {
StringBuilder sb = new StringBuilder();
try {
URL url = new URL(requestURL + id);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String s;
while ((s = bufferedReader.readLine()) != null) {
sb.append(s + "\n");
}
} catch (Exception e) {
}
return sb.toString();
}
private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for (Map.Entry<String, String> entry : params.entrySet()) {
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
return result.toString();
}
public void setBasicAuth(String username, String password) {
this.username = username;
this.password = password;
}
}
If I use Action=GetServiceStatus, the program below works fine, returning "200 OK" and an XML response as MWS scratchpad.
But if I just change Action="GetOrder" (and add the required extra string AmazonOrderId.Id.1=xxx-5684184-6801000) then it gives me error 403 "SignatureDoesNotMatch" <RequestID>b055a685-dfbb-494b-8cf5-bcec31e2fa3a</RequestID>.
The program, except for sending the request to read the URL, is taken from Amazon documentation. I copied the query string with the URL.
public class AmazonExample {
private static final String CHARACTER_ENCODING = "UTF-8";
final static String ALGORITHM = "HmacSHA256";
public static void main(String[] args) throws Exception {
String secretKey = "/ioTb2imZWZ/IHTKKfc62BFvBxxxxxxxxxxxxxxx";
// Use the endpoint for your marketplace
String serviceUrl = "https://mws.amazonservices.ca/";
// Create set of parameters needed and store in a map
HashMap<String, String> parameters = new HashMap<String,String>();
// Add required parameters. Change these as needed.
parameters.put("AWSAccessKeyId", urlEncode("AKIAJUTBJCxxxxxxxxxx"));
parameters.put("Action", urlEncode("GetServiceStatus")); //GetOrder
parameters.put("AmazonOrderId.Id.1", urlEncode("xxx-5684184-6801000"));
parameters.put("MWSAuthToken", urlEncode("amzn.mws.xxxxx7b8-5c81-3abc-06c2-c09e7dfd6ef3"));
parameters.put("SellerId", urlEncode("xxxxYI70TZB97A"));
parameters.put("SignatureMethod", urlEncode(ALGORITHM));
parameters.put("SignatureVersion", urlEncode("2"));
parameters.put("SubmittedFromDate",urlEncode("2015-03-14T17:02:05.264Z"));
parameters.put("Timestamp", urlEncode("2015-03-14T17:02:05.264Z"));
parameters.put("Version", urlEncode("2013-09-01"));
String formattedParameters = calculateStringToSignV2(parameters, serviceUrl);
String signature = sign(formattedParameters, secretKey);
// Add signature to the parameters and display final results
parameters.put("Signature", urlEncode(signature));
System.out.println(calculateStringToSignV2(parameters, serviceUrl));
try {
URL url = new URL("https://mws.amazonservices.ca/Orders/2013-09-01/?AWSAccessKeyId=xxxxxxxBJCIA4YSSWYNA&Action=GetServiceStatus&AmazonOrderId.Id.1=xxx-5684184-6801000&MWSAuthToken=amzn.mws.xxxxxxxx-5c81-3abc-06c2-c09e7dfd6ef3&SellerId=xxxxYI70TZB97A&Signature=yyO%2BrwMAtCcuEsYhG4KZILz2cyiSUcrAAWKqf3%2BZ454%3D&SignatureMethod=HmacSHA256&SignatureVersion=2&SubmittedFromDate=2015-03-14T17%3A02%3A05.264Z&Timestamp=2015-03-14T17%3A02%3A05.264Z&Version=2013-09-01");
HttpsURLConnection conn = (HttpsURLConnection)url.openConnection();
conn.setConnectTimeout(50000);
BufferedReader br = null;
StringBuffer sbOrderResponse= new StringBuffer();
String order = ""; String str = "";
InputStream inputstream = null;
if(conn.getResponseCode() != 200 ) {
inputstream = conn.getErrorStream();
} else{
inputstream = conn.getInputStream();
}
br = new BufferedReader(new InputStreamReader((inputstream)));
StringBuffer buffer = new StringBuffer();
while ((order = br.readLine()) != null) {
sbOrderResponse.append(order);
str = str + order + "\n";
}
System.out.println(conn.getResponseCode() + " " + conn.getResponseMessage());
System.out.println(str);
} catch(Exception e)
{
System.out.println("Error " + e);
}
}
private static String calculateStringToSignV2(Map<String, String> parameters, String serviceUrl)
throws SignatureException, URISyntaxException {
// Sort the parameters alphabetically by storing
// in TreeMap structure
Map<String, String> sorted = new TreeMap<String, String>();
sorted.putAll(parameters);
// Set endpoint value
URI endpoint = new URI(serviceUrl.toLowerCase());
// Create flattened (String) representation
StringBuilder data = new StringBuilder();
data.append("GET\n");
data.append(endpoint.getHost());
data.append("\n/"); // /Orders/2013-09-01
data.append("\n");
Iterator<Entry<String, String>> pairs = sorted.entrySet().iterator();
while (pairs.hasNext()) {
Map.Entry<String, String> pair = pairs.next();
if (pair.getValue() != null) {
data.append( pair.getKey() + "=" + pair.getValue());
}
else {
data.append( pair.getKey() + "=");
}
// Delimit parameters with ampersand (&)
if (pairs.hasNext()) {
data.append( "&");
}
}
return data.toString();
}
/*
* Sign the text with the given secret key and convert to base64
*/
private static String sign(String data, String secretKey) throws NoSuchAlgorithmException, InvalidKeyException,
IllegalStateException, UnsupportedEncodingException {
Mac mac = Mac.getInstance(ALGORITHM);
mac.init(new SecretKeySpec(secretKey.getBytes(CHARACTER_ENCODING), ALGORITHM));
byte[] signature = mac.doFinal(data.getBytes(CHARACTER_ENCODING));
String signatureBase64 = new String(Base64.encodeBase64(signature), CHARACTER_ENCODING);
return new String(signatureBase64);
}
private static String urlEncode(String rawValue) {
String value = (rawValue == null) ? "" : rawValue;
String encoded = null;
try {
encoded = URLEncoder.encode(value, CHARACTER_ENCODING)
.replace("+", "%20")
.replace("*", "%2A")
.replace("%7E","~");
} catch (UnsupportedEncodingException e) {
System.err.println("Unknown encoding: " + CHARACTER_ENCODING);
e.printStackTrace();
}
return encoded;
}}
I need to search Goolge Places Pages by long/lat for any banks in the area of 20m.
This Google Places Doc describes how to do it with JavaScript. They are using a google.maps.LatLng Object that i don't have in Java.
Does anyone now how to call the Service?
Maybe there is also an Java API for Goolge Places?
Best Regards,
Christian.
Edit 1:
I found someone constructing the url like this:
String url = baseUrl + "location=" + lat + "," + lon + "&" +
"radius=" + searchRadius + "&" + types + "&" + "sensor=true" +
"&" + "key=" + googleAPIKey;
Answer: Edit 2:
I because of the post above i found out how to do it. This is a example how to send the request:
public class GooglePlacesClient
{
private static final String GOOGLE_API_KEY = "***";
private final HttpClient client = new DefaultHttpClient();
public static void main(final String[] args) throws ParseException, IOException, URISyntaxException
{
new GooglePlacesClient().performSearch("establishment", 8.6668310, 50.1093060);
}
public void performSearch(final String types, final double lon, final double lat) throws ParseException, IOException, URISyntaxException
{
final URIBuilder builder = new URIBuilder().setScheme("https").setHost("maps.googleapis.com").setPath("/maps/api/place/search/json");
builder.addParameter("location", lat + "," + lon);
builder.addParameter("radius", "5");
builder.addParameter("types", types);
builder.addParameter("sensor", "true");
builder.addParameter("key", GooglePlacesClient.GOOGLE_API_KEY);
final HttpUriRequest request = new HttpGet(builder.build());
final HttpResponse execute = this.client.execute(request);
final String response = EntityUtils.toString(execute.getEntity());
System.out.println(response);
}
}
Here's a more complete example (includes JSON parsing and some exception handling) for Places API search, autocomplete, and details. It was written for Android, but can be easily ported for non-Android use (need to include org.json libs and use different logging). The Place class is a simple value object.
package com.example.google.places;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
/**
* #author saxman
*/
public class PlacesService {
private static final String LOG_TAG = "ExampleApp";
private static final String PLACES_API_BASE = "https://maps.googleapis.com/maps/api/place";
private static final String TYPE_AUTOCOMPLETE = "/autocomplete";
private static final String TYPE_DETAILS = "/details";
private static final String TYPE_SEARCH = "/search";
private static final String OUT_JSON = "/json";
// KEY!
private static final String API_KEY = "YOUR KEY";
public static ArrayList<Place> autocomplete(String input) {
ArrayList<Place> resultList = null;
HttpURLConnection conn = null;
StringBuilder jsonResults = new StringBuilder();
try {
StringBuilder sb = new StringBuilder(PLACES_API_BASE);
sb.append(TYPE_AUTOCOMPLETE);
sb.append(OUT_JSON);
sb.append("?sensor=false");
sb.append("&key=" + API_KEY);
sb.append("&input=" + URLEncoder.encode(input, "utf8"));
URL url = new URL(sb.toString());
conn = (HttpURLConnection) url.openConnection();
InputStreamReader in = new InputStreamReader(conn.getInputStream());
int read;
char[] buff = new char[1024];
while ((read = in.read(buff)) != -1) {
jsonResults.append(buff, 0, read);
}
} catch (MalformedURLException e) {
Log.e(LOG_TAG, "Error processing Places API URL", e);
return resultList;
} catch (IOException e) {
Log.e(LOG_TAG, "Error connecting to Places API", e);
return resultList;
} finally {
if (conn != null) {
conn.disconnect();
}
}
try {
// Create a JSON object hierarchy from the results
JSONObject jsonObj = new JSONObject(jsonResults.toString());
JSONArray predsJsonArray = jsonObj.getJSONArray("predictions");
// Extract the Place descriptions from the results
resultList = new ArrayList<Place>(predsJsonArray.length());
for (int i = 0; i < predsJsonArray.length(); i++) {
Place place = new Place();
place.reference = predsJsonArray.getJSONObject(i).getString("reference");
place.name = predsJsonArray.getJSONObject(i).getString("description");
resultList.add(place);
}
} catch (JSONException e) {
Log.e(LOG_TAG, "Error processing JSON results", e);
}
return resultList;
}
public static ArrayList<Place> search(String keyword, double lat, double lng, int radius) {
ArrayList<Place> resultList = null;
HttpURLConnection conn = null;
StringBuilder jsonResults = new StringBuilder();
try {
StringBuilder sb = new StringBuilder(PLACES_API_BASE);
sb.append(TYPE_SEARCH);
sb.append(OUT_JSON);
sb.append("?sensor=false");
sb.append("&key=" + API_KEY);
sb.append("&keyword=" + URLEncoder.encode(keyword, "utf8"));
sb.append("&location=" + String.valueOf(lat) + "," + String.valueOf(lng));
sb.append("&radius=" + String.valueOf(radius));
URL url = new URL(sb.toString());
conn = (HttpURLConnection) url.openConnection();
InputStreamReader in = new InputStreamReader(conn.getInputStream());
int read;
char[] buff = new char[1024];
while ((read = in.read(buff)) != -1) {
jsonResults.append(buff, 0, read);
}
} catch (MalformedURLException e) {
Log.e(LOG_TAG, "Error processing Places API URL", e);
return resultList;
} catch (IOException e) {
Log.e(LOG_TAG, "Error connecting to Places API", e);
return resultList;
} finally {
if (conn != null) {
conn.disconnect();
}
}
try {
// Create a JSON object hierarchy from the results
JSONObject jsonObj = new JSONObject(jsonResults.toString());
JSONArray predsJsonArray = jsonObj.getJSONArray("results");
// Extract the Place descriptions from the results
resultList = new ArrayList<Place>(predsJsonArray.length());
for (int i = 0; i < predsJsonArray.length(); i++) {
Place place = new Place();
place.reference = predsJsonArray.getJSONObject(i).getString("reference");
place.name = predsJsonArray.getJSONObject(i).getString("name");
resultList.add(place);
}
} catch (JSONException e) {
Log.e(LOG_TAG, "Error processing JSON results", e);
}
return resultList;
}
public static Place details(String reference) {
HttpURLConnection conn = null;
StringBuilder jsonResults = new StringBuilder();
try {
StringBuilder sb = new StringBuilder(PLACES_API_BASE);
sb.append(TYPE_DETAILS);
sb.append(OUT_JSON);
sb.append("?sensor=false");
sb.append("&key=" + API_KEY);
sb.append("&reference=" + URLEncoder.encode(reference, "utf8"));
URL url = new URL(sb.toString());
conn = (HttpURLConnection) url.openConnection();
InputStreamReader in = new InputStreamReader(conn.getInputStream());
// Load the results into a StringBuilder
int read;
char[] buff = new char[1024];
while ((read = in.read(buff)) != -1) {
jsonResults.append(buff, 0, read);
}
} catch (MalformedURLException e) {
Log.e(LOG_TAG, "Error processing Places API URL", e);
return null;
} catch (IOException e) {
Log.e(LOG_TAG, "Error connecting to Places API", e);
return null;
} finally {
if (conn != null) {
conn.disconnect();
}
}
Place place = null;
try {
// Create a JSON object hierarchy from the results
JSONObject jsonObj = new JSONObject(jsonResults.toString()).getJSONObject("result");
place = new Place();
place.icon = jsonObj.getString("icon");
place.name = jsonObj.getString("name");
place.formatted_address = jsonObj.getString("formatted_address");
if (jsonObj.has("formatted_phone_number")) {
place.formatted_phone_number = jsonObj.getString("formatted_phone_number");
}
} catch (JSONException e) {
Log.e(LOG_TAG, "Error processing JSON results", e);
}
return place;
}
}
A Java library for working with the Google Places API is available on GitHub and in Maven Central (disclosure: I'm the developer.) Getting a list of places (or details, photo, etc.) can be done in one or two lines. See the project page for examples and set up details.
https://github.com/pushbit/sprockets
There doesn't exist any official Java library available for Google Places API. However, there are several projects hosted on Github. Another one is this:
Google Places API Java Library on Github
I can't figure this one out. I'm trying to dynamically roll keys. I can create the POST request fine, but receive a 400 error and a stacktrace with an IOException when I call post. Below is a self-contained example. I'm using JSCH to generate keys. API doc: http://developer.github.com/v3/users/keys/
The API call: POST /user/keys
public static class LiberalHostnameVerifier implements HostnameVerifier {
public boolean verify(String hostname, SSLSession session) {
return true;
}
}
public static String post(String requestUrl, Map<String, String> params,
String username, String password) throws Exception {
String data = "";
int paramCount = 1;
for (Entry<String, String> param : params.entrySet()) {
if (paramCount == 1) {
data = URLEncoder.encode(param.getKey(), "UTF-8") + "="
+ URLEncoder.encode(param.getValue(), "UTF-8");
} else {
data += "&" + URLEncoder.encode(param.getKey(), "UTF-8") + "="
+ URLEncoder.encode(param.getValue(), "UTF-8");
}
paramCount++;
}
URL url = new URL(requestUrl);
HttpsURLConnection conn = (HttpsURLConnection) (url).openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setHostnameVerifier(new LiberalHostnameVerifier());
BASE64Encoder enc = new BASE64Encoder();
String userAuth = username + ":" + password;
String encodedAuthorization = enc.encode(userAuth.getBytes());
conn.setRequestProperty("Authorization", "Basic " + encodedAuthorization);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
BufferedReader rd = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String line;
String response = "";
while ((line = rd.readLine()) != null) {
response += line;
}
wr.close();
rd.close();
return response;
}
public static KeyPair generateKey(String filename) throws Exception {
JSch jsch = new JSch();
try {
KeyPair kpair = KeyPair.genKeyPair(jsch, KeyPair.RSA);
kpair.setPassphrase("");
kpair.writePrivateKey(filename + ".pem");
kpair.writePublicKey(filename + ".pub", "Auto-generated.");
System.out.println("Finger print: " + kpair.getFingerPrint());
// kpair.dispose();
return kpair;
} catch (Exception e) {
System.out.println(e);
}
return null;
}
public static String getFileContents(File file) throws Exception {
byte[] buffer = new byte[(int) file.length()];
FileInputStream f = new FileInputStream(file);
f.read(buffer);
return new String(buffer);
}
public static String createKey(String title) throws Exception {
generateKey(title);
final String key = getFileContents(new File(
"/Users/franklovecchio/Desktop/development/" + title
+ ".pub"));
System.out.println("key: " + key);
Map<String, String> params = new HashMap<String, String>() {
{
put("title", title);
put("key", key);
}
};
return post("https://api.github.com/user/keys", params, "username",
"password");
}
// call createKey("key);
Thanks to #nico_ekito and #J-16 SDiZ for helping in the right direction. If you look closely at the documentation, the request doesn't use standard POST parameters, but rather takes JSON as Raw Input, and the ssh-rsa key can NOT be encoded. Next up, I can't get GSON to not encode a string, even using disableHtmlEscaping. So, I had to fake it:
String json = "{\"title\":\"" + title + "\",\"key\":\"" + key.trim() + "\"}";
Did you try a ssh library (e.g. JSch). They can generate RSA key in SSH consumable format.