/* Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. */ /* Licensed under the Apache License, Version 2.0. */ /** * Download the following artifacts and add to your projects classpath: * o httpclient-4.5.5.jar * o httpcore-4.4.9.jar * o commons-logging-1.2.jar * o json-20180130.jar * Put your Secret Key in place of ********** * Make sure AWSV4Auth Class is available in same package */ package com.amazon.paapi.scratchpad; import java.util.Map; import java.util.TreeMap; import java.nio.charset.StandardCharsets; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; import org.json.JSONArray; import org.json.JSONObject; import com.amazon.paapi.scratchpad.AWSV4Auth; public class GetItems { private static final String HOST = "webservices.amazon.com"; private static final String URI_PATH = "/paapi5/getitems"; private static final String ACCESS_KEY = "thailyny95@gmail.com"; private static final String SECRET_KEY = "**********"; private static final String REGION = "us-east-1"; public static void main(String[] args) throws Exception { String requestPayload = "{" +" \"ItemIds\": [" +" \"B0DGQ31FT6\"" +" ]," +" \"Resources\": [" +" \"CustomerReviews.Count\"," +" \"CustomerReviews.StarRating\"," +" \"Images.Primary.Large\"" +" ]," +" \"PartnerTag\": \"assfd134676-20\"," +" \"PartnerType\": \"Associates\"," +" \"Marketplace\": \"www.amazon.com\"" +"}"; TreeMap headers = new TreeMap(); headers.put("host", HOST); headers.put("content-type", "application/json; charset=UTF-8"); headers.put("x-amz-target", "com.amazon.paapi5.v1.ProductAdvertisingAPIv1.GetItems"); headers.put("content-encoding", "amz-1.0"); AWSV4Auth awsv4Auth = new AWSV4Auth.Builder(ACCESS_KEY, SECRET_KEY) .path(URI_PATH) .region(REGION) .service("ProductAdvertisingAPI") .httpMethodName("POST") .headers(headers) .payload(requestPayload) .build(); HttpClient client = HttpClientBuilder.create().build(); HttpPost httpPost = new HttpPost("https://" + HOST + URI_PATH); httpPost.setEntity(new StringEntity(requestPayload)); Map header = awsv4Auth.getHeaders(); for (Map.Entry entrySet : header.entrySet()) { httpPost.addHeader(entrySet.getKey(), entrySet.getValue()); } HttpResponse response = client.execute(httpPost); HttpEntity entity = response.getEntity(); String jsonResponse = EntityUtils.toString(entity, StandardCharsets.UTF_8); int statusCode = response.getStatusLine().getStatusCode(); System.out.println(jsonResponse); if(statusCode == 200) { System.out.println("Successfully received response from Product Advertising API."); System.out.println(jsonResponse); } else { JSONObject json = new JSONObject(jsonResponse); if(json.has("Errors")) { JSONArray errorArray = json.getJSONArray("Errors"); for(int i = 0; i < errorArray.length(); i++) { JSONObject e = errorArray.getJSONObject(i); System.out.println("Error Code: "+e.get("Code")+", Message: "+e.get("Message")); } } else { System.out.println("Error Code: InternalFailure, Message: The request processing has failed because of an unknown error, exception or failure. Please retry again."); } } } }

Comments

Popular posts from this blog

Best 10 Online Learning Platforms In 2022 To Jumpstart Your Career

Insurance : Importance, Types and Benefits