1

I am trying to implement my server API call in my Android application, which is needed AWS signed header, The same headers are working with the postman and node.js but not working with Android code. I am using latest version of AWS mobile client ie 2.9.1.

I have tried with different combinations of headers, I tried adding dummy access token, session key. With every combination it is returning 403 only.

private void startProcess(View view) {        
        buildRequest();
        new SessionCredentialLoader().execute();
    }

private void buildRequest(){

        awsRequest = generateBasicRequest(URL);
        Map<String, String> requestHeaders =  getSignHeader(awsRequest);

        okhttp3.Request.Builder builder = new okhttp3.Request.Builder().url(URL).get();


        for (HashMap.Entry<String, String> entrySet : requestHeaders.entrySet()) {            

            String key = entrySet.getKey();
            String value = entrySet.getValue();
            builder.addHeader(key,value );
            Log.d(TAG, "Header() "+key+" : "+value);
        }
        request =builder.build();
    }

    public  Map<String, String> getSignHeader(com.amazonaws.Request request){
        AWS4Signer signer = new AWS4Signer();

        com.amazonaws.Request<?> aws;

        aws = request;
      /*  AWSCredentials credentials = new BasicAWSCredentials(
                *//*getAWSAccessKeyId*//* ACCESS_KEY,
                *//*getAWSSecretKey*//* SECRET_KEY);*/
        BasicSessionCredentials credentials = new BasicSessionCredentials(ACCESS_KEY, SECRET_KEY, SESSION_KEY);

        signer.setServiceName("execute-api");
        signer.setRegionName("ap-south-1");

        signer.sign(aws, credentials);
        Log.d(TAG, "getSignHeader() getHeaders(): "+aws.getHeaders().toString());        
        return aws.getHeaders();       
    }

    public  com.amazonaws.Request<?> generateBasicRequest(String url) {
        //com.amazonaws.Request<?> request = new DefaultRequest<Void>("execute-api");
        AmazonWebServiceRequest amazonWebServiceRequest = new AmazonWebServiceRequest() {
        };

        //ClientConfiguration clientConfiguration = new ClientConfiguration();

        String API_GATEWAY_SERVICE_NAME = "execute-api";

        com.amazonaws.Request<?> request = new DefaultRequest(amazonWebServiceRequest, API_GATEWAY_SERVICE_NAME);

        request.addHeader("Content-type", "application/json");
        //request.addHeader("Content-Type","application/x-www-form-urlencoded");  
        request.addHeader("x-api-key",  XAPI_KEY);               
        // request.setResourcePath("/");
        request.setEndpoint(URI.create(url));
        request.setResourcePath(url);
        request.setHttpMethod(HttpMethodName.GET);
        return request;
    }

    private class SessionCredentialLoader extends AsyncTask<Void, Void, Boolean> {


        @Override
        protected Boolean doInBackground(Void... voids) {
            try {
                response = client.newCall(request).execute();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return (response != null);
        }

        @Override
        protected void onPostExecute(Boolean result) {
            Log.d(TAG, "onPostExecute() result: "+result );
            Log.d(TAG, "onPostExecute() response: "+response.toString()    );         

        }
    }

I am expecting the API should be hit and I should get the response from my CMS. Can any please let me know, where I am making the mistake. These are the header are being added into http reqest:

Header() X-Amz-Date : 20181224T112843Z
Header() Host : abc.amazonaws.com
Header() x-api-key : 23423432432342XYZ
Header() Content-type : application/json
Header() x-amz-security-token : abc......xyz
Header() Authorization : AWS4-HMAC-SHA256 Credential=ACCESS_KEY/20181224/ap-south-1/execute-api/aws4_request, SignedHeaders=host;x-amz-date;x-amz-security-token, Signature=85246b145dbef7b119c93ee71c9ee7dbd0f017893cc25b162234445149a91461

Generated error:

{"message":"The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details. \n\n The Canonical String for this request should have been\n'POST\n/dev/api/client/getAllChannels\n\nhost:abc.amazonaws.com \n x-amz-date:20181225T121555Z\nx-amz-security-token:abcsessionToken'\n\n The String-to-Sign should have been\n'AWS4-HMAC-SHA256\n 20181225T121555Z\n20181225/ap-south-1/execute-api/aws4_request\signatureCode'\n"}
Nero
  • 1,058
  • 1
  • 9
  • 25
DkR
  • 11
  • 2
  • 1
    Can you share the error log? – Nero Dec 24 '18 at 12:09
  • Response{protocol=h2, code=403, message=, url=https://MY_SERVER_URL} – DkR Dec 25 '18 at 09:07
  • I replace the request from HttpUrlConnection and got the below error msg with the same error code 403: – DkR Dec 26 '18 at 04:49
  • {"message":"The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details. \n\n The Canonical String for this request should have been\n'POST\n/dev/api/client/getAllChannels\n\nhost:abc.amazonaws.com \n x-amz-date:20181225T121555Z\nx-amz-security-token:abcsessionToken'\n\n The String-to-Sign should have been\n'AWS4-HMAC-SHA256\n 20181225T121555Z\n20181225/ap-south-1/execute-api/aws4_request\signatureCode'\n"} – DkR Dec 26 '18 at 04:50
  • Get in the habit of not writing code/errors in the comment section if you are the OP. Having these aspects in the actual questions just makes it more readable for readers. – Nero Dec 27 '18 at 09:25
  • `request.setHttpMethod(HttpMethodName.GET);` shouldn't this be a POST request based on the error you've provided? Are you doing a POST request or GET request on POSTMAN? – Nero Dec 27 '18 at 09:35
  • This is a GET request, which is working with Postman. – DkR Dec 31 '18 at 05:56
  • 1
    If you are certain that the header information is a replicate of what was being used in POSTMAN, I would recommend you to debug the application and check for inadvertent leading or trailing space on your secret access key. Looking at the history of this error type, this is often the problem. – Nero Dec 31 '18 at 07:46

0 Answers0