· 4 years ago · May 25, 2021, 05:42 AM
1private static String key = "ck_your_consumer_key";
2 private static String secret = "cs_your_consumer_secret";
3
4 private static final String HMAC_SHA1 = "HmacSHA1";
5
6 private static final String ENC = "UTF-8";
7
8 private static Base64 base64 = new Base64();
9
10 private static String getSignature(String url, String params)
11 throws UnsupportedEncodingException, NoSuchAlgorithmException,
12 InvalidKeyException, EncoderException {
13 /**
14 * base has three parts, they are connected by "&": 1) protocol 2) URL
15 * (need to be URLEncoded) 3) Parameter List (need to be URLEncoded).
16 */
17 StringBuilder base = new StringBuilder();
18 base.append("GET&");
19
20 //follow Step 2 and encode
21 base.append(new URLCodec(Consts.UTF_8.displayName()).encode(url));
22 base.append("&");
23 base.append(params);
24
25 System.out.println("String for oauth_signature generation: " + base);
26
27 byte[] keyBytes = (String.format("%s", secret)).getBytes(ENC);
28
29 SecretKey key = new SecretKeySpec(keyBytes, HMAC_SHA1);
30
31 Mac mac = Mac.getInstance(HMAC_SHA1);
32 mac.init(key);
33
34 // encode it, base64 it, change it to string and return.
35 String signature = new String(base64.encode(mac.doFinal(base.toString().getBytes(ENC))), ENC).trim();
36 return signature;
37 }
38
39
40 public static void main(String[] args) throws IOException, URISyntaxException, InvalidKeyException,
41 NoSuchAlgorithmException, EncoderException {
42
43 String nonce = RandomStringUtils.randomAlphanumeric(32);
44
45 Map<String, String> paramMap = new TreeMap<>();
46 paramMap.put("oauth_consumer_key", key);
47 paramMap.put("oauth_timestamp", "" + (System.currentTimeMillis() / 1000));
48 paramMap.put("oauth_nonce", nonce);
49 paramMap.put("oauth_signature_method", "HMAC-SHA1");
50
51 List<NameValuePair> qparams = new ArrayList<>();
52
53 //uksort( $params, 'strcmp' ) mimic
54 paramMap.entrySet().stream().forEach(stringStringEntry -> qparams.add(new BasicNameValuePair(stringStringEntry.getKey(), stringStringEntry.getValue())));
55
56 //double encode Step 3 (in order to replace '%' with '%25') after sorting (Step 4)
57 String encodedParams = URLEncoder.encode(URLEncodedUtils.format(qparams, ENC), ENC);
58
59 System.out.println("Encoded Params "+ encodedParams);
60
61 String signature = getSignature("http://your_end_url/wc-api/v2/orders", encodedParams);
62
63 qparams.add(new BasicNameValuePair("oauth_signature", signature));
64
65 HttpClient httpclient = HttpClientBuilder.create().build();
66
67 System.out.println("Getting Oauth Signature...");
68
69 // generate URI which lead to access_token and token_secret.
70 URI uri = URIUtils.createURI("http", "your_end_url", -1, "wc-api/v2/orders", URLEncodedUtils.format(qparams, ENC), null);
71
72 System.out.println("Connecting to the URL : \n" + uri.toString());
73
74 HttpGet httpget = new HttpGet(uri);
75 httpget.setHeader("X-Stream" , "true");
76
77 // output the response content.
78 System.out.println("Getting Response from the server :");
79
80 HttpResponse response = httpclient.execute(httpget);
81 HttpEntity entity = response.getEntity();
82
83 System.out.println("Response Code " + response.getStatusLine().getStatusCode());
84
85 if (entity != null) {
86 System.out.println("Json response" + IOUtils.toString(entity.getContent(), ENC));
87 }
88 }