public static void main()

in legacy/java/TopSites.java [130:182]


    public static void main(String[] args) throws Exception {
      if (args.length < 2) {
          System.out.println("Usage: java AlexaTopSites ACCESS_KEY_ID " +
                             "SECRET_ACCESS_KEY [COUNTRY_CODE]");
          System.exit(-1);
      }
      String accessKey = args[0];
      String secretKey = args[1];
      String countryCode = (args.length > 2) ? args[2] : "";

      TopSites topSites = new TopSites(accessKey, secretKey, countryCode);

        String canonicalQuery = "Action=" + ACTION_NAME + "&Count=" + NUMBER_TO_RETURN  + "&ResponseGroup=" + RESPONSE_GROUP_NAME + "&Start=" + START_NUMBER;
        String canonicalHeaders = "host:" + SERVICE_ENDPOINT + "\n" + "x-amz-date:" + topSites.amzDate + "\n";
        String signedHeaders = "host;x-amz-date";

        String payloadHash = topSites.sha256("");

        String canonicalRequest = "GET" + "\n" + SERVICE_URI + "\n" + canonicalQuery + "\n" + canonicalHeaders + "\n" + signedHeaders + "\n" + payloadHash;
        System.out.println(canonicalRequest);

        // ************* TASK 2: CREATE THE STRING TO SIGN*************
        // Match the algorithm to the hashing algorithm you use, either SHA-1 or
        // SHA-256 (recommended)
        String algorithm = "AWS4-HMAC-SHA256";
        String credentialScope = topSites.dateStamp + "/" + SERVICE_REGION + "/" + SERVICE_NAME + "/" + "aws4_request";
        String stringToSign = algorithm + '\n' +  topSites.amzDate + '\n' +  credentialScope + '\n' +  topSites.sha256(canonicalRequest);
        System.out.println(stringToSign);

        // ************* TASK 3: CALCULATE THE SIGNATURE *************
        // Create the signing key
        byte[] signingKey = topSites.getSignatureKey(secretKey, topSites.dateStamp, SERVICE_REGION, SERVICE_NAME);

        // Sign the string_to_sign using the signing_key
        String signature = bytesToHex(HmacSHA256(stringToSign, signingKey));

        String uri = AWS_BASE_URL + "?" + canonicalQuery;

        System.out.println("Making request to:\n");
        System.out.println(uri + "\n");

        // Make the Request

        String authorization = algorithm + " " + "Credential=" + accessKey + "/" + credentialScope + ", " +  "SignedHeaders=" + signedHeaders + ", " + "Signature=" + signature;
        System.out.println("Authorization: " + authorization);

        String xmlResponse = makeRequest(uri, authorization, topSites.amzDate);

        // Print out the XML Response

        System.out.println("Response:\n");
        System.out.println(xmlResponse);
    }